class ActionAdmin::Header
Public Class Methods
new()
click to toggle source
# File lib/action_admin/header.rb, line 7 def initialize self.actions = {} self.active_links = {} end
Public Instance Methods
action(names)
click to toggle source
# File lib/action_admin/header.rb, line 12 def action(names) self.current_actions = Array(names) end
action_links(name, context)
click to toggle source
# File lib/action_admin/header.rb, line 33 def action_links(name, context) active = self.active_links[:"#{name}"] links = Hash(actions[:"#{name}"]).fetch :links, default_action_links(name, context) links = links.select { |k, _v| k.in? active } if active.is_a? Array links = links.values if links.is_a? Hash Array(links).reject(&:blank?).map do |link| Hash[link.map { |k, v| [k, evaluate_value(v, context)] }] end end
action_title(name, context)
click to toggle source
# File lib/action_admin/header.rb, line 28 def action_title(name, context) title = Hash(actions[:"#{name}"]).fetch :title, default_title(context) evaluate_value(title, context) end
default_action_links(name, context)
click to toggle source
# File lib/action_admin/header.rb, line 55 def default_action_links(name, context) setup = { index: :new, new: :index, show: [:index, :app, :edit, :destroy], edit: [:index, :app, :show, :destroy] } links = default_links(context) Hash[Array(setup[:"#{name}"]).map { |l| [l, links[l]] }.reject(&:nil?)] end
default_links(context)
click to toggle source
# File lib/action_admin/header.rb, line 62 def default_links(context) return {} unless context.controller.respond_to? :permitted_params show = -> { method(ActionAdmin.config.app_urls).call(current_record) rescue nil } { app: { label: 'Web', icon: 'web', url: show, html: { class: 'info', target: :_blank } }, # show: { label: 'View', icon: 'eye', url: :record_path, html: { class: 'success' } }, index: { label: 'Back', icon: 'arrow-left', url: :records_path, html: { class: 'secondary' } }, new: { label: 'New', icon: 'plus', url: :new_record_path, html: { class: 'success' } }, edit: { label: 'Edit', icon: 'pencil', url: :edit_record_path, html: { class: 'warning' } }, destroy: { label: 'Delete', icon: 'delete', url: :record_path, html: { class: 'alert' }, method: 'delete' } } end
default_title(context)
click to toggle source
# File lib/action_admin/header.rb, line 44 def default_title(context) singular = context.controller.try(:instance_name) plural = context.controller.try(:collection_name) if context.action_name == 'index' "#{plural || context.controller_name}".strip.titleize else "#{context.action_name} #{singular}".strip.titleize end end
link(options)
click to toggle source
# File lib/action_admin/header.rb, line 20 def link(options) current_actions.each { |a| add_action_key(a, :links, options, true) } end
links(names)
click to toggle source
# File lib/action_admin/header.rb, line 24 def links(names) current_actions.each { |a| self.active_links[a] = Array(names) } end
title(value)
click to toggle source
# File lib/action_admin/header.rb, line 16 def title(value) current_actions.each { |a| add_action_key(a, :title, value) } end
Private Instance Methods
add_action_key(action, key, value, append=false)
click to toggle source
# File lib/action_admin/header.rb, line 79 def add_action_key(action, key, value, append=false) self.actions[action] = {} unless self.actions.key?(action) if append.present? self.actions[action][key] = Array(self.actions[action][key]) self.actions[action][key] += [value] else self.actions[action][key] = value end end
evaluate_value(value, context)
click to toggle source
# File lib/action_admin/header.rb, line 90 def evaluate_value(value, context) if value.is_a?(Proc) context.instance_exec(&value) elsif value.is_a?(Symbol) context.try(value) || value else value end end