Module: Snaptable::Constructor::Renderer
- Included in:
- BaseTable
- Defined in:
- lib/snaptable/constructor/renderer.rb
Instance Method Summary collapse
- #attributes ⇒ Object
- #column_names ⇒ Object
- #enums(model) ⇒ Object
- #format(attribute, attr_value, attr_model) ⇒ Object
- #present(buttons: nil) ⇒ Object
- #respond ⇒ Object
- #string_to_class(string) ⇒ Object
- #values(element) ⇒ Object
Instance Method Details
#attributes ⇒ Object
19 20 21 |
# File 'lib/snaptable/constructor/renderer.rb', line 19 def attributes model.attribute_names end |
#column_names ⇒ Object
23 24 25 26 27 |
# File 'lib/snaptable/constructor/renderer.rb', line 23 def column_names attributes.map do |a| (a.is_a?(Hash) ? a.keys[0] : a).to_s end end |
#enums(model) ⇒ Object
54 55 56 |
# File 'lib/snaptable/constructor/renderer.rb', line 54 def enums(model) model.defined_enums.keys end |
#format(attribute, attr_value, attr_model) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/snaptable/constructor/renderer.rb', line 42 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.(attr_value.to_s) attr_value = view_context.truncate(attr_value, length: 40) unless [: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) = || "snaptable/buttons" render_to_string('/snaptable/base', layout: false, locals: { presenter: self }).html_safe end |
#respond ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/snaptable/constructor/renderer.rb', line 10 def respond respond_to do |format| format.html format.js do render '/snaptable/sort', locals: { content: self.present(buttons: params[:buttons]) } end end end |
#string_to_class(string) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/snaptable/constructor/renderer.rb', line 58 def string_to_class(string) begin return string.classify.constantize rescue NameError return nil end end |
#values(element) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/snaptable/constructor/renderer.rb', line 29 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 |