From 9ab672a31cd3a1aeea59e6ae3dab96e4057ee997 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 7 Jul 2017 16:42:58 +0200 Subject: CustomLinks: Pass referential directly when initialising Instead of getting the referential to use when building the polymorphic URL from the `UserContext`, pass in a referential directly. The old code that used `user_context.context[:referential]` relied on the fact that `ApplicationController#pundit_user` was defined as follows: def pundit_user UserContext.new(current_user, referential: self.try(:current_referential)) end (We pass `pundit_user` into `CustomLinks` from `TableBuilderHelper#build_links`.) However, Robert's change 747d333ffbcc8ee0c9f1daf93ccca32799434e04 removes the `current_referential` call from `#pundit_user`. In `CustomLinks`, we actually always want to be using `current_referential`. For example, on `Companies#index` (/line_referentials/:id/companies), `CustomLinks` fails to build a correct #show link because `user_context.context[:referential]` is `nil`, when it should instead be a `LineReferential` object, that gets returned by the `#current_referential` helper method. Sure, `#current_referential` is hard to understand, so maybe we'll change that around in the future, but this at least allows us to use the current referential in `CustomLinks`. Refs #3479 --- app/helpers/table_builder_helper.rb | 2 +- app/helpers/table_builder_helper/custom_links.rb | 10 ++++------ spec/helpers/table_builder_helper/custom_links_spec.rb | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index d82f1eb03..897e842a8 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -157,7 +157,7 @@ module TableBuilderHelper menu = content_tag :ul, class: 'dropdown-menu' do ( - CustomLinks.new(item, pundit_user, links).links + + CustomLinks.new(item, pundit_user, links, referential).links + item.action_links.select { |link| link.is_a?(Link) } ).map do |link| gear_menu_link(link) diff --git a/app/helpers/table_builder_helper/custom_links.rb b/app/helpers/table_builder_helper/custom_links.rb index 4e385b266..b1bb11f10 100644 --- a/app/helpers/table_builder_helper/custom_links.rb +++ b/app/helpers/table_builder_helper/custom_links.rb @@ -8,12 +8,13 @@ module TableBuilderHelper unarchive: :put } - attr_reader :actions, :object, :user_context + attr_reader :actions, :object, :user_context, :referential - def initialize(object, user_context, actions) + def initialize(object, user_context, actions, referential = nil) @object = object @user_context = user_context @actions = actions + @referential = referential end def links @@ -33,10 +34,7 @@ module TableBuilderHelper polymorph_url << action end - polymorph_url += URL.polymorphic_url_parts( - object, - user_context.context[:referential] - ) + polymorph_url += URL.polymorphic_url_parts(object, referential) end def method_for_action(action) diff --git a/spec/helpers/table_builder_helper/custom_links_spec.rb b/spec/helpers/table_builder_helper/custom_links_spec.rb index d6fbd2c64..ac60c7da3 100644 --- a/spec/helpers/table_builder_helper/custom_links_spec.rb +++ b/spec/helpers/table_builder_helper/custom_links_spec.rb @@ -1,4 +1,20 @@ describe TableBuilderHelper::CustomLinks do + describe "#polymorphic_url" do + it "returns the correct URL path for Companies#show" do + company = build_stubbed(:company) + user_context = UserContext.new(build_stubbed(:user)) + + expect( + TableBuilderHelper::CustomLinks.new( + company, + user_context, + [:show], + company.line_referential + ).polymorphic_url(:show) + ).to eq([company.line_referential, company]) + end + end + describe "#authorized_actions" do it "includes :show" do referential = build_stubbed(:referential) -- cgit v1.2.3