Class: ActionAdmin::Presenter

Inherits:
Object
  • Object
show all
Includes:
Presentable
Defined in:
app/presenters/action_admin/presenter.rb

Instance Method Summary collapse

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



54
55
56
57
58
59
60
61
# File 'app/presenters/action_admin/presenter.rb', line 54

def action_links(*args)
  options = args.extract_options!
  wrapper = args.first

  @context. wrapper, options do
    @context.admin_table_action_links(@record)
  end
end

#attribute_labelsObject



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_namesObject



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

#attributesObject



11
12
13
# File 'app/presenters/action_admin/presenter.rb', line 11

def attributes
  self.record_attributes
end

#fieldsObject



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

#panelsObject



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, options = {}) ⇒ Object



30
31
32
# File 'app/presenters/action_admin/presenter.rb', line 30

def render_attribute(name, options={})
  @context.simple_attribute_for(@record, name, options)
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)
  options = args.extract_options!
  wrapper = args.first
  attribs = attribute_names.map do |a|
    @context.(wrapper, render_attribute(a), options)
  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)
  options = args.extract_options!
  wrapper = args.first
  attribs = attribute_labels.map do |a|
    @context.(wrapper, a, options)
  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, options={})
  options     = Hash(options)
  association = options[:association]

  if association.present?
    form.association field, options.except(:association)
  else
    form.input field, options
  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, options={})
  template = "admin/panels/#{options.fetch :template, 'default'}"
  content  = Array(options[:fields]).map { |f| render_panel_field(form, f, options) }
  footer   = Array(Hash(options[:footer])[:fields]).map { |f| render_panel_field(form, f, options) }.join.html_safe
  footer   = nil if footer.blank?

  options = {
    layout:  false,
    content: content.join.html_safe,
    title:   options[:title],
    footer:  footer
  }

  @context.render template, options
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, options)
  opts = fields[field]

  opts[:label] = false if options[:labels] == false
  opts[:label] = true  if Array(options[: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(options={})
  form    = options[:form]
  context = options[:context]

  panels_for_context(context).map { |_i, o| render_panel(form, o) }.join.html_safe
end

#render_table_columns(table) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'app/presenters/action_admin/presenter.rb', line 154

def render_table_columns(table)
  table = self.record_tables[table]
  cells = []

  table.columns.each do |k, v|
    options = v.fetch(:attribute, {})
    cells << @context.(:td, render_attribute(k, options), v[:html])
  end

  cells.join.html_safe
end

#render_table_header(table) ⇒ Object

def render_table(table)

table = self.record_tables[table]

end



143
144
145
146
147
148
149
150
151
152
# File 'app/presenters/action_admin/presenter.rb', line 143

def render_table_header(table)
  table = self.record_tables[table]
  cells = []

  table.header.each do |_k, v|
    cells << @context.(:th, v[:label], v[:html])
  end

  cells.join.html_safe
end

#sorted_panelsObject



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