From 116dd0d94fada647c66bc2b55b366ee04d229145 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Wed, 3 Aug 2016 18:19:56 +0200 Subject: [PATCH] Administrateur can be define private champs => same as typical champs --- .../admin/types_de_champ_controller.rb | 2 +- .../types_de_champ_private_controller.rb | 44 ++++ app/decorators/type_de_champ_decorator.rb | 23 +- .../type_de_champ_private_decorator.rb | 3 + app/models/procedure.rb | 13 +- app/models/type_de_champ.rb | 2 - app/models/type_de_champ_private.rb | 6 - app/models/type_de_champ_public.rb | 3 + app/serializers/type_de_champ_serializer.rb | 3 +- app/views/admin/procedures/_navbar.html.haml | 4 + .../admin/types_de_champ/_fields.html.haml | 4 +- .../admin/types_de_champ/_form.html.haml | 2 +- .../types_de_champ_private/_fields.html.haml | 38 ++++ .../types_de_champ_private/_form.html.haml | 6 + .../types_de_champ_private/show.html.haml | 4 + .../admin/types_de_champ_private/show.js.erb | 5 + config/initializers/inflections.rb | 1 + config/routes.rb | 13 +- ...02161734_add_type_attr_on_type_de_champ.rb | 5 + ...fix_default_type_on_type_de_champ_table.rb | 17 ++ db/schema.rb | 4 +- .../admin/types_de_champ_controller_spec.rb | 18 +- .../types_de_champ_private_controller_spec.rb | 202 ++++++++++++++++++ .../api/v1/dossiers_controller_spec.rb | 2 +- .../type_de_champ_decorator_spec.rb | 6 +- spec/factories/procedure.rb | 14 +- spec/factories/type_de_champ_private.rb | 9 + ...pe_de_champ.rb => type_de_champ_public.rb} | 2 +- .../move_down_type_de_champ_private_spec.rb | 29 +++ .../admin/move_down_type_de_champ_spec.rb | 8 +- .../move_up_type_de_champ_private_spec.rb | 29 +++ .../admin/move_up_type_de_champ_spec.rb | 8 +- spec/models/champ_spec.rb | 4 +- spec/models/dossier_spec.rb | 10 +- spec/models/procedure_spec.rb | 12 +- spec/models/type_de_champ_private_spec.rb | 7 + spec/models/type_de_champ_public_spec.rb | 7 + spec/models/type_de_champ_shared_example.rb | 44 ++++ spec/models/type_de_champ_spec.rb | 43 +--- .../types_de_champ/show.html.haml_spec.rb | 10 +- .../show.html.haml_spec.rb | 48 +++++ 41 files changed, 601 insertions(+), 113 deletions(-) create mode 100644 app/controllers/admin/types_de_champ_private_controller.rb create mode 100644 app/decorators/type_de_champ_private_decorator.rb create mode 100644 app/models/type_de_champ_public.rb create mode 100644 app/views/admin/types_de_champ_private/_fields.html.haml create mode 100644 app/views/admin/types_de_champ_private/_form.html.haml create mode 100644 app/views/admin/types_de_champ_private/show.html.haml create mode 100644 app/views/admin/types_de_champ_private/show.js.erb create mode 100644 db/migrate/20160802161734_add_type_attr_on_type_de_champ.rb create mode 100644 db/migrate/20160803081304_fix_default_type_on_type_de_champ_table.rb create mode 100644 spec/controllers/admin/types_de_champ_private_controller_spec.rb create mode 100644 spec/factories/type_de_champ_private.rb rename spec/factories/{type_de_champ.rb => type_de_champ_public.rb} (82%) create mode 100644 spec/features/admin/move_down_type_de_champ_private_spec.rb create mode 100644 spec/features/admin/move_up_type_de_champ_private_spec.rb create mode 100644 spec/models/type_de_champ_private_spec.rb create mode 100644 spec/models/type_de_champ_public_spec.rb create mode 100644 spec/models/type_de_champ_shared_example.rb create mode 100644 spec/views/admin/types_de_champ_private/show.html.haml_spec.rb diff --git a/app/controllers/admin/types_de_champ_controller.rb b/app/controllers/admin/types_de_champ_controller.rb index 6aa974d74..8eab26c02 100644 --- a/app/controllers/admin/types_de_champ_controller.rb +++ b/app/controllers/admin/types_de_champ_controller.rb @@ -21,7 +21,7 @@ class Admin::TypesDeChampController < AdminController def update_params params .require(:procedure) - .permit(types_de_champ_attributes: [:libelle, :description, :order_place, :type_champ, :id, :mandatory]) + .permit(types_de_champ_attributes: [:libelle, :description, :order_place, :type_champ, :id, :mandatory, :type]) end def move_up diff --git a/app/controllers/admin/types_de_champ_private_controller.rb b/app/controllers/admin/types_de_champ_private_controller.rb new file mode 100644 index 000000000..d8f1af66c --- /dev/null +++ b/app/controllers/admin/types_de_champ_private_controller.rb @@ -0,0 +1,44 @@ +class Admin::TypesDeChampPrivateController < AdminController + before_action :retrieve_procedure + before_action :procedure_locked? + + def destroy + @procedure.types_de_champ_private.destroy(params[:id]) + render 'show', format: :js + rescue ActiveRecord::RecordNotFound + render json: { message: 'Champ not found' }, status: 404 + end + + def show + + end + + def update + @procedure.update_attributes(update_params) + flash.now.notice = 'Modifications sauvegardées' + render 'show', format: :js + end + + def update_params + params + .require(:procedure) + .permit(types_de_champ_private_attributes: [:libelle, :description, :order_place, :type_champ, :id, :mandatory, :type]) + end + + def move_up + index = params[:index].to_i - 1 + if @procedure.switch_types_de_champ_private index + render 'show', format: :js + else + render json: {}, status: 400 + end + end + + def move_down + if @procedure.switch_types_de_champ_private params[:index].to_i + render 'show', format: :js + else + render json: {}, status: 400 + end + end +end \ No newline at end of file diff --git a/app/decorators/type_de_champ_decorator.rb b/app/decorators/type_de_champ_decorator.rb index 6e92f6647..d149996a7 100644 --- a/app/decorators/type_de_champ_decorator.rb +++ b/app/decorators/type_de_champ_decorator.rb @@ -1,12 +1,13 @@ - class TypeDeChampDecorator < Draper::Decorator + delegate_all + def button_up params - h.link_to '', params[:url], class: up_classes, id: "btn_up_#{params[:index]}", remote: true, method: :post if display_up_button?(params[:index]) + h.link_to '', params[:url], class: up_classes, id: "btn_up_#{params[:index]}", remote: true, method: :post if display_up_button?(params[:index], params[:private]) end def button_down params - h.link_to '', params[:url], class: down_classes, id: "btn_down_#{params[:index]}", remote: true, method: :post if display_down_button?(params[:index]) + h.link_to '', params[:url], class: down_classes, id: "btn_down_#{params[:index]}", remote: true, method: :post if display_down_button?(params[:index], params[:private]) end private @@ -23,15 +24,19 @@ class TypeDeChampDecorator < Draper::Decorator %w(btn btn-default form-control fa) end - def display_up_button?(index) - !(index == 0 || count_type_de_champ < 2) + def display_up_button?(index, private) + !(index == 0 || count_type_de_champ(private) < 2) end - def display_down_button?(index) - (index + 1) < count_type_de_champ + def display_down_button?(index, private) + (index + 1) < count_type_de_champ(private) end - def count_type_de_champ - @count_type_de_champ ||= procedure.types_de_champ.count + def count_type_de_champ private + if private + @count_type_de_champ ||= procedure.types_de_champ_private.count + else + @count_type_de_champ ||= procedure.types_de_champ.count + end end end \ No newline at end of file diff --git a/app/decorators/type_de_champ_private_decorator.rb b/app/decorators/type_de_champ_private_decorator.rb new file mode 100644 index 000000000..1adcaca84 --- /dev/null +++ b/app/decorators/type_de_champ_private_decorator.rb @@ -0,0 +1,3 @@ +class TypeDeChampPrivateDecorator < TypeDeChampDecorator + +end \ No newline at end of file diff --git a/app/models/procedure.rb b/app/models/procedure.rb index c09adbe39..73f950cef 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -1,7 +1,7 @@ class Procedure < ActiveRecord::Base has_many :types_de_piece_justificative, dependent: :destroy - has_many :types_de_champ, dependent: :destroy - has_many :types_de_champ_private, class_name: 'TypeDeChampPrivate', dependent: :destroy + has_many :types_de_champ, class_name: 'TypeDeChampPublic', dependent: :destroy + has_many :types_de_champ_private, dependent: :destroy has_many :dossiers has_one :procedure_path, dependent: :destroy @@ -18,6 +18,7 @@ class Procedure < ActiveRecord::Base accepts_nested_attributes_for :types_de_champ,:reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true accepts_nested_attributes_for :types_de_piece_justificative, :reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true accepts_nested_attributes_for :module_api_carto + accepts_nested_attributes_for :types_de_champ_private mount_uploader :logo, ProcedureLogoUploader @@ -36,6 +37,10 @@ class Procedure < ActiveRecord::Base types_de_champ.order(:order_place) end + def types_de_champ_private_ordered + types_de_champ_private.order(:order_place) + end + def types_de_piece_justificative_ordered types_de_piece_justificative.order(:order_place) end @@ -52,6 +57,10 @@ class Procedure < ActiveRecord::Base switch_list_order(types_de_champ_ordered, index_of_first_element) end + def switch_types_de_champ_private index_of_first_element + switch_list_order(types_de_champ_private_ordered, index_of_first_element) + end + def switch_types_de_piece_justificative index_of_first_element switch_list_order(types_de_piece_justificative_ordered, index_of_first_element) end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 6a738957f..e4df31ea0 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -18,8 +18,6 @@ class TypeDeChamp < ActiveRecord::Base has_many :champ, dependent: :destroy - default_scope { where(private: false) } - validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :type_champ, presence: true, allow_blank: false, allow_nil: false # validates :order_place, presence: true, allow_blank: false, allow_nil: false diff --git a/app/models/type_de_champ_private.rb b/app/models/type_de_champ_private.rb index c9acf24e2..804bb2e99 100644 --- a/app/models/type_de_champ_private.rb +++ b/app/models/type_de_champ_private.rb @@ -1,9 +1,3 @@ class TypeDeChampPrivate < TypeDeChamp - after_initialize :force_private_value - default_scope { where(private: true) } - - def force_private_value - self.private = true - end end \ No newline at end of file diff --git a/app/models/type_de_champ_public.rb b/app/models/type_de_champ_public.rb new file mode 100644 index 000000000..90052b762 --- /dev/null +++ b/app/models/type_de_champ_public.rb @@ -0,0 +1,3 @@ +class TypeDeChampPublic < TypeDeChamp + +end \ No newline at end of file diff --git a/app/serializers/type_de_champ_serializer.rb b/app/serializers/type_de_champ_serializer.rb index ebf66c0b8..bbe2cb7dc 100644 --- a/app/serializers/type_de_champ_serializer.rb +++ b/app/serializers/type_de_champ_serializer.rb @@ -3,6 +3,5 @@ class TypeDeChampSerializer < ActiveModel::Serializer :libelle, {:type_champ => :type}, :order_place, - :description, - :private + :description end \ No newline at end of file diff --git a/app/views/admin/procedures/_navbar.html.haml b/app/views/admin/procedures/_navbar.html.haml index 25198c9b0..6a382894a 100644 --- a/app/views/admin/procedures/_navbar.html.haml +++ b/app/views/admin/procedures/_navbar.html.haml @@ -16,5 +16,9 @@ = link_to_unless(@procedure.locked?, 'Pièces justificatives', admin_procedure_pieces_justificatives_path(@procedure)) do = link_to('Pièces justificatives', '#') +%li{ class: ('disabled' if @procedure.locked?) || ('active' if active == 'Champs privés') } + = link_to_unless(@procedure.locked?, 'Champs privés', admin_procedure_types_de_champ_private_path(@procedure)) do + = link_to('Champs privés', '#') + %li{ class: ('active' if active == 'Prévisualisation'), style: 'float:right' } = link_to('Prévisualisation', admin_procedure_previsualisation_path(@procedure), {style: 'font-style: italic;'}) \ 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 4103a2329..dd0250fbd 100644 --- a/app/views/admin/types_de_champ/_fields.html.haml +++ b/app/views/admin/types_de_champ/_fields.html.haml @@ -6,7 +6,7 @@ .form-group.type %h4 Type - = ff.select :type_champ, TypeDeChamp.type_champs, {}, {class: 'form-control type_champ'} + = ff.select :type_champ, TypeDeChampPublic.type_champs, {}, {class: 'form-control type_champ'} .form-group.description %h4 Description @@ -19,6 +19,7 @@ .form-group = ff.hidden_field :order_place, value: ff.index + = ff.hidden_field :type = ff.hidden_field :id - unless ff.object.id.nil? .form-group @@ -26,7 +27,6 @@ = ff.object.button_up(index: ff.index, url: move_up_admin_procedure_types_de_champ_path(@procedure, ff.index)) = ff.object.button_down(index: ff.index, url: move_down_admin_procedure_types_de_champ_path(@procedure, ff.index)) - .form-group %br   - if ff.object.id.nil? diff --git a/app/views/admin/types_de_champ/_form.html.haml b/app/views/admin/types_de_champ/_form.html.haml index 2b2ce0b37..d92771855 100644 --- a/app/views/admin/types_de_champ/_form.html.haml +++ b/app/views/admin/types_de_champ/_form.html.haml @@ -3,4 +3,4 @@ = f.submit "Enregistrer", class: 'btn btn-success', id: :save %hr #new_type_de_champ - = render partial: 'fields', locals: { types_de_champ: TypeDeChamp.new.decorate, f: f } + = render partial: 'fields', locals: { types_de_champ: TypeDeChampPublic.new.decorate, f: f } diff --git a/app/views/admin/types_de_champ_private/_fields.html.haml b/app/views/admin/types_de_champ_private/_fields.html.haml new file mode 100644 index 000000000..6da37a941 --- /dev/null +++ b/app/views/admin/types_de_champ_private/_fields.html.haml @@ -0,0 +1,38 @@ += f.fields_for :types_de_champ_private, types_de_champ_private, remote: true do |ff| + .form-inline{class:"#{ff.object.object.type_champ == 'header_section' ? 'header_section' : ''}"} + .form-group.libelle + %h4 Libellé + = ff.text_field :libelle, class: 'form-control libelle', placeholder: 'Libellé' + + .form-group.type + %h4 Type + = ff.select :type_champ, TypeDeChampPrivate.type_champs, {}, {class: 'form-control type_champ'} + + .form-group.description + %h4 Description + = ff.text_area :description, class: 'form-control description', placeholder: 'Description' + + .form-group.mandatory + %h4 Obligatoire ? + .center + = ff.check_box :mandatory, placeholder: 'Obligatoire ?' + + .form-group + = ff.hidden_field :order_place, value: ff.index + = ff.hidden_field :type + = ff.hidden_field :id + + - unless ff.object.id.nil? + .form-group + %br   + = ff.object.button_up(index: ff.index, url: move_up_admin_procedure_types_de_champ_private_path(@procedure, ff.index), private: true) + = ff.object.button_down(index: ff.index, url: move_down_admin_procedure_types_de_champ_private_path(@procedure, ff.index), private: true) + + .form-group + %br   + - if ff.object.id.nil? + = f.submit "Ajouter le champ", class: 'btn btn-success', id: :add_type_de_champ_private + - else + = link_to("", admin_procedure_type_de_champ_private_path(@procedure, ff.object.id), method: :delete, remote: true, id: "delete_type_de_champ_private_#{ff.object.id}", class: %w(form-control btn btn-danger fa fa-trash-o) ) + + diff --git a/app/views/admin/types_de_champ_private/_form.html.haml b/app/views/admin/types_de_champ_private/_form.html.haml new file mode 100644 index 000000000..a098de1f8 --- /dev/null +++ b/app/views/admin/types_de_champ_private/_form.html.haml @@ -0,0 +1,6 @@ += form_for [:admin, @procedure], url: admin_procedure_types_de_champ_private_path(@procedure) , remote: true do |f| + = render partial: 'fields', locals: { types_de_champ_private: @procedure.types_de_champ_private_ordered.decorate, f: f } + = f.submit "Enregistrer", class: 'btn btn-success', id: :save + %hr + #new_type_de_champ + = render partial: 'fields', locals: { types_de_champ_private: TypeDeChampPrivate.new.decorate, f: f } diff --git a/app/views/admin/types_de_champ_private/show.html.haml b/app/views/admin/types_de_champ_private/show.html.haml new file mode 100644 index 000000000..464f9083d --- /dev/null +++ b/app/views/admin/types_de_champ_private/show.html.haml @@ -0,0 +1,4 @@ +=render partial: 'admin/procedures/head', locals: {active: 'Champs privés'} + +#liste_champ + = render partial: 'form' \ No newline at end of file diff --git a/app/views/admin/types_de_champ_private/show.js.erb b/app/views/admin/types_de_champ_private/show.js.erb new file mode 100644 index 000000000..259d609e3 --- /dev/null +++ b/app/views/admin/types_de_champ_private/show.js.erb @@ -0,0 +1,5 @@ +<% flash.each do |type, message| %> +$("#flash_message").html("
<%= message.html_safe %>
").children().fadeOut(5000) +<% end %> +$('#liste_champ').html("<%= escape_javascript(render partial: 'form', locals: { procedure: @procedure, types_de_champ: @types_de_champ } ) %>"); +on_change_type_de_champ_select (); \ No newline at end of file diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 1e5e84144..940e78aad 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -13,6 +13,7 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.irregular 'piece_justificative', 'pieces_justificatives' inflect.irregular 'type_de_piece_justificative', 'types_de_piece_justificative' inflect.irregular 'type_de_champ', 'types_de_champ' + inflect.irregular 'type_de_champ_private', 'types_de_champ_private' inflect.irregular 'assign_to', 'assign_tos' end diff --git a/config/routes.rb b/config/routes.rb index cb11c81c3..a60906714 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -93,10 +93,20 @@ Rails.application.routes.draw do get 'profile' => 'profile#show', as: :profile resources :procedures do + resources :types_de_champ, only: [:destroy] resource :types_de_champ, only: [:show, :update] do post '/:index/move_up' => 'types_de_champ#move_up', as: :move_up post '/:index/move_down' => 'types_de_champ#move_down', as: :move_down end + + resources :types_de_champ_private, only: [:destroy] + resource :types_de_champ_private, only: [:show, :update] do + post '/:index/move_up' => 'types_de_champ_private#move_up', as: :move_up + post '/:index/move_down' => 'types_de_champ_private#move_down', as: :move_down + end + + resource :pieces_justificatives, only: [:show, :update] + resources :pieces_justificatives, only: :destroy resource :pieces_justificatives, only: [:show, :update] do post '/:index/move_up' => 'pieces_justificatives#move_up', as: :move_up post '/:index/move_down' => 'pieces_justificatives#move_down', as: :move_down @@ -111,9 +121,6 @@ Rails.application.routes.draw do resource :previsualisation, only: [:show] - resources :types_de_champ, only: [:destroy] - resource :pieces_justificatives, only: [:show, :update] - resources :pieces_justificatives, only: :destroy end namespace :accompagnateurs do diff --git a/db/migrate/20160802161734_add_type_attr_on_type_de_champ.rb b/db/migrate/20160802161734_add_type_attr_on_type_de_champ.rb new file mode 100644 index 000000000..d1ab25589 --- /dev/null +++ b/db/migrate/20160802161734_add_type_attr_on_type_de_champ.rb @@ -0,0 +1,5 @@ +class AddTypeAttrOnTypeDeChamp < ActiveRecord::Migration + def change + add_column :types_de_champ, :type, :string + end +end diff --git a/db/migrate/20160803081304_fix_default_type_on_type_de_champ_table.rb b/db/migrate/20160803081304_fix_default_type_on_type_de_champ_table.rb new file mode 100644 index 000000000..b5aec3a3a --- /dev/null +++ b/db/migrate/20160803081304_fix_default_type_on_type_de_champ_table.rb @@ -0,0 +1,17 @@ +class FixDefaultTypeOnTypeDeChampTable < ActiveRecord::Migration + class TypeDeChamp < ActiveRecord::Base + + end + + def up + TypeDeChamp.where("private = false").update_all("type = 'TypeDeChampPublic'") + TypeDeChamp.where("private = true").update_all("type = 'TypeDeChampPrivate'") + remove_column :types_de_champ, :private + end + + def down + add_column :types_de_champ, :private, :boolean, default: true + TypeDeChamp.where("type = 'TypeDeChampPublic'").update_all("private = false") + TypeDeChamp.where("type = 'TypeDeChampPrivate'").update_all("private = true") + end +end diff --git a/db/schema.rb b/db/schema.rb index 65a008f87..d022e46cd 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: 20160802131031) do +ActiveRecord::Schema.define(version: 20160803081304) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -296,7 +296,7 @@ ActiveRecord::Schema.define(version: 20160802131031) do t.integer "procedure_id" t.text "description" t.boolean "mandatory", default: false - t.boolean "private", default: false + t.string "type" end create_table "types_de_piece_justificative", force: :cascade do |t| diff --git a/spec/controllers/admin/types_de_champ_controller_spec.rb b/spec/controllers/admin/types_de_champ_controller_spec.rb index c334ccb0c..68203e16f 100644 --- a/spec/controllers/admin/types_de_champ_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_controller_spec.rb @@ -121,7 +121,7 @@ describe Admin::TypesDeChampController, type: :controller do end end context 'when procedure and type de champs are not linked' do - let(:type_de_champ) { create(:type_de_champ) } + let(:type_de_champ) { create(:type_de_champ_public) } let(:type_de_champ_id) { type_de_champ.id } it { expect(subject.status).to eq(404) } end @@ -136,20 +136,20 @@ describe Admin::TypesDeChampController, type: :controller do end context 'when procedure have only one type de champ' do let(:index) { 1 } - let!(:type_de_champ) { create(:type_de_champ, procedure: procedure) } + let!(:type_de_champ) { create(:type_de_champ_public, procedure: procedure) } it { expect(subject.status).to eq(400) } end context 'when procedure have tow type de champs' do context 'when index == 0' do let(:index) { 0 } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure) } - let!(:type_de_champ_2) { create(:type_de_champ, procedure: procedure) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure) } + let!(:type_de_champ_2) { create(:type_de_champ_public, procedure: procedure) } it { expect(subject.status).to eq(400) } end context 'when index > 0' do let(:index) { 1 } - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 1) } it { expect(subject.status).to eq(200) } it { expect(subject).to render_template('show') } @@ -174,12 +174,12 @@ describe Admin::TypesDeChampController, type: :controller do it { expect(subject.status).to eq(400) } end context 'when procedure have only one type de champ' do - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure) } it { expect(subject.status).to eq(400) } end context 'when procedure have 2 type de champ' do - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 1) } context 'when index represent last type_de_champ' do let(:index) { 1 } it { expect(subject.status).to eq(400) } diff --git a/spec/controllers/admin/types_de_champ_private_controller_spec.rb b/spec/controllers/admin/types_de_champ_private_controller_spec.rb new file mode 100644 index 000000000..2db709955 --- /dev/null +++ b/spec/controllers/admin/types_de_champ_private_controller_spec.rb @@ -0,0 +1,202 @@ +require 'spec_helper' + +describe Admin::TypesDeChampPrivateController, type: :controller do + let(:admin) { create(:administrateur) } + let(:procedure) { create(:procedure, administrateur: admin) } + + before do + sign_in admin + end + + describe 'GET #show' do + let(:published) { false } + let(:procedure) { create(:procedure, administrateur: admin, published: published) } + let(:procedure_id) { procedure.id } + + subject { get :show, procedure_id: procedure_id } + + context 'when procedure is not found' do + let(:procedure_id) { 9_999_999 } + it { expect(subject.status).to eq(404) } + end + + context 'when procedure is published' do + let(:published) { true } + it { is_expected.to redirect_to admin_procedure_path id: procedure_id } + end + + context 'when procedure does not belong to admin' do + let(:admin_2) { create(:administrateur) } + let(:procedure) { create(:procedure, administrateur: admin_2) } + it { expect(subject.status).to eq(404) } + end + end + + describe '#update' do + let(:libelle) { 'mon libelle' } + let(:type_champ) { 'text' } + let(:description) { 'titi' } + let(:order_place) { '' } + let(:types_de_champ_id) { '' } + let(:mandatory) { 'on' } + + let(:procedure_params) do + { types_de_champ_private_attributes: + { '0' => + { + libelle: libelle, + type_champ: type_champ, + description: description, + order_place: order_place, + id: types_de_champ_id, + mandatory: mandatory, + type: 'TypeDeChampPrivate' + } + } + } + end + + let(:request) { put :update, format: :js, procedure_id: procedure.id, procedure: procedure_params } + + context 'when procedure is found' do + it { expect{ request }.to change(TypeDeChamp, :count).by(1) } + + describe 'created type de champ' do + before do + request + procedure.reload + end + subject { procedure.types_de_champ_private.first } + + it { expect(subject.libelle).to eq('mon libelle') } + it { expect(subject.type_champ).to eq('text') } + it { expect(subject.description).to eq('titi') } + it { expect(subject.mandatory).to be_truthy } + end + + context 'when type_de_champ already exist' do + let(:procedure) { create(:procedure, :with_type_de_champ_private, administrateur: admin) } + let(:type_de_champ) { procedure.types_de_champ_private.first } + let(:types_de_champ_id) { type_de_champ.id } + let(:libelle) { 'toto' } + let(:type_champ) { 'text' } + let(:description) { 'citrouille' } + let(:order_place) { '0' } + let(:mandatory) { 'on' } + before do + request + procedure.reload + end + subject { procedure.types_de_champ_private.first } + it { expect(subject.libelle).to eq('toto') } + it { expect(subject.type_champ).to eq('text') } + it { expect(subject.description).to eq('citrouille') } + it { expect(subject.order_place).to eq(0) } + it { expect(subject.order_place).to be_truthy } + end + end + context 'when procedure is not found' do + subject { put :update, format: :js, procedure_id: 9_999_999, procedure: procedure_params } + it 'creates type de champ' do + expect(subject.status).to eq(404) + end + end + end + + describe '#destroy' do + before do + delete :destroy, procedure_id: procedure.id, id: type_de_champ_id, format: :js + end + + context 'when type de champs does not exist' do + let(:type_de_champ_id) { 99999999 } + it { expect(subject.status).to eq(404) } + end + context 'when types_de_champ exists' do + let(:procedure) { create(:procedure, :with_type_de_champ_private, administrateur: admin) } + let(:type_de_champ_id) { procedure.types_de_champ_private.first.id } + it { expect(subject.status).to eq(200) } + it 'destroy type de champ' do + procedure.reload + expect(procedure.types_de_champ.count).to eq(0) + end + end + context 'when procedure and type de champs are not linked' do + let(:type_de_champ) { create(:type_de_champ_public) } + let(:type_de_champ_id) { type_de_champ.id } + it { expect(subject.status).to eq(404) } + end + end + + describe 'POST #move_up' do + subject { post :move_up, procedure_id: procedure.id, index: index, format: :js } + + context 'when procedure have no type de champ' do + let(:index) { 0 } + it { expect(subject.status).to eq(400) } + end + context 'when procedure have only one type de champ' do + let(:index) { 1 } + let!(:type_de_champ) { create(:type_de_champ_private, procedure: procedure) } + it { expect(subject.status).to eq(400) } + end + context 'when procedure have tow type de champs' do + context 'when index == 0' do + let(:index) { 0 } + let!(:type_de_champ_1) { create(:type_de_champ_private, procedure: procedure) } + let!(:type_de_champ_2) { create(:type_de_champ_private, procedure: procedure) } + it { expect(subject.status).to eq(400) } + end + context 'when index > 0' do + let(:index) { 1 } + 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.status).to eq(200) } + it { expect(subject).to render_template('show') } + it 'changes order places' do + post :move_up, procedure_id: procedure.id, index: index, format: :js + type_de_champ_0.reload + type_de_champ_1.reload + expect(type_de_champ_0.order_place).to eq(1) + expect(type_de_champ_1.order_place).to eq(0) + end + end + end + end + + describe 'POST #move_down' do + let(:request) { post :move_down, procedure_id: procedure.id, index: index, format: :js } + let(:index) { 0 } + + subject { request } + + context 'when procedure have no type de champ' do + it { expect(subject.status).to eq(400) } + end + context 'when procedure have only one type de champ' do + let!(:type_de_champ_0) { create(:type_de_champ_private, procedure: procedure) } + it { expect(subject.status).to eq(400) } + end + context 'when procedure have 2 type de champ' 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) } + context 'when index represent last type_de_champ' do + let(:index) { 1 } + it { expect(subject.status).to eq(400) } + end + context 'when index does not represent last type_de_champ' do + let(:index) { 0 } + it { expect(subject.status).to eq(200) } + it { expect(subject).to render_template('show') } + it 'changes order place' do + request + type_de_champ_0.reload + type_de_champ_1.reload + expect(type_de_champ_0.order_place).to eq(1) + expect(type_de_champ_1.order_place).to eq(0) + end + end + end + end +end diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index 6ff404ec4..a2eeaeeca 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -230,7 +230,7 @@ describe API::V1::DossiersController do 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]).to eq('text') } + it { expect(subject[:type_champ]).to eq('text') } end end end diff --git a/spec/decorators/type_de_champ_decorator_spec.rb b/spec/decorators/type_de_champ_decorator_spec.rb index 61a2bc73f..7ab455e00 100644 --- a/spec/decorators/type_de_champ_decorator_spec.rb +++ b/spec/decorators/type_de_champ_decorator_spec.rb @@ -5,9 +5,9 @@ describe TypeDeChampDecorator do let(:procedure) { create(:procedure) } let(:url) { 'http://localhost' } let(:params) { { url: url, index: index } } - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } - let!(:type_de_champ_2) { create(:type_de_champ, procedure: procedure, order_place: 2) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 1) } + let!(:type_de_champ_2) { create(:type_de_champ_public, procedure: procedure, order_place: 2) } describe '#button_up' do diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index d8d5dc8c1..11cd238b1 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -24,15 +24,23 @@ FactoryGirl.define do trait :with_type_de_champ do after(:build) do |procedure, _evaluator| - type_de_champ = create(:type_de_champ) + type_de_champ = create(:type_de_champ_public) procedure.types_de_champ << type_de_champ end end + trait :with_type_de_champ_private do + after(:build) do |procedure, _evaluator| + type_de_champ = create(:type_de_champ_private) + + procedure.types_de_champ_private << type_de_champ + end + end + trait :with_type_de_champ_mandatory do after(:build) do |procedure, _evaluator| - type_de_champ = create(:type_de_champ, mandatory: true) + type_de_champ = create(:type_de_champ_public, mandatory: true) procedure.types_de_champ << type_de_champ end @@ -40,7 +48,7 @@ FactoryGirl.define do trait :with_datetime do after(:build) do |procedure, _evaluator| - type_de_champ = create(:type_de_champ, mandatory: true, type_champ: :datetime) + type_de_champ = create(:type_de_champ_public, mandatory: true, type_champ: :datetime) procedure.types_de_champ << type_de_champ end diff --git a/spec/factories/type_de_champ_private.rb b/spec/factories/type_de_champ_private.rb new file mode 100644 index 000000000..408279d05 --- /dev/null +++ b/spec/factories/type_de_champ_private.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :type_de_champ_private do + libelle 'Description' + description 'description de votre projet' + type_champ 'text' + order_place 1 + mandatory false + end +end diff --git a/spec/factories/type_de_champ.rb b/spec/factories/type_de_champ_public.rb similarity index 82% rename from spec/factories/type_de_champ.rb rename to spec/factories/type_de_champ_public.rb index 8e348a254..937921d0d 100644 --- a/spec/factories/type_de_champ.rb +++ b/spec/factories/type_de_champ_public.rb @@ -1,5 +1,5 @@ FactoryGirl.define do - factory :type_de_champ do + factory :type_de_champ_public do libelle 'Description' description 'description de votre projet' type_champ 'text' diff --git a/spec/features/admin/move_down_type_de_champ_private_spec.rb b/spec/features/admin/move_down_type_de_champ_private_spec.rb new file mode 100644 index 000000000..b811617a0 --- /dev/null +++ b/spec/features/admin/move_down_type_de_champ_private_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +feature 'move down button type de champs', js: true do + let(:administrateur) { create(:administrateur) } + + before do + login_as administrateur, scope: :administrateur + end + + let(:procedure) { create(:procedure, administrateur: administrateur) } + 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) } + let!(:type_de_champ_2) { create(:type_de_champ_private, procedure: procedure, order_place: 2) } + let!(:type_de_champ_3) { create(:type_de_champ_private, procedure: procedure, order_place: 3) } + + context 'when clicking on move down for type de champ 1' do + before do + visit admin_procedure_types_de_champ_private_path procedure.id + page.find_by_id('btn_down_1').click + wait_for_ajax + type_de_champ_1.reload + type_de_champ_2.reload + end + scenario 'it switches type_de_champ 1 and 2 place ' do + expect(type_de_champ_1.order_place).to eq(2) + expect(type_de_champ_2.order_place).to eq(1) + end + end +end diff --git a/spec/features/admin/move_down_type_de_champ_spec.rb b/spec/features/admin/move_down_type_de_champ_spec.rb index 84f291caf..0e9243cf9 100644 --- a/spec/features/admin/move_down_type_de_champ_spec.rb +++ b/spec/features/admin/move_down_type_de_champ_spec.rb @@ -8,10 +8,10 @@ feature 'move down button type de champs', js: true do end let(:procedure) { create(:procedure, administrateur: administrateur) } - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } - let!(:type_de_champ_2) { create(:type_de_champ, procedure: procedure, order_place: 2) } - let!(:type_de_champ_3) { create(:type_de_champ, procedure: procedure, order_place: 3) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 1) } + let!(:type_de_champ_2) { create(:type_de_champ_public, procedure: procedure, order_place: 2) } + let!(:type_de_champ_3) { create(:type_de_champ_public, procedure: procedure, order_place: 3) } context 'when clicking on move down for type de champ 1' do before do diff --git a/spec/features/admin/move_up_type_de_champ_private_spec.rb b/spec/features/admin/move_up_type_de_champ_private_spec.rb new file mode 100644 index 000000000..377fcea61 --- /dev/null +++ b/spec/features/admin/move_up_type_de_champ_private_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +feature 'move up button type de champs', js: true do + let(:administrateur) { create(:administrateur) } + + before do + login_as administrateur, scope: :administrateur + end + + let(:procedure) { create(:procedure, administrateur: administrateur) } + 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) } + let!(:type_de_champ_2) { create(:type_de_champ_private, procedure: procedure, order_place: 2) } + let!(:type_de_champ_3) { create(:type_de_champ_private, procedure: procedure, order_place: 3) } + + context 'when clicking on move down for type de champ 1' do + before do + visit admin_procedure_types_de_champ_private_path procedure.id + page.find_by_id('btn_up_1').click + wait_for_ajax + type_de_champ_0.reload + type_de_champ_1.reload + end + scenario 'it switches type_de_champ 1 and 2 place ' do + expect(type_de_champ_0.order_place).to eq(1) + expect(type_de_champ_1.order_place).to eq(0) + end + end +end diff --git a/spec/features/admin/move_up_type_de_champ_spec.rb b/spec/features/admin/move_up_type_de_champ_spec.rb index 4f0679509..28c0ab9f5 100644 --- a/spec/features/admin/move_up_type_de_champ_spec.rb +++ b/spec/features/admin/move_up_type_de_champ_spec.rb @@ -8,10 +8,10 @@ feature 'move up button type de champs', js: true do end let(:procedure) { create(:procedure, administrateur: administrateur) } - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } - let!(:type_de_champ_2) { create(:type_de_champ, procedure: procedure, order_place: 2) } - let!(:type_de_champ_3) { create(:type_de_champ, procedure: procedure, order_place: 3) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 1) } + let!(:type_de_champ_2) { create(:type_de_champ_public, procedure: procedure, order_place: 2) } + let!(:type_de_champ_3) { create(:type_de_champ_public, procedure: procedure, order_place: 3) } context 'when clicking on move down for type de champ 1' do before do diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index 0e2cd61a8..edd9bfa2e 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -23,7 +23,7 @@ describe Champ do context 'when type_champ is datetime' do before do - champ.type_de_champ = create :type_de_champ, type_champ: 'datetime' + champ.type_de_champ = create :type_de_champ_public, type_champ: 'datetime' end it { is_expected.to eq 'datepicker' } @@ -31,7 +31,7 @@ describe Champ do context 'when type_champ is address' do before do - champ.type_de_champ = create :type_de_champ, type_champ: 'address' + champ.type_de_champ = create :type_de_champ_public, type_champ: 'address' end it { is_expected.to eq 'typeahead' } diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 3a789effc..ebf8b2c9c 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -591,12 +591,12 @@ describe Dossier do let(:dossier_2) { Dossier.new(id: 0, procedure: procedure_2) } before do - create :type_de_champ, libelle: 'type_1_1', order_place: 1, procedure: dossier_1.procedure - create :type_de_champ, libelle: 'type_1_2', order_place: 2, procedure: dossier_1.procedure + create :type_de_champ_public, libelle: 'type_1_1', order_place: 1, procedure: dossier_1.procedure + create :type_de_champ_public, libelle: 'type_1_2', order_place: 2, procedure: dossier_1.procedure - create :type_de_champ, libelle: 'type_2_1', order_place: 1, procedure: dossier_2.procedure - create :type_de_champ, libelle: 'type_2_2', order_place: 2, procedure: dossier_2.procedure - create :type_de_champ, libelle: 'type_2_3', order_place: 3, procedure: dossier_2.procedure + create :type_de_champ_public, libelle: 'type_2_1', order_place: 1, procedure: dossier_2.procedure + create :type_de_champ_public, libelle: 'type_2_2', order_place: 2, procedure: dossier_2.procedure + create :type_de_champ_public, libelle: 'type_2_3', order_place: 3, procedure: dossier_2.procedure dossier_1.build_default_champs dossier_2.build_default_champs diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 1724ed8d2..6e4f6fdbb 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -44,8 +44,8 @@ describe Procedure do describe '#types_de_champ_ordered' do let(:procedure) { create(:procedure) } - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 1) } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 0) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 1) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } subject { procedure.types_de_champ_ordered } it { expect(subject.first).to eq(type_de_champ_1) } it { expect(subject.last).to eq(type_de_champ_0) } @@ -60,8 +60,8 @@ describe Procedure do it { expect(subject).to eq(false) } end context 'when procedure have 2 types de champ' do - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 1) } context 'when index is not the last element' do it { expect(subject).to eq(true) } it 'switch order place' do @@ -128,8 +128,8 @@ describe Procedure do let(:archived) { false } let(:published) { false } let(:procedure) { create(:procedure, archived: archived, published: published) } - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 1) } let!(:piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } let!(:piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) } subject { procedure.clone } diff --git a/spec/models/type_de_champ_private_spec.rb b/spec/models/type_de_champ_private_spec.rb new file mode 100644 index 000000000..6f0b1ce8a --- /dev/null +++ b/spec/models/type_de_champ_private_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe TypeDeChampPrivate do + require 'models/type_de_champ_shared_example' + + it_should_behave_like "type_de_champ_spec" +end diff --git a/spec/models/type_de_champ_public_spec.rb b/spec/models/type_de_champ_public_spec.rb new file mode 100644 index 000000000..fc855566f --- /dev/null +++ b/spec/models/type_de_champ_public_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe TypeDeChampPublic do + require 'models/type_de_champ_shared_example' + + it_should_behave_like "type_de_champ_spec" +end diff --git a/spec/models/type_de_champ_shared_example.rb b/spec/models/type_de_champ_shared_example.rb new file mode 100644 index 000000000..5ce94d7d3 --- /dev/null +++ b/spec/models/type_de_champ_shared_example.rb @@ -0,0 +1,44 @@ +shared_examples 'type_de_champ_spec' do + describe 'database columns' do + it { is_expected.to have_db_column(:libelle) } + it { is_expected.to have_db_column(:type_champ) } + it { is_expected.to have_db_column(:order_place) } + it { is_expected.to have_db_column(:description) } + end + + describe 'associations' do + it { is_expected.to belong_to(:procedure) } + it { is_expected.to have_many(:champ) } + end + + describe 'validation' do + context 'libelle' do + it { is_expected.not_to allow_value(nil).for(:libelle) } + it { is_expected.not_to allow_value('').for(:libelle) } + it { is_expected.to allow_value('Montant projet').for(:libelle) } + end + + context 'type' do + it { is_expected.not_to allow_value(nil).for(:type_champ) } + it { is_expected.not_to allow_value('').for(:type_champ) } + + it { is_expected.to allow_value('text').for(:type_champ) } + it { is_expected.to allow_value('textarea').for(:type_champ) } + it { is_expected.to allow_value('datetime').for(:type_champ) } + it { is_expected.to allow_value('number').for(:type_champ) } + it { is_expected.to allow_value('checkbox').for(:type_champ) } + end + + context 'order_place' do + # it { is_expected.not_to allow_value(nil).for(:order_place) } + # it { is_expected.not_to allow_value('').for(:order_place) } + it { is_expected.to allow_value(1).for(:order_place) } + end + + context 'description' do + it { is_expected.to allow_value(nil).for(:description) } + it { is_expected.to allow_value('').for(:description) } + it { is_expected.to allow_value('blabla').for(:description) } + end + end +end \ No newline at end of file diff --git a/spec/models/type_de_champ_spec.rb b/spec/models/type_de_champ_spec.rb index 101b2be02..9649c4078 100644 --- a/spec/models/type_de_champ_spec.rb +++ b/spec/models/type_de_champ_spec.rb @@ -1,46 +1,9 @@ require 'spec_helper' describe TypeDeChamp do - describe 'database columns' do - it { is_expected.to have_db_column(:libelle) } - it { is_expected.to have_db_column(:type_champ) } - it { is_expected.to have_db_column(:order_place) } - it { is_expected.to have_db_column(:description) } - end - describe 'associations' do - it { is_expected.to belong_to(:procedure) } - it { is_expected.to have_many(:champ) } - end + require 'models/type_de_champ_shared_example' - describe 'validation' do - context 'libelle' do - it { is_expected.not_to allow_value(nil).for(:libelle) } - it { is_expected.not_to allow_value('').for(:libelle) } - it { is_expected.to allow_value('Montant projet').for(:libelle) } - end - - context 'type' do - it { is_expected.not_to allow_value(nil).for(:type_champ) } - it { is_expected.not_to allow_value('').for(:type_champ) } - - it { is_expected.to allow_value('text').for(:type_champ) } - it { is_expected.to allow_value('textarea').for(:type_champ) } - it { is_expected.to allow_value('datetime').for(:type_champ) } - it { is_expected.to allow_value('number').for(:type_champ) } - it { is_expected.to allow_value('checkbox').for(:type_champ) } - end - - context 'order_place' do - # it { is_expected.not_to allow_value(nil).for(:order_place) } - # it { is_expected.not_to allow_value('').for(:order_place) } - it { is_expected.to allow_value(1).for(:order_place) } - end - - context 'description' do - it { is_expected.to allow_value(nil).for(:description) } - it { is_expected.to allow_value('').for(:description) } - it { is_expected.to allow_value('blabla').for(:description) } - end - end + it_should_behave_like "type_de_champ_spec" end + diff --git a/spec/views/admin/types_de_champ/show.html.haml_spec.rb b/spec/views/admin/types_de_champ/show.html.haml_spec.rb index d6b7d1355..4029d7e98 100644 --- a/spec/views/admin/types_de_champ/show.html.haml_spec.rb +++ b/spec/views/admin/types_de_champ/show.html.haml_spec.rb @@ -6,8 +6,8 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do describe 'fields sorted' do let(:first_libelle) { 'salut la compagnie' } let(:last_libelle) { 'je suis bien sur la page' } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1, libelle: last_libelle) } - let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0, libelle: first_libelle) } + let!(:type_de_champ_1) { create(:type_de_champ_public, procedure: procedure, order_place: 1, libelle: last_libelle) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0, libelle: first_libelle) } before do procedure.reload assign(:procedure, procedure) @@ -30,15 +30,15 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do 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, procedure: procedure, order_place: 0) } + let!(:type_de_champ_0) { create(:type_de_champ_public, 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, procedure: procedure, order_place: 0) } - let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } + let!(:type_de_champ_0) { create(:type_de_champ_public, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ_public, 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') } 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 new file mode 100644 index 000000000..e3515a0df --- /dev/null +++ b/spec/views/admin/types_de_champ_private/show.html.haml_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +describe 'admin/types_de_champ_private/show.html.haml', type: :view do + let(:procedure) { create(:procedure) } + + describe 'fields sorted' do + let(:first_libelle) { 'salut la compagnie' } + let(:last_libelle) { 'je suis bien sur la page' } + let!(:type_de_champ_1) { create(:type_de_champ_private, procedure: procedure, order_place: 1, libelle: last_libelle) } + let!(:type_de_champ_0) { create(:type_de_champ_private, procedure: procedure, order_place: 0, libelle: first_libelle) } + before do + procedure.reload + assign(:procedure, procedure) + render + end + it 'sorts by order place' do + expect(rendered).to match(/#{first_libelle}.*#{last_libelle}/m) + end + end + + describe 'arrow button' do + subject do + procedure.reload + assign(:procedure, procedure) + 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') } + 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 \ No newline at end of file