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.
This commit is contained in:
Javi Martín
2021-01-07 17:49:24 +01:00
parent 2968275a1c
commit c66a5a30ef
10 changed files with 63 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
class Admin::TableActionsComponent < ApplicationComponent class Admin::TableActionsComponent < ApplicationComponent
include TableActionLink include TableActionLink
attr_reader :record, :options attr_reader :record, :options
delegate :namespace, to: :helpers
def initialize(record = nil, **options) def initialize(record = nil, **options)
@record = record @record = record
@@ -18,7 +19,7 @@ class Admin::TableActionsComponent < ApplicationComponent
end end
def edit_path def edit_path
options[:edit_path] || admin_polymorphic_path(record, action: :edit) options[:edit_path] || namespaced_polymorphic_path(namespace, record, action: :edit)
end end
def edit_options def edit_options
@@ -30,7 +31,7 @@ class Admin::TableActionsComponent < ApplicationComponent
end end
def destroy_path def destroy_path
options[:destroy_path] || admin_polymorphic_path(record) options[:destroy_path] || namespaced_polymorphic_path(namespace, record)
end end
def destroy_options def destroy_options

View File

@@ -22,9 +22,6 @@ class SDGManagement::LocalTargets::IndexComponent < ApplicationComponent
end end
def actions(local_target) def actions(local_target)
render Admin::TableActionsComponent.new( render Admin::TableActionsComponent.new(local_target)
local_target,
edit_path: edit_sdg_management_local_target_path(local_target),
destroy_path: sdg_management_local_target_path(local_target))
end end
end end

View File

@@ -39,9 +39,7 @@ module AdminHelper
user_roles(user).join(", ") user_roles(user).join(", ")
end end
private def namespace
controller.class.name.split("::").first.underscore
def namespace end
controller.class.name.split("::").first.underscore
end
end end

View File

@@ -16,14 +16,23 @@ module ActionDispatch::Routing::UrlFor
end end
def admin_polymorphic_path(resource, options = {}) 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 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 = resolve_for(resource)
resolve_options = resolve.pop resolve_options = resolve.pop
polymorphic_path([:admin, *resolve], options.merge(resolve_options)) polymorphic_path([namespace, *resolve], options.merge(resolve_options))
else else
polymorphic_path([:admin, *resource_hierarchy_for(resource)], options) polymorphic_path([namespace, *resource_hierarchy_for(resource)], options)
end end
end end

View File

@@ -18,3 +18,7 @@ namespace :sdg_management do
get "#{type}/:id/edit", to: "relations#edit", as: "edit_#{type.singularize}" get "#{type}/:id/edit", to: "relations#edit", as: "edit_#{type.singularize}"
end end
end end
resolve "SDG::LocalTarget" do |target, options|
[:local_target, options.merge(id: target)]
end

View File

@@ -4,6 +4,10 @@ describe Admin::Budgets::TableActionsComponent, type: :component do
let(:budget) { create(:budget) } let(:budget) { create(:budget) }
let(:component) { Admin::Budgets::TableActionsComponent.new(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 it "renders links to edit budget, manage investments and edit groups and manage ballots" do
render_inline component render_inline component

View File

@@ -6,6 +6,10 @@ describe Admin::Poll::Officers::OfficersComponent, type: :component do
let(:officers) { [existing_officer, new_officer] } let(:officers) { [existing_officer, new_officer] }
let(:component) { Admin::Poll::Officers::OfficersComponent.new(officers) } 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 it "renders as many rows as officers" do
render_inline component render_inline component

View File

@@ -3,6 +3,10 @@ require "rails_helper"
describe Admin::Roles::TableActionsComponent, type: :component do describe Admin::Roles::TableActionsComponent, type: :component do
let(:user) { create(:user) } 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 it "renders link to add the role for new records" do
render_inline Admin::Roles::TableActionsComponent.new(user.build_manager) render_inline Admin::Roles::TableActionsComponent.new(user.build_manager)

View File

@@ -3,6 +3,10 @@ require "rails_helper"
describe Admin::TableActionsComponent, type: :component do describe Admin::TableActionsComponent, type: :component do
let(:record) { create(:banner) } 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 it "renders links to edit and destroy a record by default" do
render_inline Admin::TableActionsComponent.new(record) 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 "Edit"
expect(page).to have_link "Delete" expect(page).to have_link "Delete"
end 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 end

View File

@@ -186,6 +186,16 @@ describe "Polymorphic routes" do
) )
end end
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 end
def polymorphic_path(record, options = {}) def polymorphic_path(record, options = {})