Class: EasyAdmin::Permissions::ResourcePermissions
- Inherits:
-
Object
- Object
- EasyAdmin::Permissions::ResourcePermissions
- Defined in:
- lib/easy_admin/permissions/resource_permissions.rb
Constant Summary collapse
- STANDARD_ACTIONS =
Standard EasyAdmin actions that every resource supports
%w[create read update delete].freeze
- ADMIN_ACTIONS =
Additional administrative actions
%w[manage_versions batch_actions row_actions].freeze
- ACTION_MAPPING =
Legacy mapping for backwards compatibility
{ 'index' => 'read', 'show' => 'read', 'new' => 'create', 'edit' => 'update', 'destroy' => 'delete' }.freeze
Class Method Summary collapse
-
.actions_for_resource(resource_name) ⇒ Object
Get all actions for a specific resource.
-
.available_resources ⇒ Object
Get all available resource names.
-
.discover_all_permissions ⇒ Object
Discover all EasyAdmin resources and their permissions.
-
.seed_permissions! ⇒ Object
Seed permissions into database.
Class Method Details
.actions_for_resource(resource_name) ⇒ Object
Get all actions for a specific resource
68 69 70 71 72 73 |
# File 'lib/easy_admin/permissions/resource_permissions.rb', line 68 def actions_for_resource(resource_name) resource_class = find_resource_class(resource_name) return STANDARD_ACTIONS if resource_class.nil? STANDARD_ACTIONS + extract_custom_actions(resource_class) end |
.available_resources ⇒ Object
Get all available resource names
63 64 65 |
# File 'lib/easy_admin/permissions/resource_permissions.rb', line 63 def available_resources all_resource_classes.map { |resource_class| extract_resource_name(resource_class) } end |
.discover_all_permissions ⇒ Object
Discover all EasyAdmin resources and their permissions
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/easy_admin/permissions/resource_permissions.rb', line 21 def = [] all_resource_classes.each do |resource_class| resource_name = extract_resource_name(resource_class) # Add standard CRUD permissions STANDARD_ACTIONS.each do |action| << { name: "#{resource_name}:#{action}", resource_type: resource_name, action: action, description: (resource_name, action) } end # Add administrative permissions ADMIN_ACTIONS.each do |action| << { name: "#{resource_name}:#{action}", resource_type: resource_name, action: action, description: (resource_name, action) } end # Add any custom actions defined in the resource custom_actions = extract_custom_actions(resource_class) custom_actions.each do |action| << { name: "#{resource_name}:#{action}", resource_type: resource_name, action: action, description: (resource_name, action) } end end end |
.seed_permissions! ⇒ Object
Seed permissions into database
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/easy_admin/permissions/resource_permissions.rb', line 76 def = created_count = 0 .each do || = EasyAdmin::Permissions::Permission.find_or_create_by( name: [:name] ) do |p| p.resource_type = [:resource_type] p.action = [:action] p.description = [:description] end created_count += 1 if .saved_change_to_id? end .size end |