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']
    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
# File 'app/presenters/action_admin/presenter.rb', line 63

def fields
  self.record_fields
end

#panelsObject



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)
  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



67
68
69
70
71
72
73
74
75
# File 'app/presenters/action_admin/presenter.rb', line 67

def render_field(form, field, options={})
  association = options[:association]

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

    opts[:label] = false if options[:labels] == false
    opts[:label] = true  if Array(options[:labels]).include?(f)

    render_field(form, f, opts)
  end

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

  @context.render template, options
end

#render_panels(options = {}) ⇒ Object



106
107
108
109
110
111
# File 'app/presenters/action_admin/presenter.rb', line 106

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

#sorted_panelsObject



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