Extract component to add/remove admin/mod/manager
We remove some duplication by doing so.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<% if already_has_role? %>
|
||||
<%= render Admin::TableActionsComponent.new(record, actions: actions) %>
|
||||
<% else %>
|
||||
<%= render Admin::TableActionsComponent.new(actions: []) do %>
|
||||
<%= link_to add_user_text, add_user_path, method: :post, class: "button success expanded" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
30
app/components/admin/roles/table_actions_component.rb
Normal file
30
app/components/admin/roles/table_actions_component.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
class Admin::Roles::TableActionsComponent < ApplicationComponent
|
||||
attr_reader :record, :actions
|
||||
|
||||
def initialize(record, actions: [:destroy])
|
||||
@record = record
|
||||
@actions = actions
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def role
|
||||
record.class.name.tableize
|
||||
end
|
||||
|
||||
def already_has_role?
|
||||
record.persisted?
|
||||
end
|
||||
|
||||
def add_user_text
|
||||
t("admin.#{role}.#{role.singularize}.add")
|
||||
end
|
||||
|
||||
def add_user_path
|
||||
{
|
||||
controller: "admin/#{role}",
|
||||
action: :create,
|
||||
user_id: record.user
|
||||
}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user