Module: Snaptable::Constructor::Renderer

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

Instance Method Summary collapse

Instance Method Details

#attributesObject



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

def attributes
  model.attribute_names
end

#column_namesObject



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

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

#enums(model) ⇒ Object



51
52
53
# File 'lib/snaptable/constructor/renderer.rb', line 51

def enums(model)
  model.defined_enums.keys
end

#format(attribute, attr_value, attr_model) ⇒ Object



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

def format(attribute, attr_value, attr_model)
  attr_model ||= model
  if attr_value.is_a?(Date) || attr_value.is_a?(Time) || attr_value.is_a?(DateTime)
    attr_value = l(attr_value, format: :snaptable)
  elsif !attr_value.nil? && attribute.to_s.in?(enums(attr_model))
    attr_value = t("#{attr_model.model_name.i18n_key}.#{attribute.to_s.pluralize}.#{attr_value}")
  end
  attr_value = view_context.strip_tags(attr_value.to_s)
  attr_value = view_context.truncate(attr_value, length: 40) unless options[:truncate] == false
  return attr_value
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
# File 'lib/snaptable/constructor/renderer.rb', line 10

def respond
  respond_to do |format|
    format.html
  end
end

#string_to_class(string) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/snaptable/constructor/renderer.rb', line 55

def string_to_class(string)
  begin
    return string.classify.constantize
  rescue NameError
    return nil
  end
end

#values(element) ⇒ Object



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

def values(element)
  attributes.map do |attribute|
    if attribute.is_a?(Symbol) || attribute.is_a?(String)
      attr_value = element.send(attribute)
    else # a hash { model: :attribute }
      attr_value = element.send(attribute.keys[0]).send(attribute.values[0])
      attr_model = string_to_class(attribute.keys[0].to_s)
      attribute = attribute.values[0]
    end
    format(attribute, attr_value, attr_model)
  end
end