From 9466442aff4989413dfae3d730a6915d7e0dcf6e Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Thu, 27 Dec 2018 14:22:04 +0100 Subject: [PATCH] Remove the TypeDeChamp decorators --- app/decorators/type_de_champ_decorator.rb | 49 ----------------- .../type_de_piece_justificative_decorator.rb | 40 -------------- app/helpers/admin_formulaire_helper.rb | 55 +++++++++++++++++++ app/services/types_de_champ_service.rb | 4 +- .../pieces_justificatives/_fields.html.haml | 4 +- .../pieces_justificatives/_form.html.haml | 4 +- app/views/admin/procedures/_list.html.haml | 2 +- .../admin/types_de_champ/_fields.html.haml | 13 +++-- .../type_de_champ_decorator_spec.rb | 52 ------------------ .../admin_formulaire_helper.rb} | 49 ++++++++++------- .../types_de_champ/show.html.haml_spec.rb | 8 +-- .../show.html.haml_spec.rb | 8 +-- 12 files changed, 107 insertions(+), 181 deletions(-) delete mode 100644 app/decorators/type_de_champ_decorator.rb delete mode 100644 app/decorators/type_de_piece_justificative_decorator.rb create mode 100644 app/helpers/admin_formulaire_helper.rb delete mode 100644 spec/decorators/type_de_champ_decorator_spec.rb rename spec/{decorators/type_de_piece_justificative_decorator_spec.rb => helpers/admin_formulaire_helper.rb} (51%) diff --git a/app/decorators/type_de_champ_decorator.rb b/app/decorators/type_de_champ_decorator.rb deleted file mode 100644 index 8e3bc8118..000000000 --- a/app/decorators/type_de_champ_decorator.rb +++ /dev/null @@ -1,49 +0,0 @@ -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, - style: display_up_button?(params[:index], params[:private]) ? '' : 'visibility: hidden;' - end - - def button_down(params) - h.link_to '', params[:url], class: down_classes, - id: "btn_down_#{params[:index]}", - remote: true, - method: :post, - style: display_down_button?(params[:index], params[:private]) ? '' : 'visibility: hidden;' - end - - private - - def up_classes - base_classes << 'fa-chevron-up' - end - - def down_classes - base_classes << 'fa-chevron-down' - end - - def base_classes - ['btn', 'btn-default', 'form-control', 'fa'] - end - - def display_up_button?(index, private) - !(index == 0 || count_type_de_champ(private) < 2) - end - - def display_down_button?(index, private) - (index + 1) < count_type_de_champ(private) - end - - 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 diff --git a/app/decorators/type_de_piece_justificative_decorator.rb b/app/decorators/type_de_piece_justificative_decorator.rb deleted file mode 100644 index 906e61864..000000000 --- a/app/decorators/type_de_piece_justificative_decorator.rb +++ /dev/null @@ -1,40 +0,0 @@ -class TypeDePieceJustificativeDecorator < Draper::Decorator - delegate_all - def button_up(params) - if display_up_button?(params[:index]) - h.link_to '', params[:url], class: up_classes, id: "btn_up_#{params[:index]}", remote: true, method: :post - end - end - - def button_down(params) - if display_down_button?(params[:index]) - h.link_to '', params[:url], class: down_classes, id: "btn_down_#{params[:index]}", remote: true, method: :post - end - end - - private - - def up_classes - base_classes << 'fa-chevron-up' - end - - def down_classes - base_classes << 'fa-chevron-down' - end - - def base_classes - ['btn', 'btn-default', 'form-control', 'fa'] - end - - def display_up_button?(index) - !(index == 0 || count_type_de_piece_justificative < 2) - end - - def display_down_button?(index) - (index + 1) < count_type_de_piece_justificative - end - - def count_type_de_piece_justificative - @count_type_de_piece_justificative ||= procedure.types_de_piece_justificative.count - end -end diff --git a/app/helpers/admin_formulaire_helper.rb b/app/helpers/admin_formulaire_helper.rb new file mode 100644 index 000000000..1b0dd9a53 --- /dev/null +++ b/app/helpers/admin_formulaire_helper.rb @@ -0,0 +1,55 @@ +module AdminFormulaireHelper + BASE_CLASSES = ['btn', 'btn-default', 'form-control', 'fa'] + + def button_up(procedure, kind, index, url) + if display_up_button?(index, procedure, kind) + button(up_classes, "btn_up_#{index}", url) + end + end + + def button_down(procedure, kind, index, url) + if display_down_button?(index, procedure, kind) + button(down_classes, "btn_down_#{index}", url) + end + end + + private + + def button(classes, id, url) + link_to( + '', + url, + class: classes, + id: id, + remote: true, + method: :post + ) + end + + def up_classes + BASE_CLASSES + ['fa-chevron-up'] + end + + def down_classes + BASE_CLASSES + ['fa-chevron-down'] + end + + def display_up_button?(index, procedure, kind) + index != 0 && count_type_de_champ(procedure, kind) > 1 + end + + def display_down_button?(index, procedure, kind) + (index + 1) < count_type_de_champ(procedure, kind) + end + + def count_type_de_champ(procedure, kind) + case kind + when "public" + @count_type_de_champ_public ||= procedure.types_de_champ.count + when "private" + @count_type_de_champ_private ||= procedure.types_de_champ_private.count + when "piece_justificative" + @count_type_de_piece_justificative ||= procedure.types_de_piece_justificative.count + end + end +end diff --git a/app/services/types_de_champ_service.rb b/app/services/types_de_champ_service.rb index afc531349..288e30f59 100644 --- a/app/services/types_de_champ_service.rb +++ b/app/services/types_de_champ_service.rb @@ -36,11 +36,11 @@ class TypesDeChampService end def types_de_champ - private? ? @procedure.types_de_champ_private.decorate : @procedure.types_de_champ.decorate + private? ? @procedure.types_de_champ_private : @procedure.types_de_champ end def new_type_de_champ - TypeDeChamp.new(private: private?).decorate + TypeDeChamp.new(private: private?) end def fields_for_var diff --git a/app/views/admin/pieces_justificatives/_fields.html.haml b/app/views/admin/pieces_justificatives/_fields.html.haml index 27294bbf2..8343e84af 100644 --- a/app/views/admin/pieces_justificatives/_fields.html.haml +++ b/app/views/admin/pieces_justificatives/_fields.html.haml @@ -20,8 +20,8 @@ - if ff.object.id.present? .form-group %br   - = ff.object.button_up(index: ff.index, url: move_up_admin_procedure_pieces_justificatives_path(@procedure, ff.index)) - = ff.object.button_down(index: ff.index, url: move_down_admin_procedure_pieces_justificatives_path(@procedure, ff.index)) + = button_up(@procedure, "piece_justificative", ff.index, move_up_admin_procedure_pieces_justificatives_path(@procedure, ff.index)) + = button_down(@procedure, "piece_justificative", ff.index, move_down_admin_procedure_pieces_justificatives_path(@procedure, ff.index)) .form-group %h4 Obligatoire ? diff --git a/app/views/admin/pieces_justificatives/_form.html.haml b/app/views/admin/pieces_justificatives/_form.html.haml index 2499b7cad..e48f4077d 100644 --- a/app/views/admin/pieces_justificatives/_form.html.haml +++ b/app/views/admin/pieces_justificatives/_form.html.haml @@ -1,7 +1,7 @@ = form_for [:admin, @procedure], url: admin_procedure_pieces_justificatives_path(@procedure), remote: true do |f| #liste_piece_justificative - = render partial: 'fields', locals: { types_de_piece_justificative: @procedure.types_de_piece_justificative.decorate, f: f } + = render partial: 'fields', locals: { types_de_piece_justificative: @procedure.types_de_piece_justificative, f: f } = f.submit "Enregistrer", class: 'btn btn-success', id: :save %hr #new_type_de_piece_justificative - = render partial: 'fields', locals: { types_de_piece_justificative: TypeDePieceJustificative.new.decorate, f: f } + = render partial: 'fields', locals: { types_de_piece_justificative: TypeDePieceJustificative.new, f: f } diff --git a/app/views/admin/procedures/_list.html.haml b/app/views/admin/procedures/_list.html.haml index 9c5ce616a..8fcb04aee 100644 --- a/app/views/admin/procedures/_list.html.haml +++ b/app/views/admin/procedures/_list.html.haml @@ -20,7 +20,7 @@ - if procedure.publiee? %td.procedure-lien= link_to(procedure_lien(procedure), procedure_lien(procedure)) - if procedure.publiee_ou_archivee? - %td= link_to(published_at.present? ? published_at.strftime('%d/%m/%Y %H:%M') : "", admin_procedure_href) + %td= link_to(procedure.published_at.present? ? procedure.published_at.strftime('%d/%m/%Y %H:%M') : "", admin_procedure_href) - else %td= link_to(procedure.created_at.strftime('%d/%m/%Y %H:%M'), admin_procedure_href) %td diff --git a/app/views/admin/types_de_champ/_fields.html.haml b/app/views/admin/types_de_champ/_fields.html.haml index 9acfa02a2..8d6ca418a 100644 --- a/app/views/admin/types_de_champ/_fields.html.haml +++ b/app/views/admin/types_de_champ/_fields.html.haml @@ -1,5 +1,5 @@ = f.fields_for type_de_champ_service.fields_for_var, types_de_champ, remote: true do |ff| - - type_champ = ff.object.object.type_champ + - type_champ = ff.object.type_champ .form-inline{ class: (type_champ == TypeDeChamp.type_champs.fetch(:header_section) ? 'header-section' : nil) } .form-group.libelle @@ -16,14 +16,14 @@ .form-group.drop-down-list{ class: (%w(drop_down_list multiple_drop_down_list linked_drop_down_list).include?(type_champ) ? 'show-inline' : nil), style: 'margin-right: 5px;' } %h4 Liste déroulante - = ff.fields_for :drop_down_list_attributes, ff.object.object.drop_down_list do |fff| + = ff.fields_for :drop_down_list_attributes, ff.object.drop_down_list do |fff| ~ fff.text_area :value, class: 'form-control drop_down_list', placeholder: "Ecrire une valeur par ligne et --valeur-- pour un séparateur.", rows: 3, cols: 30 = fff.hidden_field :id .form-group.pj-template{ class: (type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative)) ? 'show-inline' : nil } %h4 Modèle - if type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) - - template = ff.object.object.piece_justificative_template + - template = ff.object.piece_justificative_template - if !template.attached? = ff.file_field :piece_justificative_template, @@ -52,7 +52,7 @@ = ff.check_box :parcelles_agricoles Parcelles Agricoles - - hide_mandatory = (ff.object.object.private? || type_champ == TypeDeChamp.type_champs.fetch(:explication)) + - hide_mandatory = (ff.object.private? || type_champ == TypeDeChamp.type_champs.fetch(:explication)) .form-group.mandatory{ style: hide_mandatory ? 'visibility: hidden;' : nil } %h4 Obligatoire ? .center @@ -65,8 +65,9 @@ - if ff.object.id.present? .form-group %br   - = ff.object.button_up(index: ff.index, url: type_de_champ_service.move_up_url(ff), private: type_de_champ_service.private?) - = ff.object.button_down(index: ff.index, url: type_de_champ_service.move_down_url(ff), private: type_de_champ_service.private?) + - kind = type_de_champ_service.private? ? "private" : "public" + = button_up(@procedure, kind, ff.index, type_de_champ_service.move_up_url(ff)) + = button_down(@procedure, kind, ff.index, type_de_champ_service.move_down_url(ff)) .form-group %h4 position diff --git a/spec/decorators/type_de_champ_decorator_spec.rb b/spec/decorators/type_de_champ_decorator_spec.rb deleted file mode 100644 index 7a490aefc..000000000 --- a/spec/decorators/type_de_champ_decorator_spec.rb +++ /dev/null @@ -1,52 +0,0 @@ -require 'spec_helper' - -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) } - - describe '#button_up' do - describe 'with first piece justificative' do - let(:index) { 0 } - subject { type_de_champ_0.decorate } - let(:button_up) { type_de_champ_.decorate } - - it 'hide a button up' do - expect(subject.button_up(params)).to include('visibility: hidden') - end - it 'returns a button down' do - expect(subject.button_down(params)).to match(/fa-chevron-down/) - end - end - - describe 'with second out of three piece justificative' do - let(:index) { 1 } - subject { type_de_champ_1.decorate } - let(:button_up) { type_de_champ_1.decorate } - - it 'returns a button up' do - expect(subject.button_up(params)).to match(/fa-chevron-up/) - end - it 'returns a button down' do - expect(subject.button_down(params)).to match(/fa-chevron-down/) - end - end - - describe 'with last piece justificative' do - let(:index) { 2 } - subject { type_de_champ_2.decorate } - let(:button_up) { type_de_champ_1.decorate } - - it 'returns a button up' do - expect(subject.button_up(params)).to match(/fa-chevron-up/) - end - - it 'hide a button down' do - expect(subject.button_down(params)).to include('visibility: hidden') - end - end - end -end diff --git a/spec/decorators/type_de_piece_justificative_decorator_spec.rb b/spec/helpers/admin_formulaire_helper.rb similarity index 51% rename from spec/decorators/type_de_piece_justificative_decorator_spec.rb rename to spec/helpers/admin_formulaire_helper.rb index 5503b68a0..7f0804497 100644 --- a/spec/decorators/type_de_piece_justificative_decorator_spec.rb +++ b/spec/helpers/admin_formulaire_helper.rb @@ -1,9 +1,9 @@ -require 'spec_helper' +require 'rails_helper' -describe TypeDePieceJustificativeDecorator do +RSpec.describe AdminFormulaireHelper, type: :helper do let(:procedure) { create(:procedure) } + let(:kind) { 'piece_justificative' } let(:url) { 'http://localhost' } - let(:params) { { url: url, index: index } } let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) } let!(:type_de_piece_justificative_2) { create(:type_de_piece_justificative, procedure: procedure, order_place: 2) } @@ -11,40 +11,51 @@ describe TypeDePieceJustificativeDecorator do describe '#button_up' do describe 'with first piece justificative' do let(:index) { 0 } - subject { type_de_piece_justificative_0.decorate } - let(:button_up) { type_de_piece_justificative_.decorate } it 'returns a button up' do - expect(subject.button_up(params)).to be(nil) - end - it 'returns a button down' do - expect(subject.button_down(params)).to match(/fa-chevron-down/) + expect(button_up(procedure, kind, index, url)).to be(nil) end end describe 'with second out of three piece justificative' do let(:index) { 1 } - subject { type_de_piece_justificative_1.decorate } - let(:button_up) { type_de_piece_justificative_1.decorate } it 'returns a button up' do - expect(subject.button_up(params)).to match(/fa-chevron-up/) - end - it 'returns a button down' do - expect(subject.button_down(params)).to match(/fa-chevron-down/) + expect(button_up(procedure, kind, index, url)).to match(/fa-chevron-up/) end end describe 'with last piece justificative' do let(:index) { 2 } - subject { type_de_piece_justificative_2.decorate } - let(:button_up) { type_de_piece_justificative_1.decorate } it 'returns a button up' do - expect(subject.button_up(params)).to match(/fa-chevron-up/) + expect(button_up(procedure, kind, index, url)).to match(/fa-chevron-up/) end + end + end + + describe '#button_down' do + describe 'with first piece justificative' do + let(:index) { 0 } + it 'returns a button down' do - expect(subject.button_down(params)).to be(nil) + expect(button_down(procedure, kind, index, url)).to match(/fa-chevron-down/) + end + end + + describe 'with second out of three piece justificative' do + let(:index) { 1 } + + it 'returns a button down' do + expect(button_down(procedure, kind, index, url)).to match(/fa-chevron-down/) + end + end + + describe 'with last piece justificative' do + let(:index) { 2 } + + it 'returns nil' do + expect(button_down(procedure, kind, index, url)).to be(nil) end end 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 9cb1e32c9..ad15d570f 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 @@ -36,18 +36,18 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do 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) } - it { expect(subject).to have_css('#btn_down_0[style*="visibility: hidden"]') } - it { expect(subject).to have_css('#btn_up_0[style*="visibility: hidden"]') } + 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) } + it { expect(subject).not_to have_css('#btn_up_0') } it { expect(subject).to have_css('#btn_down_0') } - it { expect(subject).to have_css('#btn_up_0[style*="visibility: hidden"]') } it { expect(subject).to have_css('#btn_up_1') } - it { expect(subject).to have_css('#btn_down_1[style*="visibility: hidden"]') } + it { expect(subject).not_to have_css('#btn_down_1') } end end end 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 074cd31b7..8f80aa9c6 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 @@ -44,8 +44,8 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do 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).to have_css('#btn_down_0[style*="visibility: hidden"]') } - it { expect(subject).to have_css('#btn_up_0[style*="visibility: hidden"]') } + 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 @@ -53,9 +53,9 @@ describe 'admin/types_de_champ/show.html.haml', type: :view 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).to have_css('#btn_up_0[style*="visibility: hidden"]') } + it { expect(subject).not_to have_css('#btn_up_0') } it { expect(subject).to have_css('#btn_up_1') } - it { expect(subject).to have_css('#btn_down_1[style*="visibility: hidden"]') } + it { expect(subject).not_to have_css('#btn_down_1') } end end end