Class: Model::Tpl::ResourcifyTpl
- Inherits:
-
Struct
- Object
- Struct
- Model::Tpl::ResourcifyTpl
- Defined in:
- lib/resourcify/model/tpl.rb
Instance Attribute Summary collapse
-
#model_class ⇒ Object
Returns the value of attribute model_class.
Instance Method Summary collapse
- #columns ⇒ Object
- #filters ⇒ Object
- #form_columns ⇒ Object
- #grid_columns ⇒ Object
- #lookups ⇒ Object
- #options ⇒ Object
Instance Attribute Details
#model_class ⇒ Object
Returns the value of attribute model_class
3 4 5 |
# File 'lib/resourcify/model/tpl.rb', line 3 def model_class @model_class end |
Instance Method Details
#columns ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/resourcify/model/tpl.rb', line 5 def columns fields = [] foreign_keys = self.model_class.reflections.each_with_object({}) {|(k, v), h| h[v.foreign_key] = v.name.to_s } self.model_class.columns.each do |c| f = { name: c.name, type: c.type.to_s, label: c.name.titleize } if foreign_keys.keys.include?(c.name) f[:lookup] = foreign_keys[c.name] f[:label] = c.name[0, c.name.length - 3].titleize if c.name.ends_with?("_id") if foreign_keys[c.name] == 'children' f[:lookup] = :parent f[:label] = "Parent #{self.model_class.name.split('::').last.singularize.titleize}" end end fields.push f end fields rescue Exception => e [] end |
#filters ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/resourcify/model/tpl.rb', line 83 def filters if _tpl = method_exists?('filters') return _tpl.new.filters end {} rescue {} end |
#form_columns ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/resourcify/model/tpl.rb', line 50 def form_columns if _tpl = method_exists?('form_columns') return _tpl.new.form_columns end excluded_fields = ["id", "created_at", "updated_at", "lft", "rgt", "depth"] fields = self.columns.select { |e| !excluded_fields.include? e[:name] } rescue Exception => e [] end |
#grid_columns ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/resourcify/model/tpl.rb', line 62 def grid_columns if _tpl = method_exists?('grid_columns') return _tpl.new.grid_columns end excluded_fields = ["id", "created_at", "updated_at", "lft", "rgt", "depth"] fields = self.columns.select { |e| !excluded_fields.include? e[:name] } rescue Exception => e [] end |
#lookups ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/resourcify/model/tpl.rb', line 27 def lookups if _tpl = method_exists?('lookups') return _tpl.new.lookups end lookups = {} model_class.reflect_on_all_associations(:belongs_to).each do |association| lookups[association.name] = association.klass.all.map { |e| {id: e.id, name: e.name} } lookups[association.name].unshift({ id: nil, name: '' }) # if association.name == :parent # lookups[association.name] = association.klass.all # lookups[association.name].unshift(association.klass.new(id: nil, name: 'N/A')) # else # lookups[association.name] = association.klass.all.map { |e| {id: e.id, name: e.name} } # end end lookups rescue Exception => e {} end |
#options ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/resourcify/model/tpl.rb', line 74 def if _tpl = method_exists?('options') return _tpl.new. end {} rescue {} end |