Method: Olivander::ApplicationHelper#authorized_resource_actions

Defined in:
app/helpers/olivander/application_helper.rb

#authorized_resource_actions(resource, for_action: :show) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/olivander/application_helper.rb', line 38

def authorized_resource_actions(resource, for_action: :show)
  action_klass = action_klass_for(resource)
  action_resource = action_resource_for(resource)
  raw_name = action_klass.name
  plural_name = raw_name.demodulize.underscore.pluralize
  routed_resource = Olivander::CurrentContext.application_context.route_builder.resources[plural_name.to_sym]
  return [] if routed_resource.nil?

  actions = if resource.is_a?(Class)
              routed_resource.unpersisted_crud_actions | routed_resource.collection_actions.reject(&:crud_action)
            elsif resource.persisted?
              routed_resource.persisted_crud_actions | routed_resource.member_actions.reject(&:crud_action)
            else
              []
            end
  actions = actions.select { |a| authorized_resource_action?(a.sym, action_resource, for_action) }
  preferred = %i[show edit destroy]
  [].tap do |arr|
    preferred.each do |p|
      actions.each do |a|
        arr << a if a.sym == p
      end
    end
    actions.reject { |x| preferred.include?(x.sym) }.each do |a|
      arr << a
    end
  end
end