Method: EffectiveResourcesHelper#render_resource_actions

Defined in:
app/helpers/effective_resources_helper.rb

#render_resource_actions(resource, atts = {}, &block) ⇒ Object

partial: :dropleft|:glyphicons|string locals: {} render locals you can also pass all action names and true/false such as edit: true, show: false



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/effective_resources_helper.rb', line 61

def render_resource_actions(resource, atts = {}, &block)
  unless resource.kind_of?(ActiveRecord::Base) || resource.kind_of?(Class) || resource.kind_of?(Array) || resource.class.ancestors.include?(ActiveModel::Model)
    raise 'expected first argument to be an ActiveRecord::Base object or Array of objects'
  end

  raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash)

  btn_class = atts[:btn_class]
  effective_resource = atts[:effective_resource]
  namespace = atts[:controller_namespace] || atts[:namespace]
  locals = atts[:locals] || {}
  partial = atts[:partial]
  spacer_template = locals[:spacer_template]

  effective_resource ||= find_effective_resource(resource)
  namespace ||= (effective_resource.namespace.to_sym if effective_resource.namespace)

  # Assign actions
  # We filter out any actions passed to us that aren't supported
  actions = if atts.key?(:actions)
    {}.tap do |actions|
      atts[:actions].each do |commit, opts|
        actions[commit] = opts if (effective_resource.actions.include?(opts[:action]) || opts[:path]).present?
      end
    end
  else
    (resource.kind_of?(Class) ? effective_resource.resource_klass_actions : effective_resource.resource_actions)
  end

  # Consider only, except, false and proc false
  only = Array(atts[:only]) if atts[:only].present?
  except = Array(atts[:except]) if atts[:except].present?

  actions.select! do |_, opts|
    action = opts[:action]

    if only.present? && !only.include?(action)
      false
    elsif except.present? && except.include?(action)
      false
    elsif atts[action].respond_to?(:call)
      Effective::ResourceExec.new(self, resource).instance_exec(&atts[action])
    else
      atts[action] != false
    end
  end

  # Select Partial
  partial = if partial.kind_of?(Symbol)
    "effective/resource/actions_#{partial}.html"
  else
    "#{partial.presence || 'effective/resource/actions'}.html"
  end

  # Assign Locals
  locals = {
    resource: resource,
    effective_resource: effective_resource,
    format_block: (block if block_given?),
    namespace: namespace,
    actions: actions,
    btn_class: (btn_class || '')
  }.compact.merge(locals)

  if resource.kind_of?(Array)
    render(partial: partial, collection: resource, as: :resource, locals: locals.except(:resource), spacer_template: spacer_template)
  else
    render(partial, locals)
  end
end