From ef69fa453c451e68d26969343f40042300cc6193 Mon Sep 17 00:00:00 2001 From: Tanguy PATTE Date: Mon, 17 Aug 2015 17:08:13 +0200 Subject: [PATCH] refactor demandes controller --- app/controllers/demandes_controller.rb | 7 ++++--- app/models/formulaire.rb | 4 ++++ app/views/demandes/show.html.haml | 17 ++++++++--------- config/routes.rb | 3 ++- spec/controllers/demandes_controller_spec.rb | 4 ++-- spec/models/formulaire_spec.rb | 7 +++++++ 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/controllers/demandes_controller.rb b/app/controllers/demandes_controller.rb index 0497aa776..312cde9e9 100644 --- a/app/controllers/demandes_controller.rb +++ b/app/controllers/demandes_controller.rb @@ -1,13 +1,14 @@ class DemandesController < ApplicationController def show @dossier = Dossier.find(params[:dossier_id]) - @evenement_vie = EvenementVie.for_admi_facile + @evenements_vie = EvenementVie.for_admi_facile end - def choice + def update @dossier = Dossier.find(params[:dossier_id]) @dossier.update_attributes(formulaire_id: params[:formulaire]) - redirect_to url_for( { controller: :carte, action: :show, :dossier_id => params[:dossier_id] } ) + redirect_to url_for(controller: :carte, action: :show, dossier_id: params[:dossier_id]) end + end diff --git a/app/models/formulaire.rb b/app/models/formulaire.rb index 7dec90c7d..2a6281ad3 100644 --- a/app/models/formulaire.rb +++ b/app/models/formulaire.rb @@ -2,4 +2,8 @@ class Formulaire < ActiveRecord::Base has_many :types_piece_jointe has_many :dossiers belongs_to :evenement_vie + + def self.for_admi_facile + where(use_admi_facile: true) + end end diff --git a/app/views/demandes/show.html.haml b/app/views/demandes/show.html.haml index 597efdf57..e14e87505 100644 --- a/app/views/demandes/show.html.haml +++ b/app/views/demandes/show.html.haml @@ -2,15 +2,14 @@ ='Type de demande' %br -= form_tag(url_for({controller: :demandes, action: :choice}), class: 'form-inline', method: 'POST') do - - %div{style:'text-align:center'} += form_tag(url_for({controller: :demandes, action: :update}), class: 'form-inline', method: 'POST') do + %div.center %select{name:'formulaire', id: 'formulaire', class:'form-control'} - -@evenement_vie.each do |ev| - %optgroup{:label => ev.nom} - - Formulaire.where(evenement_vie: ev.id, use_admi_facile: true).each do |form| - %option{value: form.id} + - @evenements_vie.each do |evenement_vie| + %optgroup{label: evenement_vie.nom} + - evenement_vie.formulaires.for_admi_facile.each do |formulaire| + %option{value: formulaire.id}    - =form.nom + =formulaire.nom %br - =render partial: 'layouts/etape_suivante' \ No newline at end of file + =render partial: 'layouts/etape_suivante' diff --git a/config/routes.rb b/config/routes.rb index 6ded2e1db..a54e0e2e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,7 +26,8 @@ Rails.application.routes.draw do resources :dossiers do get '/demande' => 'demandes#show' - post '/demande' => 'demandes#choice' + post '/demande' => 'demandes#update' + get '/carte/position' => 'carte#get_position' get '/carte' => 'carte#show' diff --git a/spec/controllers/demandes_controller_spec.rb b/spec/controllers/demandes_controller_spec.rb index e64cf07fd..767cb643a 100644 --- a/spec/controllers/demandes_controller_spec.rb +++ b/spec/controllers/demandes_controller_spec.rb @@ -14,8 +14,8 @@ RSpec.describe DemandesController, type: :controller do describe 'POST #choice' do context 'dans tous les cas on affiche la carte' do it { - post :choice, :dossier_id => dossier_id, :type_demande => '1' - expect(response).to redirect_to("/dossiers/#{dossier_id}/carte") + post :update, :dossier_id => dossier_id, :type_demande => '1' + expect(response).to redirect_to(controller: :carte, action: :show, dossier_id: dossier_id) } end end diff --git a/spec/models/formulaire_spec.rb b/spec/models/formulaire_spec.rb index ba54b2abd..589fc8fa5 100644 --- a/spec/models/formulaire_spec.rb +++ b/spec/models/formulaire_spec.rb @@ -23,4 +23,11 @@ describe Formulaire do it { is_expected.to have_db_column(:mail_pj) } it { is_expected.to have_db_column(:use_admi_facile) } end + + describe '.for_admi_facile' do + it 'retruns Formulaire where use_admi_facile is true' do + expect(Formulaire.for_admi_facile.size).to eq(Formulaire.where(use_admi_facile: true).count) + expect(Formulaire.for_admi_facile).to include(Formulaire.where(use_admi_facile: true).first) + end + end end \ No newline at end of file