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_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'] 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 |
# File 'app/presenters/action_admin/presenter.rb', line 63 def fields self.record_fields end |
#panels ⇒ Object
81 82 83 |
# File 'app/presenters/action_admin/presenter.rb', line 81 def panels self.record_panels end |
#panels_for_context(context = nil) ⇒ Object
118 119 120 121 122 123 124 |
# File 'app/presenters/action_admin/presenter.rb', line 118 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
67 68 69 70 71 72 73 74 75 |
# File 'app/presenters/action_admin/presenter.rb', line 67 def render_field(form, field, ={}) association = [:association] if association.present? form.association field, Hash().except(:association) else form.input field, Hash() end end |
#render_fields(form) ⇒ Object
77 78 79 |
# File 'app/presenters/action_admin/presenter.rb', line 77 def render_fields(form) fields.map { |f, o| render_field(form, f, 0) }.join.html_safe end |
#render_panel(form, options = {}) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/presenters/action_admin/presenter.rb', line 85 def render_panel(form, ={}) template = "admin/panels/#{options.fetch :template, 'default'}" content = Array([:fields]).map do |f| opts = fields[f] opts[:label] = false if [:labels] == false opts[:label] = true if Array([:labels]).include?(f) render_field(form, f, opts) end = { layout: false, content: content.join.html_safe, title: [:title], footer: [:footer] } @context.render template, end |
#render_panels(options = {}) ⇒ Object
106 107 108 109 110 111 |
# File 'app/presenters/action_admin/presenter.rb', line 106 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
113 114 115 116 |
# File 'app/presenters/action_admin/presenter.rb', line 113 def sorted_panels items = [:high, :medium, :low] panels.sort_by { |_i, o| [items.index(o[:priority]).to_i, o[:order].to_i] } end |