Class: Model::Tpl::ResourcifyTpl

Inherits:
Struct
  • Object
show all
Defined in:
lib/resourcify/model/tpl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#model_classObject

Returns the value of attribute model_class

Returns:

  • (Object)

    the current value of model_class



3
4
5
# File 'lib/resourcify/model/tpl.rb', line 3

def model_class
  @model_class
end

Instance Method Details

#columnsObject



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

#filtersObject



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_columnsObject



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_columnsObject



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

#lookupsObject



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

#optionsObject



74
75
76
77
78
79
80
81
# File 'lib/resourcify/model/tpl.rb', line 74

def options
  if _tpl = method_exists?('options')
    return _tpl.new.options
  end
  {}
rescue
  {}
end