Class: Forcast::Application::Meta::MetaRelation

Inherits:
Object
  • Object
show all
Defined in:
lib/forcast/controllers/application/meta.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(e) ⇒ MetaRelation

Returns a new instance of MetaRelation.



18
19
20
21
22
23
# File 'lib/forcast/controllers/application/meta.rb', line 18

def initialize(e)
  self.class_name = e.to_s
  self.belongs_to = e.reflect_on_all_associations(:belongs_to).map { |e| e.name.to_s }
  self.has_many = e.reflect_on_all_associations(:has_many).map { |e| e.name.to_s }
  #e.includes(self.belongs_to + self.has_many)
end

Instance Attribute Details

#belongs_toObject

Returns the value of attribute belongs_to.



15
16
17
# File 'lib/forcast/controllers/application/meta.rb', line 15

def belongs_to
  @belongs_to
end

#class_nameObject

Returns the value of attribute class_name.



14
15
16
# File 'lib/forcast/controllers/application/meta.rb', line 14

def class_name
  @class_name
end

#has_manyObject

Returns the value of attribute has_many.



16
17
18
# File 'lib/forcast/controllers/application/meta.rb', line 16

def has_many
  @has_many
end

Instance Method Details

#egear_load(model) ⇒ Object



50
51
52
# File 'lib/forcast/controllers/application/meta.rb', line 50

def egear_load(model)
  model.includes(self.belongs_to + self.has_many)
end

#has_relation(model) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/forcast/controllers/application/meta.rb', line 25

def has_relation(model)
  rel = Hash.new
  rel[:has_relation] = {}
  rel[:has_relation][:belongs_to] = []
  rel[:has_relation][:has_many] = []
  self.belongs_to.each do |relation|
    object = model.send(relation)
    unless object.blank?
      sender = [object].map {|e| e}
      #object.is_a?(Array) ? sender = object.map {|e| e.as_json["id"]} : sender = [object].map {|e| e.as_json["id"]}
      rel[:has_relation][:belongs_to].push({related_to: relation.to_s.pluralize, label: relation.to_s.pluralize.capitalize, ids: sender})
    end
  end
  self.has_many.each do |relation|
    object = model.send(relation)
    ##Return 'Array' Coleection Proxy
    unless object.blank?
      sender = object.map {|e| e}
      #object.is_a?(Array) ? sender = object.map {|e| e.as_json["id"]} : sender = [object].map {|e| e.as_json["id"]}
      rel[:has_relation][:has_many].push({related_to: relation.to_s.pluralize, label: relation.to_s.pluralize.capitalize, ids: sender})
    end
  end
  return rel
end