diff --git a/app/components/admin/organizations/table_actions_component.html.erb b/app/components/admin/organizations/table_actions_component.html.erb new file mode 100644 index 000000000..5c0b565ab --- /dev/null +++ b/app/components/admin/organizations/table_actions_component.html.erb @@ -0,0 +1,13 @@ +<%= render Admin::TableActionsComponent.new(actions: []) do %> + <% if can_verify? %> + <%= link_to t("admin.organizations.index.verify"), + verify_admin_organization_path(organization, request.query_parameters), + method: :put, class: "button success small-5" %> + <% end %> + + <% if can_reject? %> + <%= link_to t("admin.organizations.index.reject"), + reject_admin_organization_path(organization, request.query_parameters), + method: :put, class: "button hollow alert small-5" %> + <% end %> +<% end %> diff --git a/app/components/admin/organizations/table_actions_component.rb b/app/components/admin/organizations/table_actions_component.rb new file mode 100644 index 000000000..d0b29708f --- /dev/null +++ b/app/components/admin/organizations/table_actions_component.rb @@ -0,0 +1,18 @@ +class Admin::Organizations::TableActionsComponent < ApplicationComponent + delegate :can?, to: :controller + attr_reader :organization + + def initialize(organization) + @organization = organization + end + + private + + def can_verify? + can? :verify, organization + end + + def can_reject? + can? :reject, organization + end +end diff --git a/app/views/admin/organizations/index.html.erb b/app/views/admin/organizations/index.html.erb index 7970fde46..b1110488a 100644 --- a/app/views/admin/organizations/index.html.erb +++ b/app/views/admin/organizations/index.html.erb @@ -48,17 +48,7 @@ <% end %> - <% if can? :verify, organization %> - <%= link_to t("admin.organizations.index.verify"), - verify_admin_organization_path(organization, request.query_parameters), - method: :put, class: "button success small-5" %> - <% end %> - - <% if can? :reject, organization %> - <%= link_to t("admin.organizations.index.reject"), - reject_admin_organization_path(organization, request.query_parameters), - method: :put, class: "button hollow alert small-5" %> - <% end %> + <%= render Admin::Organizations::TableActionsComponent.new(organization) %> <% end %> diff --git a/app/views/admin/organizations/search.html.erb b/app/views/admin/organizations/search.html.erb index e3d0564cf..2e534d052 100644 --- a/app/views/admin/organizations/search.html.erb +++ b/app/views/admin/organizations/search.html.erb @@ -44,17 +44,7 @@ <% end %> - <% if can? :verify, organization %> - <%= link_to t("admin.organizations.index.verify"), - verify_admin_organization_path(organization, request.query_parameters), - method: :put, class: "button success small-5" %> - <% end %> - - <% if can? :reject, organization %> - <%= link_to t("admin.organizations.index.reject"), - reject_admin_organization_path(organization, request.query_parameters), - method: :put, class: "button hollow alert small-5" %> - <% end %> + <%= render Admin::Organizations::TableActionsComponent.new(organization) %> <% end %> diff --git a/spec/components/admin/organizations/table_actions_component_spec.rb b/spec/components/admin/organizations/table_actions_component_spec.rb new file mode 100644 index 000000000..66ab51cf2 --- /dev/null +++ b/spec/components/admin/organizations/table_actions_component_spec.rb @@ -0,0 +1,46 @@ +require "rails_helper" + +describe Admin::Organizations::TableActionsComponent, type: :component do + let(:organization) { create(:organization) } + let(:component) { Admin::Organizations::TableActionsComponent.new(organization) } + + it "renders links to verify and reject when it can" do + allow(component).to receive(:can_verify?).and_return(true) + allow(component).to receive(:can_reject?).and_return(true) + + render_inline component + + expect(page).to have_css "a", count: 2 + expect(page).to have_css "a[href*='verify'][data-method='put']", text: "Verify" + expect(page).to have_css "a[href*='reject'][data-method='put']", text: "Reject" + end + + it "renders link to verify when it cannot reject" do + allow(component).to receive(:can_verify?).and_return(true) + allow(component).to receive(:can_reject?).and_return(false) + + render_inline component + + expect(page).to have_css "a", count: 1 + expect(page).to have_link "Verify" + end + + it "renders link to reject when it cannot verify" do + allow(component).to receive(:can_verify?).and_return(false) + allow(component).to receive(:can_reject?).and_return(true) + + render_inline component + + expect(page).to have_css "a", count: 1 + expect(page).to have_link "Reject" + end + + it "does not render any actions when it cannot verify nor reject" do + allow(component).to receive(:can_verify?).and_return(false) + allow(component).to receive(:can_reject?).and_return(false) + + render_inline component + + expect(page).not_to have_css "a" + end +end