Module: Ooor::ReflectionOoor::ClassMethods

Defined in:
lib/ooor/reflection_ooor.rb

Instance Method Summary collapse

Instance Method Details

#column_for_attribute(name) ⇒ Object



21
22
23
# File 'lib/ooor/reflection_ooor.rb', line 21

def column_for_attribute(name)
  columns_hash[name.to_s]
end

#create_reflection(name) ⇒ 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/ooor/reflection_ooor.rb', line 25

def create_reflection(name)
  options = {}
  if many2one_associations.keys.include?(name)
    macro = :belongs_to
    relation = many2one_associations[name]['relation'] #TODO prefix?
    const_get(relation)
    options[:class_name] = relation #TODO or pass it camelized already?
  elsif many2many_associations.keys.include?(name)
    macro = :has_and_belongs_to_many
  elsif one2many_associations.keys.include?(name)
    macro = :has_many
  end
  reflection = Reflection::AssociationReflection.new(macro, name, options, nil)#active_record) #TODO active_record?
#        case macro
#          when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
#            klass = options[:through] ? ThroughReflection : AssociationReflection
#            reflection = klass.new(macro, name, options, active_record)
#          when :composed_of
#            reflection = AggregateReflection.new(macro, name, options, active_record)
#        end

  self.reflections = self.reflections.merge(name => reflection)
  reflection
end

#reflect_on_association(association) ⇒ Object



50
51
52
53
54
# File 'lib/ooor/reflection_ooor.rb', line 50

def reflect_on_association(association)
  reflections[association] ||= create_reflection(association.to_s).tap do |reflection|
    reflection.connection = connection
  end
end

#set_columns_hash(view_fields = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/ooor/reflection_ooor.rb', line 10

def set_columns_hash(view_fields={})
  reload_fields_definition()
  @t.columns_hash ||= {}
  @t.fields.each do |k, field|
    unless @t.associations_keys.index(k)
      @t.columns_hash[k] = field.merge({type: to_rails_type(view_fields[k] && view_fields[k]['type'] || field['type'])})
    end
  end
  @t.columns_hash
end