Module: NinjaModel::ActiveRecordExtensions::ReflectionExt::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ninja_model/rails_ext/active_record.rb', line 52

def create_reflection(macro, name, options, ninja_model)
  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)
    super
  else
    case macro
    when :has_many, :belongs_to, :has_one
      reflection = ActiveRecord::Reflection::AssociationReflection.new(macro, name, options, ninja_model)
    when :composed_of
      reflection = ActiveRecord::Reflection::AggregateReflection.new(macro, name, options, ninja_model)
    else
      raise NotImplementedError, "NinjaModel does not currently support #{macro} associations."
    end
    self.reflections = self.reflections.merge(name => reflection)
  end
end

#reflect_on_aggregation(aggregation) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/ninja_model/rails_ext/active_record.rb', line 72

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

#reflect_on_association(association) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/ninja_model/rails_ext/active_record.rb', line 80

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