Module: Snaptable::Constructor::Renderer

Included in:
BaseTable
Defined in:
lib/snaptable/constructor/renderer.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



17
18
19
# File 'lib/snaptable/constructor/renderer.rb', line 17

def attributes
  model.attribute_names
end

#column_namesObject



21
22
23
24
25
# File 'lib/snaptable/constructor/renderer.rb', line 21

def column_names
  attributes.map do |a|
    (a.is_a?(Hash) ? a.keys[0] : a).to_s
  end
end

#enumsObject



48
49
50
# File 'lib/snaptable/constructor/renderer.rb', line 48

def enums
  model.defined_enums.keys
end

#format(attribute, attr_value) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/snaptable/constructor/renderer.rb', line 38

def format(attribute, attr_value)
  if attr_value.is_a?(Date) || attr_value.is_a?(Time) || attr_value.is_a?(DateTime)
    l attr_value, format: :snaptable
  elsif attribute.to_s.in? enums
    t "#{model.model_name.i18n_key}.#{attribute.to_s.pluralize}.#{attr_value}"
  else
    attr_value
  end.to_s
end

#present(buttons: nil) ⇒ Object



5
6
7
8
# File 'lib/snaptable/constructor/renderer.rb', line 5

def present(buttons: nil)
  @buttons = buttons || "snaptable/buttons"
  render_to_string('/snaptable/base', layout: false, locals: { presenter: self }).html_safe
end

#respondObject



10
11
12
13
14
15
# File 'lib/snaptable/constructor/renderer.rb', line 10

def respond
  respond_to do |format|
    format.html
    format.js { render '/snaptable/sort' }
  end
end

#values(element) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/snaptable/constructor/renderer.rb', line 27

def values(element)
  attributes.map do |attribute|
    attr_value = if attribute.is_a?(Symbol) || attribute.is_a?(String)
      element.send(attribute)
    else
      element.send(*attribute.keys).send(*attribute.values)
    end
    format(attribute, attr_value)
  end
end