diff options
| -rw-r--r-- | app/controllers/workbenches_controller.rb | 3 | ||||
| -rw-r--r-- | app/decorators/company_decorator.rb | 4 | ||||
| -rw-r--r-- | app/decorators/referential_decorator.rb | 9 | ||||
| -rw-r--r-- | app/helpers/table_builder_helper.rb | 27 | ||||
| -rw-r--r-- | app/views/layouts/navigation/_page_header.html.slim | 13 | ||||
| -rw-r--r-- | app/views/workbenches/show.html.slim | 4 | ||||
| -rw-r--r-- | lib/af83/decorator.rb | 19 | ||||
| -rw-r--r-- | lib/af83/decorator/enhanced_decorator.rb (renamed from lib/af83/enhanced_decorator.rb) | 5 | ||||
| -rw-r--r-- | lib/af83/decorator/link.rb | 1 | ||||
| -rw-r--r-- | spec/decorators/referential_decorator_spec.rb | 52 | ||||
| -rw-r--r-- | spec/helpers/table_builder_helper_spec.rb | 70 | ||||
| -rw-r--r-- | spec/support/decorator_helpers.rb | 23 |
12 files changed, 139 insertions, 91 deletions
diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index b2dac9e67..2a71fe811 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -18,9 +18,8 @@ class WorkbenchesController < ChouetteController @q_for_form = scope.ransack(params[:q]) @q_for_result = scope.ransack(ransack_params) @wbench_refs = sort_result(@q_for_result.result).paginate(page: params[:page], per_page: 30) - @wbench_refs = ModelDecorator.decorate( + @wbench_refs = ReferentialDecorator.decorate( @wbench_refs, - with: ReferentialDecorator, context: { current_workbench_id: params[:id] } diff --git a/app/decorators/company_decorator.rb b/app/decorators/company_decorator.rb index 3737b851e..aadce68bb 100644 --- a/app/decorators/company_decorator.rb +++ b/app/decorators/company_decorator.rb @@ -7,7 +7,9 @@ class CompanyDecorator < AF83::Decorator end with_instance_decorator do |instance_decorator| - instance_decorator.show_action_link + instance_decorator.show_action_link do |l| + l.href { [context[:referential], object] } + end instance_decorator.edit_action_link do |l| l.content {|l| l.action == "show" ? h.t('actions.edit') : h.t('companies.actions.edit') } diff --git a/app/decorators/referential_decorator.rb b/app/decorators/referential_decorator.rb index 226f8269e..3132cbf92 100644 --- a/app/decorators/referential_decorator.rb +++ b/app/decorators/referential_decorator.rb @@ -2,14 +2,15 @@ class ReferentialDecorator < AF83::Decorator decorates Referential with_instance_decorator do |instance_decorator| + instance_decorator.show_action_link instance_decorator.edit_action_link - instance_decorator.action_link feature: :referential_vehicle_journeys, secondary: :show do |l| + instance_decorator.action_link feature: :referential_vehicle_journeys, secondary: :show, on: :show do |l| l.content t('referential_vehicle_journeys.index.title') l.href { h.referential_vehicle_journeys_path(object) } end - instance_decorator.action_link feature: :purchase_windows, secondary: :show do |l| + instance_decorator.action_link feature: :purchase_windows, secondary: :show, on: :show do |l| l.content t('purchase_windows.index.title') l.href { h.referential_purchase_windows_path(object) } end @@ -35,13 +36,13 @@ class ReferentialDecorator < AF83::Decorator l.method :put end - instance_decorator.action_link policy: :unarchive, secondary: :show do |l| + instance_decorator.action_link policy: :unarchive, secondary: :show, on: :show do |l| l.content t('actions.unarchive') l.href { h.unarchive_referential_path(object.id) } l.method :put end - instance_decorator.action_link policy: :edit, secondary: :show do |l| + instance_decorator.action_link policy: :edit, secondary: :show, on: :show do |l| l.content 'Purger' l.href '#' l.type 'button' diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index e29695b55..5c9da3d36 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -86,12 +86,15 @@ module TableBuilderHelper overhead: [], # Possibility to override the result of collection.model - model: nil + model: nil, + + #overrides the params[:action] value + action: nil ) content_tag :table, - thead(collection, columns, sortable, selectable, links.any?, overhead, model || collection.model) + - tbody(collection, columns, selectable, links, overhead), + thead(collection, columns, sortable, selectable, links.any?, overhead, model || collection.model, action || params[:action]) + + tbody(collection, columns, selectable, links, overhead, action || params[:action]), class: cls end @@ -109,7 +112,7 @@ module TableBuilderHelper private - def thead(collection, columns, sortable, selectable, has_links, overhead, model ) + def thead(collection, columns, sortable, selectable, has_links, overhead, model, action) content_tag :thead do # Inserts overhead content if any specified over_head = '' @@ -189,7 +192,7 @@ module TableBuilderHelper # Inserts a blank column for the gear menu last_item = collection.last - action_links = last_item && last_item.respond_to?(:action_links) && (last_item&.action_links&.is_a?(AF83::Decorator::ActionLinks) ? last_item.action_links(params[:action]) : last_item.action_links) + action_links = last_item && last_item.respond_to?(:action_links) && (last_item&.action_links&.is_a?(AF83::Decorator::ActionLinks) ? last_item.action_links(action) : last_item.action_links) if has_links || action_links.try(:any?) hcont << content_tag(:th, '') end @@ -201,7 +204,7 @@ module TableBuilderHelper end end - def tr item, columns, selectable, links, overhead, model_name + def tr item, columns, selectable, links, overhead, model_name, action klass = "#{model_name}-#{item.id}" content_tag :tr, class: klass do bcont = [] @@ -269,12 +272,12 @@ module TableBuilderHelper end end - action_links = item && item.respond_to?(:action_links) && (item.action_links.is_a?(AF83::Decorator::ActionLinks) ? item.action_links(params[:action]) : item.action_links) + action_links = item && item.respond_to?(:action_links) && (item.action_links.is_a?(AF83::Decorator::ActionLinks) ? item.action_links(action) : item.action_links) if links.any? || action_links.try(:any?) bcont << content_tag( :td, - build_links(item, links), + build_links(item, links, action), class: 'actions' ) end @@ -283,12 +286,12 @@ module TableBuilderHelper end end - def tbody(collection, columns, selectable, links, overhead) + def tbody(collection, columns, selectable, links, overhead, action) model_name = TableBuilderHelper.item_row_class_name collection content_tag :tbody do collection.map do |item| - tr item, columns, selectable, links, overhead, model_name + tr item, columns, selectable, links, overhead, model_name, action end.join.html_safe end end @@ -301,7 +304,7 @@ module TableBuilderHelper end end - def build_links(item, links) + def build_links(item, links, action) trigger = content_tag( :div, class: 'btn dropdown-toggle', @@ -313,7 +316,7 @@ module TableBuilderHelper action_links = item.action_links if action_links.is_a?(AF83::Decorator::ActionLinks) menu = content_tag :div, class: 'dropdown-menu' do - item.action_links(params[:action]).grouped_by(:primary, :secondary, :footer).map do |group, _links| + item.action_links(action).grouped_by(:primary, :secondary, :footer).map do |group, _links| if _links.any? content_tag :ul, class: group do _links.map{|link| gear_menu_link(link)}.join.html_safe diff --git a/app/views/layouts/navigation/_page_header.html.slim b/app/views/layouts/navigation/_page_header.html.slim index 8240aa4c0..e407e53da 100644 --- a/app/views/layouts/navigation/_page_header.html.slim +++ b/app/views/layouts/navigation/_page_header.html.slim @@ -26,12 +26,11 @@ = link.to_html do |l| - l.class "btn btn-default #{l.disabled ? "disabled" : ""}" - - if action_links&.secondary&.any? || content_for?(:page_header_content) - .container-fluid - .row - .col-lg-12.text-right.mb-sm - - action_links && action_links.secondary.each do |link| + - if action_links&.secondary&.any? + .row.mb-sm + .col-lg-12.text-right + - action_links.secondary.each do |link| = link.to_html do |l| - l.class "btn btn-primary #{l.disabled ? "disabled" : ""}" - - if content_for? :page_header_content - = yield :page_header_content + - if content_for? :page_header_content + = yield :page_header_content diff --git a/app/views/workbenches/show.html.slim b/app/views/workbenches/show.html.slim index 8907f3f08..a162ca334 100644 --- a/app/views/workbenches/show.html.slim +++ b/app/views/workbenches/show.html.slim @@ -59,8 +59,8 @@ ) \ ], selectable: ->(ref){ @workbench.referentials.include?(ref) }, - links: [:show, :edit], - cls: 'table has-filter has-search' + cls: 'table has-filter has-search', + action: :index = multiple_selection_toolbox([:delete], collection_name: 'referentials') diff --git a/lib/af83/decorator.rb b/lib/af83/decorator.rb index 53e09fed3..e83e03e0f 100644 --- a/lib/af83/decorator.rb +++ b/lib/af83/decorator.rb @@ -1,6 +1,6 @@ class AF83::Decorator < ModelDecorator - include AF83::EnhancedDecorator - extend AF83::EnhancedDecorator::ClassMethods + include AF83::Decorator::EnhancedDecorator + extend AF83::Decorator::EnhancedDecorator::ClassMethods def self.decorates klass instance_decorator.decorates klass @@ -31,6 +31,8 @@ class AF83::Decorator < ModelDecorator class ActionLinks attr_reader :options + delegate :each, :map, :size, :first, :last, :any?, :select, to: :resolve + def initialize opts @options = opts.deep_dup end @@ -66,6 +68,7 @@ class AF83::Decorator < ModelDecorator end out end + alias_method :to_ary, :resolve def grouped_by *groups add_footer = groups.include?(:footer) @@ -91,14 +94,6 @@ class AF83::Decorator < ModelDecorator out end - alias_method :to_ary, :resolve - - %w(each map size first last any?).each do |meth| - define_method meth do |*args, &block| - resolve.send meth, *args, &block - end - end - private def returning_a_copy &block out = ActionLinks.new options @@ -111,7 +106,7 @@ class AF83::Decorator < ModelDecorator end class InstanceDecorator < Draper::Decorator - include AF83::EnhancedDecorator - extend AF83::EnhancedDecorator::ClassMethods + include AF83::Decorator::EnhancedDecorator + extend AF83::Decorator::EnhancedDecorator::ClassMethods end end diff --git a/lib/af83/enhanced_decorator.rb b/lib/af83/decorator/enhanced_decorator.rb index aa9620dba..904d1b2da 100644 --- a/lib/af83/enhanced_decorator.rb +++ b/lib/af83/decorator/enhanced_decorator.rb @@ -1,4 +1,4 @@ -module AF83::EnhancedDecorator +module AF83::Decorator::EnhancedDecorator module ClassMethods def action_link args={} raise "You are using `action_link` inside a with_instance_decorator block, but not on the instance decorator itself.\n Use `instance_decorator.action_link` or move outside of the block, as this may lead to an unforeseen behaviour." if @_with_instance_decorator @@ -33,6 +33,7 @@ module AF83::EnhancedDecorator def show_action_link args={}, &block opts = { + on: :index, primary: :index, before_block: -> (l){ l.content { h.t('actions.show') } @@ -62,7 +63,7 @@ module AF83::EnhancedDecorator before_block: -> (l){ l.content { h.destroy_link_content } l.href { [object] } - l.method { :delete } + l.method :delete l.data {{ confirm: h.t('actions.destroy_confirm') }} } } diff --git a/lib/af83/decorator/link.rb b/lib/af83/decorator/link.rb index 83d40eba8..b1958532f 100644 --- a/lib/af83/decorator/link.rb +++ b/lib/af83/decorator/link.rb @@ -96,7 +96,6 @@ class AF83::Decorator::Link enabled = enabled && check_policy(@options[:_policy]) if @options[:_policy].present? enabled = enabled && check_feature(@options[:_feature]) if @options[:_feature].present? - enabled end diff --git a/spec/decorators/referential_decorator_spec.rb b/spec/decorators/referential_decorator_spec.rb index 9e34a0109..efc438132 100644 --- a/spec/decorators/referential_decorator_spec.rb +++ b/spec/decorators/referential_decorator_spec.rb @@ -21,8 +21,8 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do context 'unarchived referential' do context 'no rights' do - it 'has only a Calendar action' do - expect_action_link_hrefs.to eq([referential_time_tables_path(object)]) + it 'has only show and Calendar actions' do + expect_action_link_hrefs.to eq([[object], referential_time_tables_path(object)]) end end @@ -31,8 +31,9 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do let( :user ){ build_stubbed :allmighty_user } it 'has only default actions' do - expect_action_link_elements.to be_empty + expect_action_link_elements.to eq ["Consulter", "Calendriers", "Dupliquer"] expect_action_link_hrefs.to eq([ + [object], referential_time_tables_path(object), new_referential_path(from: object) ]) @@ -41,16 +42,36 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do context 'all rights and same organisation' do let( :user ){ build_stubbed :allmighty_user, organisation: referential.organisation } + let( :action){ :index } + context "on index" do + it 'has corresponding actions' do + expect_action_link_elements(action).to eq ["Consulter", "Editer", "Calendriers", "Dupliquer", "Valider", "Conserver","<span class=\"fa fa-trash mr-xs\"></span>Supprimer"] + expect_action_link_hrefs(action).to eq([ + [object], + [:edit, object], + referential_time_tables_path(object), + new_referential_path(from: object), + referential_select_compliance_control_set_path(object), + archive_referential_path(object), + referential_path(object) + ]) + end + end - it 'has all actions' do - expect_action_link_elements.to eq(%w{Purger}) - expect_action_link_hrefs.to eq([ - referential_time_tables_path(object), - new_referential_path(from: object), - referential_select_compliance_control_set_path(object), - archive_referential_path(object), - referential_path(object) - ]) + context "on show" do + let( :action){ :show } + it 'has corresponding actions' do + expect_action_link_elements(action).to eq ["Editer", "Calendriers", "Dupliquer", "Valider", "Conserver", "Purger", "<span class=\"fa fa-trash mr-xs\"></span>Supprimer"] + expect_action_link_hrefs(action).to eq([ + [:edit, object], + referential_time_tables_path(object), + new_referential_path(from: object), + referential_select_compliance_control_set_path(object), + archive_referential_path(object), + "#", + referential_path(object) + ]) + end end end end @@ -58,16 +79,17 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do context 'archived referential' do before { referential.archived_at = 42.seconds.ago } context 'no rights' do - it 'has only a Calendar action' do - expect_action_link_hrefs.to eq([referential_time_tables_path(object)]) + it 'has only ahow and calendar actions' do + expect_action_link_hrefs.to eq([[object], referential_time_tables_path(object)]) end end context 'all rights and different organisation' do let( :user ){ build_stubbed :allmighty_user } it 'has only default actions' do - expect_action_link_elements.to be_empty + expect_action_link_elements.to eq ["Consulter", "Calendriers", "Dupliquer"] expect_action_link_hrefs.to eq([ + [object], referential_time_tables_path(object), new_referential_path(from: object) ]) diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb index 8b383d88d..5bddbb16f 100644 --- a/spec/helpers/table_builder_helper_spec.rb +++ b/spec/helpers/table_builder_helper_spec.rb @@ -6,6 +6,13 @@ module TableBuilderHelper end describe TableBuilderHelper, type: :helper do + let(:features){ [] } + before do + allow_any_instance_of(AF83::Decorator::Link).to receive(:check_feature){|f| + features.include?(f) + } + end + describe "#table_builder_2" do it "builds a table" do referential = build_stubbed(:workbench_referential) @@ -36,9 +43,8 @@ describe TableBuilderHelper, type: :helper do id: referentials[0].workbench.id }) - referentials = ModelDecorator.decorate( - referentials, - with: ReferentialDecorator + referentials = ReferentialDecorator.decorate( + referentials ) expected = <<-HTML @@ -77,15 +83,21 @@ describe TableBuilderHelper, type: :helper do <td class="actions"> <div class="btn-group"> <div class="btn dropdown-toggle" data-toggle="dropdown"><span class="fa fa-cog"></span></div> - <ul class="dropdown-menu"> - <li><a href="/referentials/#{referential.id}">Consulter</a></li> - <li><a href="/referentials/#{referential.id}/edit">Editer</a></li> - <li><a href="/referentials/#{referential.id}/time_tables">Calendriers</a></li> - <li><a href="/referentials/new?from=#{referential.id}">Dupliquer</a></li> - <li><a href="/referentials/#{referential.id}/select_compliance_control_set">Valider</a></li> - <li><a rel="nofollow" data-method="put" href="/referentials/#{referential.id}/archive">Conserver</a></li> - <li class="delete-action"><a data-confirm="Etes vous sûr de vouloir supprimer ce jeu de données ?" rel="nofollow" data-method="delete" href="/referentials/#{referential.id}"><span class="fa fa-trash mr-xs"></span>Supprimer</a></li> - </ul> + <div class="dropdown-menu"> + <ul class="primary"> + <li class=""><a href="/referentials/#{referential.id}">Consulter</a></li> + <li class=""><a href="/referentials/#{referential.id}/edit">Editer</a></li> + </ul> + <ul class="other"> + <li class=""><a href="/referentials/#{referential.id}/time_tables">Calendriers</a></li> + <li class=""><a href="/referentials/new?from=#{referential.id}">Dupliquer</a></li> + <li class=""><a href="/referentials/#{referential.id}/select_compliance_control_set">Valider</a></li> + <li class=""><a rel="nofollow" data-method="put" href="/referentials/#{referential.id}/archive">Conserver</a></li> + </ul> + <ul class="footer"> + <li class=" delete-action"><a data-confirm="Etes vous sûr de vouloir supprimer ce jeu de données ?" rel="nofollow" data-method="delete" href="/referentials/#{referential.id}"><span class="fa fa-trash mr-xs"></span>Supprimer</a></li> + </ul> + </div> </div> </td> </tr> @@ -149,7 +161,7 @@ describe TableBuilderHelper, type: :helper do ) ], selectable: true, - links: [:show, :edit], + action: :index, cls: 'table has-filter has-search' ) @@ -195,9 +207,9 @@ describe TableBuilderHelper, type: :helper do referential_id: referential.id }) - companies = ModelDecorator.decorate( + companies = CompanyDecorator.decorate( companies, - with: CompanyDecorator + context: { referential: referential } ) stub_policy_scope(company) @@ -223,9 +235,11 @@ describe TableBuilderHelper, type: :helper do <td class="actions"> <div class="btn-group"> <div class="btn dropdown-toggle" data-toggle="dropdown"><span class="fa fa-cog"></span></div> - <ul class="dropdown-menu"> - <li><a href="/referentials/#{referential.id}/companies/#{company.id}">Consulter</a></li> - </ul> + <div class="dropdown-menu"> + <ul class="primary"> + <li class=""><a href="/referentials/#{referential.id}/companies/#{company.id}">Consulter</a></li> + </ul> + </div> </div> </td> </tr> @@ -307,9 +321,8 @@ describe TableBuilderHelper, type: :helper do referential_id: referential.id }) - companies = ModelDecorator.decorate( + companies = CompanyDecorator.decorate( companies, - with: CompanyDecorator, context: { referential: line_referential } ) stub_policy_scope(company) @@ -336,9 +349,11 @@ describe TableBuilderHelper, type: :helper do <td class="actions"> <div class="btn-group"> <div class="btn dropdown-toggle" data-toggle="dropdown"><span class="fa fa-cog"></span></div> - <ul class="dropdown-menu"> - <li><a href="/referentials/#{referential.id}/companies/#{company.id}">Consulter</a></li> - </ul> + <div class="dropdown-menu"> + <ul class="primary"> + <li class=""><a href="/line_referentials/#{line_referential.id}/companies/#{company.id}">Consulter</a></li> + </ul> + </div> </div> </td> </tr> @@ -374,7 +389,6 @@ describe TableBuilderHelper, type: :helper do ), ], sortable: false, - links: [:show, :edit, :delete], cls: 'table has-search' ) @@ -425,7 +439,7 @@ describe TableBuilderHelper, type: :helper do let(:selectable){ false } it "sets all rows as non selectable" do items.each do |i| - tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name) + tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name, :index) klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}" selector = "tr.#{klass} [type=checkbox]" expect(tr).to_not have_selector selector @@ -437,7 +451,7 @@ describe TableBuilderHelper, type: :helper do let(:selectable){ true } it "adds a checkbox in all rows" do items.each do |i| - tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name) + tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name, :index) klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}" selector = "tr.#{klass} [type=checkbox]" expect(tr).to have_selector selector @@ -449,14 +463,14 @@ describe TableBuilderHelper, type: :helper do let(:selectable){ ->(i){ i.id != item.id } } it "adds a checkbox in all rows" do items.each do |i| - tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name) + tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name, :index) klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}" selector = "tr.#{klass} [type=checkbox]" expect(tr).to have_selector selector end end it "disables this rows checkbox" do - tr = helper.send(:tr, item, columns, selectable, links, overhead, model_name) + tr = helper.send(:tr, item, columns, selectable, links, overhead, model_name, :index) klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{item.id}" selector = "tr.#{klass} [type=checkbox][disabled]" expect(tr).to have_selector selector diff --git a/spec/support/decorator_helpers.rb b/spec/support/decorator_helpers.rb index 9d450deb1..b2c41e842 100644 --- a/spec/support/decorator_helpers.rb +++ b/spec/support/decorator_helpers.rb @@ -5,18 +5,31 @@ module Support subject{ object.decorate } let( :policy ){ ::Pundit.policy(user_context, object) } let( :user_context ){ UserContext.new(user, referential: referential) } - + let( :features ){ [] } + let( :filtered_action_links){} before do allow_any_instance_of(Draper::HelperProxy).to receive(:policy).and_return policy + allow_any_instance_of(AF83::Decorator::Link).to receive(:check_feature){|f| + features.include?(f) + } end end end - def expect_action_link_hrefs - expect( subject.action_links.select(&Link.method(:===)).map(&:href) ) + def expect_action_link_hrefs(action=:index) + if subject.action_links.is_a? AF83::Decorator::ActionLinks + expect( subject.action_links(action).map(&:href) ) + else + expect( subject.action_links.select(&Link.method(:===)).map(&:href) ) + end end - def expect_action_link_elements - expect( subject.action_links.select(&HTMLElement.method(:===)).map(&:content) ) + + def expect_action_link_elements(action=:index) + if subject.action_links.is_a? AF83::Decorator::ActionLinks + expect( subject.action_links(action).map(&:content) ) + else + expect( subject.action_links.select(&HTMLElement.method(:===)).map(&:content) ) + end end end end |
