From 85e7af840a2fbc7bfaf4bafc06662c0e9451ed85 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 9 Aug 2016 16:21:39 +0200 Subject: [PATCH] Add drop down list on type champ list --- app/assets/javascripts/admin.js | 18 ++++++---- app/assets/javascripts/application.js | 2 -- .../stylesheets/admin_type_de_champ.scss | 9 +++++ app/assets/stylesheets/description.scss | 6 ++++ .../admin/types_de_champ_controller.rb | 10 ++---- .../types_de_champ_private_controller.rb | 12 ++----- app/models/champ.rb | 2 +- app/models/drop_down_list.rb | 7 ++++ app/models/type_de_champ.rb | 5 +++ app/services/types_de_champ_service.rb | 18 ++++++++++ .../admin/types_de_champ/_fields.html.haml | 7 +++- app/views/users/description/_champs.html.haml | 3 ++ .../champs/_drop_down_list.html.haml | 13 +++++++ ...60809083606_create_drop_down_list_table.rb | 8 +++++ db/schema.rb | 7 +++- .../admin/types_de_champ_controller_spec.rb | 34 ++++++++++++------- .../types_de_champ_private_controller_spec.rb | 12 ++++++- 17 files changed, 132 insertions(+), 41 deletions(-) create mode 100644 app/models/drop_down_list.rb create mode 100644 app/services/types_de_champ_service.rb create mode 100644 app/views/users/description/champs/_drop_down_list.html.haml create mode 100644 db/migrate/20160809083606_create_drop_down_list_table.rb diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js index 8cf2e6467..571e93b65 100644 --- a/app/assets/javascripts/admin.js +++ b/app/assets/javascripts/admin.js @@ -26,13 +26,19 @@ function on_change_type_de_champ_select (){ $("select.form-control.type_champ").on('change', function(e){ - parent = $(this).parent().parent() + parent = $(this).parent().parent(); + + parent.removeClass('header_section'); + parent.children(".drop_down_list").removeClass('show_inline'); + + switch(this.value){ + case 'header_section': + parent.addClass('header_section'); + break; + case 'drop_down_list': + parent.children(".drop_down_list").addClass('show_inline'); + break; - if (this.value === 'header_section') { - parent.addClass('header_section') - } - else { - parent.removeClass('header_section') } }) } \ No newline at end of file diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 288d060c0..79ba48052 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -44,5 +44,3 @@ function scroll_to() { }); } - - diff --git a/app/assets/stylesheets/admin_type_de_champ.scss b/app/assets/stylesheets/admin_type_de_champ.scss index d8b9d1a7b..71747f829 100644 --- a/app/assets/stylesheets/admin_type_de_champ.scss +++ b/app/assets/stylesheets/admin_type_de_champ.scss @@ -14,5 +14,14 @@ .form-group.mandatory { display: none; } +} +#liste_champ{ + .show_inline { + display: inline-block !important; + } + + .form-group.drop_down_list{ + display: none; + } } \ No newline at end of file diff --git a/app/assets/stylesheets/description.scss b/app/assets/stylesheets/description.scss index 6c0845c46..1c6129833 100644 --- a/app/assets/stylesheets/description.scss +++ b/app/assets/stylesheets/description.scss @@ -57,6 +57,12 @@ } } +.type_champ-drop_down_list { + @extend .col-md-4; + @extend .col-lg-4; + +} + .type_champ-civilite { @extend .col-md-3; @extend .col-lg-3; diff --git a/app/controllers/admin/types_de_champ_controller.rb b/app/controllers/admin/types_de_champ_controller.rb index d28fccc87..ee3205901 100644 --- a/app/controllers/admin/types_de_champ_controller.rb +++ b/app/controllers/admin/types_de_champ_controller.rb @@ -7,7 +7,7 @@ class Admin::TypesDeChampController < AdminController create_facade render 'show', format: :js rescue ActiveRecord::RecordNotFound - render json: { message: 'Champ not found' }, status: 404 + render json: {message: 'Champ not found'}, status: 404 end def show @@ -15,18 +15,12 @@ class Admin::TypesDeChampController < AdminController end def update - @procedure.update_attributes(update_params) + @procedure.update_attributes(TypesDeChampService.create_update_procedure_params params) create_facade flash.now.notice = 'Modifications sauvegardées' render 'show', format: :js end - def update_params - params - .require(:procedure) - .permit(types_de_champ_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 index diff --git a/app/controllers/admin/types_de_champ_private_controller.rb b/app/controllers/admin/types_de_champ_private_controller.rb index fce48700a..25252b88c 100644 --- a/app/controllers/admin/types_de_champ_private_controller.rb +++ b/app/controllers/admin/types_de_champ_private_controller.rb @@ -7,27 +7,21 @@ class Admin::TypesDeChampPrivateController < AdminController create_facade render 'admin/types_de_champ/show', format: :js rescue ActiveRecord::RecordNotFound - render json: { message: 'Champ not found' }, status: 404 + render json: {message: 'Champ not found'}, status: 404 end def show - create_facade + create_facade render 'admin/types_de_champ/show' end def update - @procedure.update_attributes(update_params) + @procedure.update_attributes(TypesDeChampService.create_update_procedure_params params, true) create_facade flash.now.notice = 'Modifications sauvegardées' render 'admin/types_de_champ/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 diff --git a/app/models/champ.rb b/app/models/champ.rb index 7b6c357b9..ca8e1b531 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -2,7 +2,7 @@ class Champ < ActiveRecord::Base belongs_to :dossier belongs_to :type_de_champ - delegate :libelle, :type_champ, :order_place, :mandatory, :description, to: :type_de_champ + delegate :libelle, :type_champ, :order_place, :mandatory, :description, :drop_down_list, to: :type_de_champ def mandatory? mandatory diff --git a/app/models/drop_down_list.rb b/app/models/drop_down_list.rb new file mode 100644 index 000000000..d6e2d3d02 --- /dev/null +++ b/app/models/drop_down_list.rb @@ -0,0 +1,7 @@ +class DropDownList < ActiveRecord::Base + belongs_to :type_de_champ + + def options + value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?) + end +end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index e4df31ea0..4292aa8c7 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -11,12 +11,17 @@ class TypeDeChamp < ActiveRecord::Base phone: 'phone', address: 'address', yes_no: 'yes_no', + drop_down_list: 'drop_down_list', header_section: 'header_section' } belongs_to :procedure has_many :champ, dependent: :destroy + has_one :drop_down_list + + accepts_nested_attributes_for :drop_down_list + validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :type_champ, presence: true, allow_blank: false, allow_nil: false diff --git a/app/services/types_de_champ_service.rb b/app/services/types_de_champ_service.rb new file mode 100644 index 000000000..8a6c7a066 --- /dev/null +++ b/app/services/types_de_champ_service.rb @@ -0,0 +1,18 @@ +class TypesDeChampService + def self.create_update_procedure_params(params, private=false) + attributes = (private ? 'types_de_champ_private_attributes' : 'types_de_champ_attributes') + + parameters = params + .require(:procedure) + .permit("#{attributes}" => [:libelle, :description, :order_place, :type_champ, :id, :mandatory, :type, + drop_down_list_attributes: [:value, :id]]) + + parameters[attributes].each do |param| + if param.second[:libelle].empty? + parameters[attributes].delete(param.first.to_s) + end + end + + parameters + 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 8ef85b152..3b9c5c6f9 100644 --- a/app/views/admin/types_de_champ/_fields.html.haml +++ b/app/views/admin/types_de_champ/_fields.html.haml @@ -10,8 +10,13 @@ .form-group.description %h4 Description - = ff.text_area :description, class: 'form-control description', placeholder: 'Description' + = ff.text_area :description, class: 'form-control description', placeholder: 'Description', rows: 2 + .form-group.drop_down_list{class:"#{ff.object.object.type_champ == 'drop_down_list' ? 'show_inline' : ''}",style:'margin-right: 5px'} + %h4 Liste déroulante + = ff.fields_for :drop_down_list_attributes, ff.object.object.drop_down_list do |fff| + = fff.text_area :value, class: 'form-control drop_down_list', placeholder: "Ecrire une valeur par ligne.\nEcrire --valeur-- pour un séparateur.", rows: 3, cols: 30 + = fff.hidden_field :id - unless ff.object.object.class == TypeDeChampPrivate .form-group.mandatory %h4 Obligatoire ? diff --git a/app/views/users/description/_champs.html.haml b/app/views/users/description/_champs.html.haml index 3d6d5387f..cb78dbd3b 100644 --- a/app/views/users/description/_champs.html.haml +++ b/app/views/users/description/_champs.html.haml @@ -30,6 +30,9 @@ - elsif champ.type_champ == 'yes_no' =render partial: 'users/description/champs/yes_no', locals: {champ: champ} + - elsif champ.type_champ == 'drop_down_list' + =render partial: 'users/description/champs/drop_down_list', locals: {champ: champ} + -else %input.form-control{name:"champs['#{champ.id}']", placeholder: champ.libelle, diff --git a/app/views/users/description/champs/_drop_down_list.html.haml b/app/views/users/description/champs/_drop_down_list.html.haml new file mode 100644 index 000000000..bee4e6609 --- /dev/null +++ b/app/views/users/description/champs/_drop_down_list.html.haml @@ -0,0 +1,13 @@ +%select{ name:"champs['#{champ.id}']", + id: "champs_#{champ.id}" } + - champ.drop_down_list.options.each do |option| + - if (option=~ /^--.*--$/).nil? + - if champ.value == option + %option{selected:''} + = option + - else + %option + = option + -else + %option{disabled:''} + = option diff --git a/db/migrate/20160809083606_create_drop_down_list_table.rb b/db/migrate/20160809083606_create_drop_down_list_table.rb new file mode 100644 index 000000000..505d65067 --- /dev/null +++ b/db/migrate/20160809083606_create_drop_down_list_table.rb @@ -0,0 +1,8 @@ +class CreateDropDownListTable < ActiveRecord::Migration + def change + create_table :drop_down_lists do |t| + t.string :value + t.belongs_to :type_de_champ + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c2ca0cd47..86f0bac18 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: 20160808115924) do +ActiveRecord::Schema.define(version: 20160809083606) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -130,6 +130,11 @@ ActiveRecord::Schema.define(version: 20160808115924) do add_index "dossiers", ["procedure_id"], name: "index_dossiers_on_procedure_id", using: :btree add_index "dossiers", ["user_id"], name: "index_dossiers_on_user_id", using: :btree + create_table "drop_down_lists", force: :cascade do |t| + t.string "value" + t.integer "type_de_champ_id" + end + create_table "entreprises", force: :cascade do |t| t.string "siren" t.integer "capital_social" diff --git a/spec/controllers/admin/types_de_champ_controller_spec.rb b/spec/controllers/admin/types_de_champ_controller_spec.rb index 68203e16f..493dd64a4 100644 --- a/spec/controllers/admin/types_de_champ_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_controller_spec.rb @@ -41,24 +41,34 @@ describe Admin::TypesDeChampController, type: :controller do let(:mandatory) { 'on' } let(:procedure_params) do - { types_de_champ_attributes: - { '0' => - { - libelle: libelle, - type_champ: type_champ, - description: description, - order_place: order_place, - id: types_de_champ_id, - mandatory: mandatory - } - } + {types_de_champ_attributes: + {'0' => + { + libelle: libelle, + type_champ: type_champ, + description: description, + order_place: order_place, + id: types_de_champ_id, + mandatory: mandatory + }, + '1' => + { + libelle: '', + type_champ: 'text', + description: '', + order_place: '1', + id: '', + mandatory: false, + type: 'TypeDeChampPublic' + } + } } 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) } + it { expect { request }.to change(TypeDeChamp, :count).by(1) } describe 'created type de champ' do before do diff --git a/spec/controllers/admin/types_de_champ_private_controller_spec.rb b/spec/controllers/admin/types_de_champ_private_controller_spec.rb index 2db709955..635fca004 100644 --- a/spec/controllers/admin/types_de_champ_private_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_private_controller_spec.rb @@ -51,7 +51,17 @@ describe Admin::TypesDeChampPrivateController, type: :controller do id: types_de_champ_id, mandatory: mandatory, type: 'TypeDeChampPrivate' - } + }, + '1' => + { + libelle: '', + type_champ: 'text', + description: '', + order_place: '1', + id: '', + mandatory: false, + type: 'TypeDeChampPrivate' + } } } end