From 480431ea013cd28d86de8cecdc23e0492c23d90e Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 8 Aug 2016 12:52:30 +0200 Subject: [PATCH] Add on gestionnaire dossier view the private form --- app/assets/stylesheets/application.scss | 2 +- .../backoffice/dossiers_controller.rb | 1 + .../private_formulaires_controller.rb | 19 +++++++ .../users/description_controller.rb | 20 ++------ app/facades/dossier_facades.rb | 4 ++ app/models/champ_private.rb | 3 ++ app/models/champ_public.rb | 3 ++ app/models/dossier.rb | 13 ++++- app/serializers/champ_private_serializer.rb | 5 ++ app/serializers/champ_public_serializer.rb | 5 ++ app/serializers/champ_serializer.rb | 5 -- app/serializers/dossier_serializer.rb | 1 + app/services/champs_service.rb | 25 ++++++++++ .../admin/types_de_champ/_fields.html.haml | 9 ++-- .../dossiers/formulaire_private.js.erb | 4 ++ app/views/backoffice/dossiers/show.html.haml | 11 +++- config/routes.rb | 1 + ...0804130638_add_type_attr_in_champ_table.rb | 25 ++++++++++ db/schema.rb | 3 +- .../api/v1/dossiers_controller_spec.rb | 35 ++++++++++++- .../private_formulaires_controller_spec.rb | 28 +++++++++++ spec/factories/dossier.rb | 2 +- spec/models/champ_private_spec.rb | 7 +++ spec/models/champ_public_spec.rb | 7 +++ spec/models/champ_shared_example.rb | 38 ++++++++++++++ spec/models/champ_spec.rb | 37 +------------- spec/models/dossier_spec.rb | 50 ++++++++++++++++++- .../show.html.haml_spec.rb | 45 ++++++++++------- 28 files changed, 320 insertions(+), 88 deletions(-) create mode 100644 app/controllers/backoffice/private_formulaires_controller.rb create mode 100644 app/models/champ_private.rb create mode 100644 app/models/champ_public.rb create mode 100644 app/serializers/champ_private_serializer.rb create mode 100644 app/serializers/champ_public_serializer.rb delete mode 100644 app/serializers/champ_serializer.rb create mode 100644 app/services/champs_service.rb create mode 100644 app/views/backoffice/dossiers/formulaire_private.js.erb create mode 100644 db/migrate/20160804130638_add_type_attr_in_champ_table.rb create mode 100644 spec/controllers/backoffice/private_formulaires_controller_spec.rb create mode 100644 spec/models/champ_private_spec.rb create mode 100644 spec/models/champ_public_spec.rb create mode 100644 spec/models/champ_shared_example.rb diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index dedae8dcd..dd36af17e 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -184,7 +184,7 @@ div.pagination { .alert.alert-success.move_up, .alert.alert-danger.siret{ - position: absolute; + position: fixed; top: 0px; left: 0; height: 52px; diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index 82d41b543..a927d94d1 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -14,6 +14,7 @@ class Backoffice::DossiersController < ApplicationController def show create_dossier_facade params[:id] + @champs = @facade.champs_private unless @facade.nil? end def search diff --git a/app/controllers/backoffice/private_formulaires_controller.rb b/app/controllers/backoffice/private_formulaires_controller.rb new file mode 100644 index 000000000..4ad2ff671 --- /dev/null +++ b/app/controllers/backoffice/private_formulaires_controller.rb @@ -0,0 +1,19 @@ +class Backoffice::PrivateFormulairesController < ApplicationController + before_action :authenticate_gestionnaire! + + def update + dossier = current_gestionnaire.dossiers.find(params[:dossier_id]) + + unless params[:champs].nil? + champs_service_errors = ChampsService.save_formulaire dossier.champs_private, params + + if champs_service_errors.empty? + flash[:notice] = "Formulaire enregistré" + else + flash[:alert] = (champs_service_errors.inject('') { |acc, error| acc+= error[:message]+'
' }).html_safe + end + end + + render 'backoffice/dossiers/formulaire_private', formats: :js + end +end \ No newline at end of file diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index b3956eaa6..080b96275 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -43,23 +43,11 @@ class Users::DescriptionController < UsersController end unless params[:champs].nil? - @dossier.champs.each do |champ| - champ.value = params[:champs]["'#{champ.id}'"] + champs_service_errors = ChampsService.save_formulaire @dossier.champs, params - if champ.type_champ == 'datetime' - champ.value = params[:champs]["'#{champ.id}'"]+ - ' ' + - params[:time_hour]["'#{champ.id}'"] + - ':' + - params[:time_minute]["'#{champ.id}'"] - end - - if champ.mandatory? && (champ.value.nil? || champ.value.blank?) - flash.now.alert = "Le champ #{champ.libelle} doit être rempli." - return render 'show' - end - - champ.save + unless champs_service_errors.empty? + flash.now.alert = (champs_service_errors.inject('') {|acc, error| acc+= error[:message]+'
' }).html_safe + return render 'show' end end diff --git a/app/facades/dossier_facades.rb b/app/facades/dossier_facades.rb index 2291d608f..059462848 100644 --- a/app/facades/dossier_facades.rb +++ b/app/facades/dossier_facades.rb @@ -46,6 +46,10 @@ class DossierFacades @dossier.invites end + def champs_private + @dossier.ordered_champs_private + end + def commentaires_files PieceJustificative.where(dossier_id: @dossier.id, type_de_piece_justificative_id: nil) end diff --git a/app/models/champ_private.rb b/app/models/champ_private.rb new file mode 100644 index 000000000..6f7068148 --- /dev/null +++ b/app/models/champ_private.rb @@ -0,0 +1,3 @@ +class ChampPrivate < Champ + +end diff --git a/app/models/champ_public.rb b/app/models/champ_public.rb new file mode 100644 index 000000000..7b0efcb3d --- /dev/null +++ b/app/models/champ_public.rb @@ -0,0 +1,3 @@ +class ChampPublic < Champ + +end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index f387d28d5..f5ab282d7 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -13,7 +13,8 @@ class Dossier < ActiveRecord::Base has_many :cerfa, dependent: :destroy has_many :pieces_justificatives, dependent: :destroy - has_many :champs, dependent: :destroy + has_many :champs, class_name: 'ChampPublic', dependent: :destroy + has_many :champs_private, class_name: 'ChampPrivate', dependent: :destroy has_many :quartier_prioritaires, dependent: :destroy has_many :cadastres, dependent: :destroy has_many :commentaires, dependent: :destroy @@ -47,7 +48,11 @@ class Dossier < ActiveRecord::Base def build_default_champs procedure.types_de_champ.each do |type_de_champ| - Champ.create(type_de_champ_id: type_de_champ.id, dossier_id: id) + ChampPublic.create(type_de_champ_id: type_de_champ.id, dossier_id: id) + end + + procedure.types_de_champ_private.each do |type_de_champ| + ChampPrivate.create(type_de_champ_id: type_de_champ.id, dossier_id: id) end end @@ -55,6 +60,10 @@ class Dossier < ActiveRecord::Base champs.joins(', types_de_champ').where("champs.type_de_champ_id = types_de_champ.id AND types_de_champ.procedure_id = #{procedure.id}").order('order_place') end + def ordered_champs_private + champs_private.joins(', types_de_champ').where("champs.type_de_champ_id = types_de_champ.id AND types_de_champ.procedure_id = #{procedure.id}").order('order_place') + end + def ordered_commentaires commentaires.order(created_at: :desc) end diff --git a/app/serializers/champ_private_serializer.rb b/app/serializers/champ_private_serializer.rb new file mode 100644 index 000000000..be0320f55 --- /dev/null +++ b/app/serializers/champ_private_serializer.rb @@ -0,0 +1,5 @@ +class ChampPrivateSerializer < ActiveModel::Serializer + attributes :value + + has_one :type_de_champ +end \ No newline at end of file diff --git a/app/serializers/champ_public_serializer.rb b/app/serializers/champ_public_serializer.rb new file mode 100644 index 000000000..528a4f850 --- /dev/null +++ b/app/serializers/champ_public_serializer.rb @@ -0,0 +1,5 @@ +class ChampPublicSerializer < ActiveModel::Serializer + attributes :value + + has_one :type_de_champ +end \ No newline at end of file diff --git a/app/serializers/champ_serializer.rb b/app/serializers/champ_serializer.rb deleted file mode 100644 index 5a0ba0bf3..000000000 --- a/app/serializers/champ_serializer.rb +++ /dev/null @@ -1,5 +0,0 @@ -class ChampSerializer < ActiveModel::Serializer - attributes :value - - has_one :type_de_champ -end \ No newline at end of file diff --git a/app/serializers/dossier_serializer.rb b/app/serializers/dossier_serializer.rb index 3364c8928..5060598ab 100644 --- a/app/serializers/dossier_serializer.rb +++ b/app/serializers/dossier_serializer.rb @@ -12,6 +12,7 @@ class DossierSerializer < ActiveModel::Serializer has_many :cerfa has_many :commentaires has_many :champs + has_many :champs_private has_many :pieces_justificatives has_many :types_de_piece_justificative end \ No newline at end of file diff --git a/app/services/champs_service.rb b/app/services/champs_service.rb new file mode 100644 index 000000000..7ae8817a7 --- /dev/null +++ b/app/services/champs_service.rb @@ -0,0 +1,25 @@ +class ChampsService + def self.save_formulaire champs, params + errors = Array.new + + champs.each do |champ| + champ.value = params[:champs]["'#{champ.id}'"] + + if champ.type_champ == 'datetime' + champ.value = params[:champs]["'#{champ.id}'"]+ + ' ' + + params[:time_hour]["'#{champ.id}'"] + + ':' + + params[:time_minute]["'#{champ.id}'"] + end + + if champ.mandatory? && (champ.value.nil? || champ.value.blank?) + errors.push({message: "Le champ #{champ.libelle} doit être rempli."}) + end + + champ.save + end + + errors + end +end \ No newline at end of file diff --git a/app/views/admin/types_de_champ/_fields.html.haml b/app/views/admin/types_de_champ/_fields.html.haml index 35046c4c1..8ef85b152 100644 --- a/app/views/admin/types_de_champ/_fields.html.haml +++ b/app/views/admin/types_de_champ/_fields.html.haml @@ -12,10 +12,11 @@ %h4 Description = ff.text_area :description, class: 'form-control description', placeholder: 'Description' - .form-group.mandatory - %h4 Obligatoire ? - .center - = ff.check_box :mandatory, placeholder: 'Obligatoire ?' + - unless ff.object.object.class == TypeDeChampPrivate + .form-group.mandatory + %h4 Obligatoire ? + .center + = ff.check_box :mandatory, placeholder: 'Obligatoire ?' .form-group = ff.hidden_field :order_place, value: ff.index diff --git a/app/views/backoffice/dossiers/formulaire_private.js.erb b/app/views/backoffice/dossiers/formulaire_private.js.erb new file mode 100644 index 000000000..a4a9f595b --- /dev/null +++ b/app/views/backoffice/dossiers/formulaire_private.js.erb @@ -0,0 +1,4 @@ +<% flash.each do |type, message| %> +$("#flash_message").html("
<%= message.html_safe %>
").children().fadeOut(5000) +<% end %> +<% flash.clear %> diff --git a/app/views/backoffice/dossiers/show.html.haml b/app/views/backoffice/dossiers/show.html.haml index caba91adb..ca83b7225 100644 --- a/app/views/backoffice/dossiers/show.html.haml +++ b/app/views/backoffice/dossiers/show.html.haml @@ -27,6 +27,9 @@ %li{role: "presentation"} %a{href: "#followers", 'aria-controls' => "followers", role: "tab", 'data-toggle' => "tab"} Abonnés + %li{role: "presentation"} + %a{href: "#champs_private", 'aria-controls' => "champs_private", role: "tab", 'data-toggle' => "tab"} + Formulaire %div{class: "tab-content"} %div{role: "tabpanel", class: "tab-pane fade in active", id:"commentaires"} @@ -39,6 +42,12 @@ = render partial: '/dossiers/invites' %div{role: "tabpanel", class: "tab-pane fade", id:"followers"} = render partial: 'followers' - + %div{role: "tabpanel", class: "tab-pane fade", id:"champs_private"} + %h3 Formulaire privé + - unless @champs.nil? + = form_for @facade.dossier, url: {controller: 'backoffice/private_formulaires', action: :update, dossier_id: @facade.dossier.id}, remote: true do + = render partial: '/users/description/champs' + %br + = submit_tag :Enregistrer, {class: 'btn btn-success', style: 'float: right'} %br %br \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index a60906714..66e600ab1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -145,6 +145,7 @@ Rails.application.routes.draw do get 'filtres' => 'procedure_filter#index' patch 'filtres/update' => 'procedure_filter#update' + resource :private_formulaire namespace :preference_list_dossier do post 'add' diff --git a/db/migrate/20160804130638_add_type_attr_in_champ_table.rb b/db/migrate/20160804130638_add_type_attr_in_champ_table.rb new file mode 100644 index 000000000..465d9ce63 --- /dev/null +++ b/db/migrate/20160804130638_add_type_attr_in_champ_table.rb @@ -0,0 +1,25 @@ +class AddTypeAttrInChampTable < ActiveRecord::Migration + + class TypeDeChamp < ActiveRecord::Base + has_many :champs + end + + class Champ < ActiveRecord::Base + belongs_to :type_de_champ + end + + def up + add_column :champs, :type, :string + + Champ.all.each do |champ| + type = 'ChampPublic' if champ.type_de_champ.class == TypeDeChampPublic + type = 'ChampPrivate' if champ.type_de_champ.class == TypeDeChampPrivate + + champ.update_attribute(:type, type) + end + end + + def down + remove_column :champs, :type + end +end diff --git a/db/schema.rb b/db/schema.rb index d022e46cd..b737a1d69 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160803081304) do +ActiveRecord::Schema.define(version: 20160804130638) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -99,6 +99,7 @@ ActiveRecord::Schema.define(version: 20160803081304) do t.string "value" t.integer "type_de_champ_id" t.integer "dossier_id" + t.string "type" end create_table "commentaires", force: :cascade do |t| diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index a2eeaeeca..5295b7d49 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe API::V1::DossiersController do let(:admin) { create(:administrateur) } - let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, administrateur: admin, cerfa_flag: true) } + let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_type_de_champ_private, administrateur: admin, cerfa_flag: true) } let(:wrong_procedure) { create(:procedure) } it { expect(described_class).to be < APIController } @@ -115,7 +115,7 @@ describe API::V1::DossiersController do let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, procedure: procedure) } } let(:dossier_id) { dossier.id } let(:body) { JSON.parse(retour.body, symbolize_names: true) } - let(:field_list) { [:id, :created_at, :updated_at, :archived, :mandataire_social, :total_commentaire, :entreprise, :etablissement, :cerfa, :types_de_piece_justificative, :pieces_justificatives, :champs, :commentaires, :state] } + let(:field_list) { [:id, :created_at, :updated_at, :archived, :mandataire_social, :total_commentaire, :entreprise, :etablissement, :cerfa, :types_de_piece_justificative, :pieces_justificatives, :champs, :champs_private, :commentaires, :state] } subject { body[:dossier] } it 'return REST code 200', :show_in_doc do @@ -235,6 +235,37 @@ describe API::V1::DossiersController do end end + describe 'champs_private' do + let(:field_list) { [ + :url] } + subject { super()[:champs_private] } + + it { expect(subject.length).to eq 1 } + + describe 'first champs' do + subject { super().first } + + it { expect(subject.keys.include?(:value)).to be_truthy } + it { expect(subject.keys.include?(:type_de_champ)).to be_truthy } + + describe 'type de champ' do + let(:field_list) { [ + :id, + :libelle, + :description, + :order_place, + :type] } + subject { super()[:type_de_champ] } + + it { expect(subject.keys.include?(:id)).to be_truthy } + it { expect(subject[:libelle]).to eq('Description') } + it { expect(subject[:description]).to eq('description de votre projet') } + it { expect(subject.keys.include?(:order_place)).to be_truthy } + it { expect(subject[:type_champ]).to eq('text') } + end + end + end + describe 'commentaires' do let!(:commentaire) { create :commentaire, body: 'plop', created_at: '2016-03-14 14:00:00', email: 'plop@plip.com', dossier: dossier } let!(:commentaire_2) { create :commentaire, body: 'plip', created_at: '2016-03-14 15:00:00', email: 'plip@plap.com', dossier: dossier } diff --git a/spec/controllers/backoffice/private_formulaires_controller_spec.rb b/spec/controllers/backoffice/private_formulaires_controller_spec.rb new file mode 100644 index 000000000..80b5bb345 --- /dev/null +++ b/spec/controllers/backoffice/private_formulaires_controller_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe Backoffice::PrivateFormulairesController, type: :controller do + let(:gestionnaire) { create :gestionnaire } + let(:dossier) { create :dossier } + let(:dossier_champs_first) { 'plop' } + + before do + create :assign_to, procedure_id: dossier.procedure.id, gestionnaire_id: gestionnaire.id + + sign_in gestionnaire + end + + describe '#PATCH update' do + subject { patch :update, dossier_id: dossier.id, + champs: { + "'#{dossier.champs_private.first.id}'" => dossier_champs_first + } } + + before do + subject + end + + it { expect(response.status).to eq 200 } + it { expect(Dossier.find(dossier.id).champs_private.first.value).to eq dossier_champs_first } + it { expect(flash[:notice]).to be_present } + end +end diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index e3f1e3490..2a98f2a0b 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -5,7 +5,7 @@ FactoryGirl.define do before(:create) do |dossier, _evaluator| unless dossier.procedure - procedure = create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ) + procedure = create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_type_de_champ_private) dossier.procedure = procedure end end diff --git a/spec/models/champ_private_spec.rb b/spec/models/champ_private_spec.rb new file mode 100644 index 000000000..635cf4fbf --- /dev/null +++ b/spec/models/champ_private_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe ChampPrivate do + require 'models/champ_shared_example.rb' + + it_should_behave_like "champ_spec" +end \ No newline at end of file diff --git a/spec/models/champ_public_spec.rb b/spec/models/champ_public_spec.rb new file mode 100644 index 000000000..b52c3e1f6 --- /dev/null +++ b/spec/models/champ_public_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe ChampPublic do + require 'models/champ_shared_example.rb' + + it_should_behave_like "champ_spec" +end \ No newline at end of file diff --git a/spec/models/champ_shared_example.rb b/spec/models/champ_shared_example.rb new file mode 100644 index 000000000..bf70d0012 --- /dev/null +++ b/spec/models/champ_shared_example.rb @@ -0,0 +1,38 @@ +shared_examples 'champ_spec' do + describe 'database columns' do + it { is_expected.to have_db_column(:value) } + end + + describe 'associations' do + it { is_expected.to belong_to(:dossier) } + it { is_expected.to belong_to(:type_de_champ) } + end + + describe 'delegation' do + it { is_expected.to delegate_method(:libelle).to(:type_de_champ) } + it { is_expected.to delegate_method(:type_champ).to(:type_de_champ) } + it { is_expected.to delegate_method(:order_place).to(:type_de_champ) } + end + + describe 'data_provide' do + let(:champ) { create :champ } + + subject { champ.data_provide } + + context 'when type_champ is datetime' do + before do + champ.type_de_champ = create :type_de_champ_public, type_champ: 'datetime' + end + + it { is_expected.to eq 'datepicker' } + end + + context 'when type_champ is address' do + before do + champ.type_de_champ = create :type_de_champ_public, type_champ: 'address' + end + + it { is_expected.to eq 'typeahead' } + end + end +end \ No newline at end of file diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index edd9bfa2e..66114be2d 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -1,40 +1,7 @@ require 'spec_helper' describe Champ do - describe 'database columns' do - it { is_expected.to have_db_column(:value) } - end + require 'models/champ_shared_example.rb' - describe 'associations' do - it { is_expected.to belong_to(:dossier) } - it { is_expected.to belong_to(:type_de_champ) } - end - - describe 'delegation' do - it { is_expected.to delegate_method(:libelle).to(:type_de_champ) } - it { is_expected.to delegate_method(:type_champ).to(:type_de_champ) } - it { is_expected.to delegate_method(:order_place).to(:type_de_champ) } - end - - describe 'data_provide' do - let(:champ) { create :champ } - - subject { champ.data_provide } - - context 'when type_champ is datetime' do - before do - champ.type_de_champ = create :type_de_champ_public, type_champ: 'datetime' - end - - it { is_expected.to eq 'datepicker' } - end - - context 'when type_champ is address' do - before do - champ.type_de_champ = create :type_de_champ_public, type_champ: 'address' - end - - it { is_expected.to eq 'typeahead' } - end - end + it_should_behave_like "champ_spec" end \ No newline at end of file diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index ebf8b2c9c..393968cd1 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -92,12 +92,16 @@ describe Dossier do end describe '#build_default_champs' do - context 'when dossier is linked to a procedure' do + context 'when dossier is linked to a procedure with type_de_champ_public and private' do let(:dossier) { create(:dossier, user: user) } it 'build all champs needed' do expect(dossier.champs.count).to eq(1) end + + it 'build all champs_private needed' do + expect(dossier.champs_private.count).to eq(1) + end end end @@ -604,7 +608,7 @@ describe Dossier do subject { dossier.ordered_champs } - it { expect(Champ.where(dossier_id: 0).size).to eq 5 } + it { expect(ChampPublic.where(dossier_id: 0).size).to eq 5 } describe 'for dossier 1' do let(:dossier) { dossier_1 } @@ -626,6 +630,48 @@ describe Dossier do end + describe '#ordered_champs_private' do + let!(:procedure_1) { create :procedure } + let!(:procedure_2) { create :procedure } + + let(:dossier_1) { Dossier.new(id: 0, procedure: procedure_1) } + let(:dossier_2) { Dossier.new(id: 0, procedure: procedure_2) } + + before do + create :type_de_champ_private, libelle: 'type_1_1', order_place: 1, procedure: dossier_1.procedure + create :type_de_champ_private, libelle: 'type_1_2', order_place: 2, procedure: dossier_1.procedure + + create :type_de_champ_private, libelle: 'type_2_1', order_place: 1, procedure: dossier_2.procedure + create :type_de_champ_private, libelle: 'type_2_2', order_place: 2, procedure: dossier_2.procedure + create :type_de_champ_private, libelle: 'type_2_3', order_place: 3, procedure: dossier_2.procedure + + dossier_1.build_default_champs + dossier_2.build_default_champs + end + + subject { dossier.ordered_champs_private } + + it { expect(ChampPrivate.where(dossier_id: 0).size).to eq 5 } + + describe 'for dossier 1' do + let(:dossier) { dossier_1 } + + it { expect(subject.size).to eq 2 } + it { expect(subject.first.type_de_champ.libelle).to eq 'type_1_1' } + it { expect(subject.last.type_de_champ.libelle).to eq 'type_1_2' } + end + + describe 'for dossier 2' do + let(:dossier) { dossier_2 } + + it { expect(subject.size).to eq 3 } + + it { expect(subject.first.type_de_champ.libelle).to eq 'type_2_1' } + it { expect(subject.second.type_de_champ.libelle).to eq 'type_2_2' } + it { expect(subject.last.type_de_champ.libelle).to eq 'type_2_3' } + end + end + describe '#total_follow' do let(:dossier) { create(:dossier, :with_entreprise, user: user) } let(:dossier2) { create(:dossier, :with_entreprise, user: user) } diff --git a/spec/views/admin/types_de_champ_private/show.html.haml_spec.rb b/spec/views/admin/types_de_champ_private/show.html.haml_spec.rb index 319e9c30a..829050748 100644 --- a/spec/views/admin/types_de_champ_private/show.html.haml_spec.rb +++ b/spec/views/admin/types_de_champ_private/show.html.haml_spec.rb @@ -19,7 +19,7 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do end end - describe 'arrow button' do + describe 'elements presents or not' do subject do procedure.reload assign(:procedure, procedure) @@ -27,24 +27,33 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do render rendered end - context 'when there is no field in database' do - it { expect(subject).not_to have_css('.fa-chevron-down') } - it { expect(subject).not_to have_css('.fa-chevron-up') } + + describe 'mandatory checkbox' do + it 'no mandatory checkbox are present' do + expect(subject).not_to have_css('.form-group.mandatory') + end end - context 'when there is only one field in database' do - let!(:type_de_champ_0) { create(:type_de_champ_private, procedure: procedure, order_place: 0) } - it { expect(subject).not_to have_css('#btn_down_0') } - it { expect(subject).not_to have_css('#btn_up_0') } - it { expect(subject).not_to have_css('#btn_up_1') } - it { expect(subject).not_to have_css('#btn_down_1') } - end - context 'when there are 2 fields in database' do - let!(:type_de_champ_0) { create(:type_de_champ_private, procedure: procedure, order_place: 0) } - let!(:type_de_champ_1) { create(:type_de_champ_private, procedure: procedure, order_place: 1) } - it { expect(subject).to have_css('#btn_down_0') } - it { expect(subject).not_to have_css('#btn_up_0') } - it { expect(subject).to have_css('#btn_up_1') } - it { expect(subject).not_to have_css('#btn_down_1') } + + describe 'arrow button' do + context 'when there is no field in database' do + it { expect(subject).not_to have_css('.fa-chevron-down') } + it { expect(subject).not_to have_css('.fa-chevron-up') } + end + context 'when there is only one field in database' do + let!(:type_de_champ_0) { create(:type_de_champ_private, procedure: procedure, order_place: 0) } + it { expect(subject).not_to have_css('#btn_down_0') } + it { expect(subject).not_to have_css('#btn_up_0') } + it { expect(subject).not_to have_css('#btn_up_1') } + it { expect(subject).not_to have_css('#btn_down_1') } + end + context 'when there are 2 fields in database' do + let!(:type_de_champ_0) { create(:type_de_champ_private, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ_private, procedure: procedure, order_place: 1) } + it { expect(subject).to have_css('#btn_down_0') } + it { expect(subject).not_to have_css('#btn_up_0') } + it { expect(subject).to have_css('#btn_up_1') } + it { expect(subject).not_to have_css('#btn_down_1') } + end end end end \ No newline at end of file