Skip to content

Commit

Permalink
Remove need for macro instance var
Browse files Browse the repository at this point in the history
Same as we did for collection, removed the `@macro` instance var
and it is now set in each association. Unfortunately it can't be
left undefined in AssociationReflection so it has to be set there.
For now I am setting it to NotImplementedError since there is no
default macro and it changes based on the reflection type.
  • Loading branch information
eileencodes committed Jul 17, 2014
1 parent b062d5d commit 8c263d5
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions activerecord/lib/active_record/reflection.rb
Expand Up @@ -177,12 +177,6 @@ class MacroReflection < AbstractReflection
# <tt>has_many :clients</tt> returns <tt>:clients</tt>
attr_reader :name

# Returns the macro type.
#
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>:composed_of</tt>
# <tt>has_many :clients</tt> returns <tt>:has_many</tt>
attr_reader :macro

attr_reader :scope

# Returns the hash of options used for the macro.
Expand Down Expand Up @@ -386,7 +380,7 @@ def scope_chain
scope ? [[scope]] : [[]]
end

alias :source_macro :macro
def source_macro; macro; end

def has_inverse?
inverse_name
Expand All @@ -408,6 +402,11 @@ def polymorphic_inverse_of(associated_class)
end
end

# Returns the macro type.
#
# <tt>has_many :clients</tt> returns <tt>:has_many</tt>
def macro; raise NotImplementedError; end

# Returns whether or not this association reflection is for a collection
# association. Returns +true+ if the +macro+ is either +has_many+ or
# +has_and_belongs_to_many+, +false+ otherwise.
Expand Down Expand Up @@ -578,35 +577,39 @@ def primary_key(klass)

class HasManyReflection < AssociationReflection #:nodoc:
def initialize(name, scope, options, active_record)
@macro = :has_many
super(name, scope, options, active_record)
end

def macro; :has_many; end

def collection?
true
end
end

class HasOneReflection < AssociationReflection #:nodoc:
def initialize(name, scope, options, active_record)
@macro = :has_one
super(name, scope, options, active_record)
end

def macro; :has_one; end
end

class BelongsToReflection < AssociationReflection #:nodoc:
def initialize(name, scope, options, active_record)
@macro = :belongs_to
super(name, scope, options, active_record)
end

def macro; :belongs_to; end
end

class HasAndBelongsToManyReflection < AssociationReflection #:nodoc:
def initialize(name, scope, options, active_record)
@macro = :has_and_belongs_to_many
super
end

def macro; :has_and_belongs_to_many; end

def collection?
true
end
Expand Down

0 comments on commit 8c263d5

Please sign in to comment.