require 'active_support' module Forcast module Application module Meta extend ActiveSupport::Concern included do end class MetaRelation attr_accessor :class_name attr_accessor :belongs_to attr_accessor :has_many 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 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 def egear_load(model) model.includes(self.belongs_to + self.has_many) end # def meta_child(model) # {hasChild: [{ relatedTo: "plants", childResource: "edificio"}]} # end end end end end