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
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

View File

@@ -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

View File

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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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 = {})