Extract component to add/remove admin/mod/manager

We remove some duplication by doing so.
This commit is contained in:
Javi Martín
2020-07-01 19:01:06 +02:00
parent 02e27e13dc
commit 08c2bfc255
9 changed files with 63 additions and 33 deletions

View File

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

View 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