From c66a5a30ef250e05f37c14d7a1818516b10a7e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 7 Jan 2021 17:49:24 +0100 Subject: [PATCH] Allow using table actions in different namespaces This way we can reuse it in sections like SDGManagement and URLs will be automatically generated as expected. --- .../admin/table_actions_component.rb | 5 +++-- .../local_targets/index_component.rb | 5 +---- app/helpers/admin_helper.rb | 8 +++----- config/initializers/routes_hierarchy.rb | 15 ++++++++++++--- config/routes/sdg_management.rb | 4 ++++ .../budgets/table_actions_component_spec.rb | 4 ++++ .../poll/officers/officers_component_spec.rb | 4 ++++ .../roles/table_actions_component_spec.rb | 4 ++++ .../admin/table_actions_component_spec.rb | 18 ++++++++++++++++++ spec/routing/polymorphic_routes_spec.rb | 10 ++++++++++ 10 files changed, 63 insertions(+), 14 deletions(-) diff --git a/app/components/admin/table_actions_component.rb b/app/components/admin/table_actions_component.rb index 990f54c74..9a5bf5134 100644 --- a/app/components/admin/table_actions_component.rb +++ b/app/components/admin/table_actions_component.rb @@ -1,6 +1,7 @@ class Admin::TableActionsComponent < ApplicationComponent include TableActionLink attr_reader :record, :options + delegate :namespace, to: :helpers def initialize(record = nil, **options) @record = record @@ -18,7 +19,7 @@ class Admin::TableActionsComponent < ApplicationComponent end def edit_path - options[:edit_path] || admin_polymorphic_path(record, action: :edit) + options[:edit_path] || namespaced_polymorphic_path(namespace, record, action: :edit) end def edit_options @@ -30,7 +31,7 @@ class Admin::TableActionsComponent < ApplicationComponent end def destroy_path - options[:destroy_path] || admin_polymorphic_path(record) + options[:destroy_path] || namespaced_polymorphic_path(namespace, record) end def destroy_options diff --git a/app/components/sdg_management/local_targets/index_component.rb b/app/components/sdg_management/local_targets/index_component.rb index 1d4a435f9..90ea383b8 100644 --- a/app/components/sdg_management/local_targets/index_component.rb +++ b/app/components/sdg_management/local_targets/index_component.rb @@ -22,9 +22,6 @@ class SDGManagement::LocalTargets::IndexComponent < ApplicationComponent end def actions(local_target) - render Admin::TableActionsComponent.new( - local_target, - edit_path: edit_sdg_management_local_target_path(local_target), - destroy_path: sdg_management_local_target_path(local_target)) + render Admin::TableActionsComponent.new(local_target) end end diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index dd5d9c6d4..45c508107 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -39,9 +39,7 @@ module AdminHelper user_roles(user).join(", ") end - private - - def namespace - controller.class.name.split("::").first.underscore - end + def namespace + controller.class.name.split("::").first.underscore + end end diff --git a/config/initializers/routes_hierarchy.rb b/config/initializers/routes_hierarchy.rb index fd096a64a..9b5cded6f 100644 --- a/config/initializers/routes_hierarchy.rb +++ b/config/initializers/routes_hierarchy.rb @@ -16,14 +16,23 @@ module ActionDispatch::Routing::UrlFor end def admin_polymorphic_path(resource, options = {}) + namespaced_polymorphic_path(:admin, resource, options) + end + + def sdg_management_polymorphic_path(resource, options = {}) + namespaced_polymorphic_path(:sdg_management, resource, options) + end + + def namespaced_polymorphic_path(namespace, resource, options = {}) if %w[Budget::Group Budget::Heading Poll::Booth Poll::BoothAssignment Poll::Officer - Poll::Question Poll::Question::Answer::Video Poll::Shift].include?(resource.class.name) + Poll::Question Poll::Question::Answer::Video Poll::Shift + SDG::LocalTarget].include?(resource.class.name) resolve = resolve_for(resource) resolve_options = resolve.pop - polymorphic_path([:admin, *resolve], options.merge(resolve_options)) + polymorphic_path([namespace, *resolve], options.merge(resolve_options)) else - polymorphic_path([:admin, *resource_hierarchy_for(resource)], options) + polymorphic_path([namespace, *resource_hierarchy_for(resource)], options) end end diff --git a/config/routes/sdg_management.rb b/config/routes/sdg_management.rb index 3e5d58bb7..445b0cf8f 100644 --- a/config/routes/sdg_management.rb +++ b/config/routes/sdg_management.rb @@ -18,3 +18,7 @@ namespace :sdg_management do get "#{type}/:id/edit", to: "relations#edit", as: "edit_#{type.singularize}" end end + +resolve "SDG::LocalTarget" do |target, options| + [:local_target, options.merge(id: target)] +end diff --git a/spec/components/admin/budgets/table_actions_component_spec.rb b/spec/components/admin/budgets/table_actions_component_spec.rb index 05312853c..36aeea537 100644 --- a/spec/components/admin/budgets/table_actions_component_spec.rb +++ b/spec/components/admin/budgets/table_actions_component_spec.rb @@ -4,6 +4,10 @@ describe Admin::Budgets::TableActionsComponent, type: :component do let(:budget) { create(:budget) } let(:component) { Admin::Budgets::TableActionsComponent.new(budget) } + before do + allow(ViewComponent::Base).to receive(:test_controller).and_return("Admin::BaseController") + end + it "renders links to edit budget, manage investments and edit groups and manage ballots" do render_inline component diff --git a/spec/components/admin/poll/officers/officers_component_spec.rb b/spec/components/admin/poll/officers/officers_component_spec.rb index f80fbb026..dab2dd04e 100644 --- a/spec/components/admin/poll/officers/officers_component_spec.rb +++ b/spec/components/admin/poll/officers/officers_component_spec.rb @@ -6,6 +6,10 @@ describe Admin::Poll::Officers::OfficersComponent, type: :component do let(:officers) { [existing_officer, new_officer] } let(:component) { Admin::Poll::Officers::OfficersComponent.new(officers) } + before do + allow(ViewComponent::Base).to receive(:test_controller).and_return("Admin::BaseController") + end + it "renders as many rows as officers" do render_inline component diff --git a/spec/components/admin/roles/table_actions_component_spec.rb b/spec/components/admin/roles/table_actions_component_spec.rb index 42382cf8b..a5bb0cd91 100644 --- a/spec/components/admin/roles/table_actions_component_spec.rb +++ b/spec/components/admin/roles/table_actions_component_spec.rb @@ -3,6 +3,10 @@ require "rails_helper" describe Admin::Roles::TableActionsComponent, type: :component do let(:user) { create(:user) } + before do + allow(ViewComponent::Base).to receive(:test_controller).and_return("Admin::BaseController") + end + it "renders link to add the role for new records" do render_inline Admin::Roles::TableActionsComponent.new(user.build_manager) diff --git a/spec/components/admin/table_actions_component_spec.rb b/spec/components/admin/table_actions_component_spec.rb index c4630c645..a643a6da1 100644 --- a/spec/components/admin/table_actions_component_spec.rb +++ b/spec/components/admin/table_actions_component_spec.rb @@ -3,6 +3,10 @@ require "rails_helper" describe Admin::TableActionsComponent, type: :component do let(:record) { create(:banner) } + before do + allow(ViewComponent::Base).to receive(:test_controller).and_return("Admin::BaseController") + end + it "renders links to edit and destroy a record by default" do render_inline Admin::TableActionsComponent.new(record) @@ -65,4 +69,18 @@ describe Admin::TableActionsComponent, type: :component do expect(page).to have_link "Edit" expect(page).to have_link "Delete" end + + context "different namespace" do + before do + allow(ViewComponent::Base).to receive(:test_controller).and_return("SDGManagement::BaseController") + end + + it "generates links to different namespaces" do + render_inline Admin::TableActionsComponent.new(create(:sdg_local_target)) + + expect(page).to have_css "a", count: 2 + expect(page).to have_css "a[href^='/sdg_management/'][href*='edit']", text: "Edit" + expect(page).to have_css "a[href^='/sdg_management/'][data-method='delete']", text: "Delete" + end + end end diff --git a/spec/routing/polymorphic_routes_spec.rb b/spec/routing/polymorphic_routes_spec.rb index 729e16d66..7874b0ac1 100644 --- a/spec/routing/polymorphic_routes_spec.rb +++ b/spec/routing/polymorphic_routes_spec.rb @@ -186,6 +186,16 @@ describe "Polymorphic routes" do ) end end + + describe "sdg_management_polymorphic_path" do + include ActionDispatch::Routing::UrlFor + + it "routes local targets" do + target = create(:sdg_local_target) + + expect(sdg_management_polymorphic_path(target)).to eq sdg_management_local_target_path(target) + end + end end def polymorphic_path(record, options = {})