Class: ActionAdmin::Presenter
- Inherits:
-
Object
- Object
- ActionAdmin::Presenter
- Includes:
- Presentable
- Defined in:
- app/presenters/action_admin/presenter.rb
Instance Method Summary collapse
- #action_links(*args) ⇒ Object
- #attribute_labels ⇒ Object
- #attribute_names ⇒ Object
- #attributes ⇒ Object
- #fields ⇒ Object
-
#initialize(record, context) ⇒ Presenter
constructor
A new instance of Presenter.
- #panels ⇒ Object
- #panels_for_context(context = nil) ⇒ Object
- #render_attribute(name) ⇒ Object
- #render_attributes(*args) ⇒ Object
- #render_attributes_labels(*args) ⇒ Object
- #render_field(form, field, options = {}) ⇒ Object
- #render_fields(form) ⇒ Object
- #render_panel(form, options = {}) ⇒ Object
- #render_panel_field(form, field, options) ⇒ Object
- #render_panels(options = {}) ⇒ Object
- #sorted_panels ⇒ Object
Constructor Details
#initialize(record, context) ⇒ Presenter
Returns a new instance of Presenter.
5 6 7 8 9 |
# File 'app/presenters/action_admin/presenter.rb', line 5 def initialize(record, context) @model = record.class @record = record @context = context end |
Instance Method Details
#action_links(*args) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'app/presenters/action_admin/presenter.rb', line 54 def action_links(*args) = args. wrapper = args.first @context.content_tag wrapper, do @context.admin_table_action_links(@record) end end |
#attribute_labels ⇒ Object
26 27 28 |
# File 'app/presenters/action_admin/presenter.rb', line 26 def attribute_labels attribute_names.map { |i| @model.human_attribute_name(i) } end |
#attribute_names ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'app/presenters/action_admin/presenter.rb', line 15 def attribute_names if attributes.keys.any? attributes.keys else items = ['title', 'name', 'email', 'id', 'var'] names = @record.class.attribute_names.select { |i| i.in? items } names.sort_by { |i| items.index i }.first(1) end end |
#attributes ⇒ Object
11 12 13 |
# File 'app/presenters/action_admin/presenter.rb', line 11 def attributes self.record_attributes end |
#fields ⇒ Object
63 64 65 66 67 68 69 |
# File 'app/presenters/action_admin/presenter.rb', line 63 def fields if self.record_fields.any? self.record_fields else Hash[@record.permitted_attributes.map { |e| [:"#{e}", {}] }] end end |
#panels ⇒ Object
86 87 88 89 90 91 92 |
# File 'app/presenters/action_admin/presenter.rb', line 86 def panels if self.record_panels.any? self.record_panels else { attributes: { title: 'Attributes', fields: fields.keys } } end end |
#panels_for_context(context = nil) ⇒ Object
131 132 133 134 135 136 137 |
# File 'app/presenters/action_admin/presenter.rb', line 131 def panels_for_context(context=nil) if context.blank? sorted_panels.select { |_i, o| o[:context].blank? } else sorted_panels.select { |_i, o| o[:context] == context } end end |
#render_attribute(name) ⇒ Object
30 31 32 |
# File 'app/presenters/action_admin/presenter.rb', line 30 def render_attribute(name) @context.simple_attribute_for(@record, name, Hash(attributes[name])) end |
#render_attributes(*args) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'app/presenters/action_admin/presenter.rb', line 34 def render_attributes(*args) = args. wrapper = args.first attribs = attribute_names.map do |a| @context.content_tag(wrapper, render_attribute(a), ) end attribs.join.html_safe end |
#render_attributes_labels(*args) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'app/presenters/action_admin/presenter.rb', line 44 def render_attributes_labels(*args) = args. wrapper = args.first attribs = attribute_labels.map do |a| @context.content_tag(wrapper, a, ) end attribs.join.html_safe end |
#render_field(form, field, options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'app/presenters/action_admin/presenter.rb', line 71 def render_field(form, field, ={}) = Hash() association = [:association] if association.present? form.association field, .except(:association) else form.input field, end end |
#render_fields(form) ⇒ Object
82 83 84 |
# File 'app/presenters/action_admin/presenter.rb', line 82 def render_fields(form) fields.map { |f, o| render_field(form, f, o) }.join.html_safe end |
#render_panel(form, options = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/presenters/action_admin/presenter.rb', line 94 def render_panel(form, ={}) template = "admin/panels/#{.fetch :template, 'default'}" content = Array([:fields]).map { |f| render_panel_field(form, f, ) } = Array(Hash([:footer])[:fields]).map { |f| render_panel_field(form, f, ) }.join.html_safe = nil if .blank? = { layout: false, content: content.join.html_safe, title: [:title], footer: } @context.render template, end |
#render_panel_field(form, field, options) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'app/presenters/action_admin/presenter.rb', line 110 def render_panel_field(form, field, ) opts = fields[field] opts[:label] = false if [:labels] == false opts[:label] = true if Array([:labels]).include?(field) render_field(form, field, opts) end |
#render_panels(options = {}) ⇒ Object
119 120 121 122 123 124 |
# File 'app/presenters/action_admin/presenter.rb', line 119 def render_panels(={}) form = [:form] context = [:context] panels_for_context(context).map { |_i, o| render_panel(form, o) }.join.html_safe end |
#sorted_panels ⇒ Object
126 127 128 129 |
# File 'app/presenters/action_admin/presenter.rb', line 126 def sorted_panels items = [:high, :medium, :low] panels.sort_by { |_i, o| [items.index(o[:priority]).to_i, o[:order].to_i] } end |