Module: ActiveRecord::NinjaModelExtensions::ReflectionExt::ClassMethods

Defined in:
lib/ninja_model/rails_ext/active_record.rb

Instance Method Summary collapse

Instance Method Details

#create_reflection(macro, name, options, active_record) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ninja_model/rails_ext/active_record.rb', line 7

def create_reflection(macro, name, options, active_record)
  klass = options[:class_name] || name
  klass = klass.to_s.camelize
  klass = klass.singularize if macro.in?([:has_many])
  klass = compute_type(klass)
  if NinjaModel.ninja_model?(klass)
    case macro
    when :has_many, :belongs_to, :has_one
      reflection = NinjaModel::Reflection::AssociationReflection.new(macro, name, options, active_record)
    else
      raise NotImplementedError, "NinjaModel does not currently support #{macro} associations."
    end
    self.reflections = self.reflections.merge(name => reflection)
    reflection
  else
    super
  end
end

#reflect_on_aggregation(aggregation) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ninja_model/rails_ext/active_record.rb', line 26

def reflect_on_aggregation(aggregation)
  if reflections[aggregation].is_a?(NinjaModel::Reflection::AggregateReflection)
    reflections[aggregation]
  else
    super
  end
end

#reflect_on_association(association) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ninja_model/rails_ext/active_record.rb', line 34

def reflect_on_association(association)
  if reflections[association].is_a?(NinjaModel::Reflection::AssociationReflection)
    reflections[association]
  else
    super
  end
end