Permalink
Browse files

Associations - where possible, call attributes methods rather than di…

…rectly accessing the instance variables
  • Loading branch information...
1 parent f826e05 commit 1d85a73cebeea26a3ec384db1c110f6b79d9cda2 @jonleighton jonleighton committed with tenderlove Feb 20, 2011
@@ -37,13 +37,13 @@ def initialize(owner, reflection)
# post.comments.aliased_table_name # => "comments"
#
def aliased_table_name
- @reflection.klass.table_name
+ reflection.klass.table_name
end
# Resets the \loaded flag to +false+ and sets the \target to +nil+.
def reset
@loaded = false
- IdentityMap.remove(@target) if IdentityMap.enabled? && @target
+ IdentityMap.remove(target) if IdentityMap.enabled? && target
@target = nil
end
@@ -52,7 +52,7 @@ def reload
reset
construct_scope
load_target
- self unless @target.nil?
+ self unless target.nil?
end
# Has the \target been already \loaded?
@@ -99,12 +99,12 @@ def construct_scope
def association_scope
scope = target_klass.unscoped
scope = scope.create_with(creation_attributes)
- scope = scope.apply_finder_options(@reflection.options.slice(:readonly, :include))
- scope = scope.where(interpolate(@reflection.options[:conditions]))
+ scope = scope.apply_finder_options(reflection.options.slice(:readonly, :include))
+ scope = scope.where(interpolate(reflection.options[:conditions]))
if select = select_value
scope = scope.select(select)
end
- scope = scope.extending(*Array.wrap(@reflection.options[:extend]))
+ scope = scope.extending(*Array.wrap(reflection.options[:extend]))
scope.where(construct_owner_conditions)
end
@@ -116,14 +116,14 @@ def aliased_table
def set_inverse_instance(record)
if record && invertible_for?(record)
inverse = record.association(inverse_reflection_for(record).name)
- inverse.target = @owner
+ inverse.target = owner
end
end
# This class of the target. belongs_to polymorphic overrides this to look at the
# polymorphic_type field on the owner.
def target_klass
- @reflection.klass
+ reflection.klass
end
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
@@ -146,7 +146,7 @@ def load_target
if find_target?
begin
if IdentityMap.enabled? && association_class && association_class.respond_to?(:base_class)
- @target = IdentityMap.get(association_class, @owner[@reflection.foreign_key])
+ @target = IdentityMap.get(association_class, owner[reflection.foreign_key])
end
rescue NameError
nil
@@ -163,19 +163,19 @@ def load_target
private
def find_target?
- !loaded? && (!@owner.new_record? || foreign_key_present?) && target_klass
+ !loaded? && (!owner.new_record? || foreign_key_present?) && target_klass
end
def interpolate(sql, record = nil)
if sql.respond_to?(:to_proc)
- @owner.send(:instance_exec, record, &sql)
+ owner.send(:instance_exec, record, &sql)
else
sql
end
end
def select_value
- @reflection.options[:select]
+ reflection.options[:select]
end
# Implemented by (some) subclasses
@@ -184,22 +184,22 @@ def creation_attributes
end
# Returns a hash linking the owner to the association represented by the reflection
- def construct_owner_attributes(reflection = @reflection)
+ def construct_owner_attributes(reflection = reflection)
attributes = {}
if reflection.macro == :belongs_to
- attributes[reflection.association_primary_key] = @owner[reflection.foreign_key]
+ attributes[reflection.association_primary_key] = owner[reflection.foreign_key]
else
- attributes[reflection.foreign_key] = @owner[reflection.active_record_primary_key]
+ attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
if reflection.options[:as]
- attributes["#{reflection.options[:as]}_type"] = @owner.class.base_class.name
+ attributes["#{reflection.options[:as]}_type"] = owner.class.base_class.name
end
end
attributes
end
# Builds an array of arel nodes from the owner attributes hash
- def construct_owner_conditions(table = aliased_table, reflection = @reflection)
+ def construct_owner_conditions(table = aliased_table, reflection = reflection)
conditions = construct_owner_attributes(reflection).map do |attr, value|
table[attr].eq(value)
end
@@ -208,14 +208,14 @@ def construct_owner_conditions(table = aliased_table, reflection = @reflection)
# Sets the owner attributes on the given record
def set_owner_attributes(record)
- if @owner.persisted?
+ if owner.persisted?
construct_owner_attributes.each { |key, value| record[key] = value }
end
end
- # Should be true if there is a foreign key present on the @owner which
+ # Should be true if there is a foreign key present on the owner which
# references the target. This is used to determine whether we can load
- # the target if the @owner is currently a new record (and therefore
+ # the target if the owner is currently a new record (and therefore
# without a key).
#
# Currently implemented by belongs_to (vanilla and polymorphic) and
@@ -228,8 +228,8 @@ def foreign_key_present?
# the kind of the class of the associated objects. Meant to be used as
# a sanity check when you are about to assign an associated record.
def raise_on_type_mismatch(record)
- unless record.is_a?(@reflection.klass) || record.is_a?(@reflection.class_name.constantize)
- message = "#{@reflection.class_name}(##{@reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})"
+ unless record.is_a?(reflection.klass) || record.is_a?(reflection.class_name.constantize)
+ message = "#{reflection.class_name}(##{reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})"
raise ActiveRecord::AssociationTypeMismatch, message
end
end
@@ -238,7 +238,7 @@ def raise_on_type_mismatch(record)
# The record parameter is necessary to support polymorphic inverses as we must check for
# the association in the specific class of the record.
def inverse_reflection_for(record)
- @reflection.inverse_of
+ reflection.inverse_of
end
# Is this association invertible? Can be redefined by subclasses.
@@ -21,9 +21,9 @@ def updated?
private
def update_counters(record)
- counter_cache_name = @reflection.counter_cache_column
+ counter_cache_name = reflection.counter_cache_column
- if counter_cache_name && @owner.persisted? && different_target?(record)
+ if counter_cache_name && owner.persisted? && different_target?(record)
if record
record.class.increment_counter(counter_cache_name, record.id)
end
@@ -36,16 +36,16 @@ def update_counters(record)
# Checks whether record is different to the current target, without loading it
def different_target?(record)
- record.nil? && @owner[@reflection.foreign_key] ||
- record.id != @owner[@reflection.foreign_key]
+ record.nil? && owner[reflection.foreign_key] ||
+ record.id != owner[reflection.foreign_key]
end
def replace_keys(record)
- @owner[@reflection.foreign_key] = record && record[@reflection.association_primary_key]
+ owner[reflection.foreign_key] = record && record[reflection.association_primary_key]
end
def foreign_key_present?
- @owner[@reflection.foreign_key]
+ owner[reflection.foreign_key]
end
# NOTE - for now, we're only supporting inverse setting from belongs_to back onto
@@ -56,15 +56,15 @@ def invertible_for?(record)
end
def target_id
- if @reflection.options[:primary_key]
- @owner.send(@reflection.name).try(:id)
+ if reflection.options[:primary_key]
+ owner.send(reflection.name).try(:id)
else
- @owner[@reflection.foreign_key]
+ owner[reflection.foreign_key]
end
end
def stale_state
- @owner[@reflection.foreign_key].to_s
+ owner[reflection.foreign_key].to_s
end
end
end
@@ -6,19 +6,19 @@ class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
def replace_keys(record)
super
- @owner[@reflection.foreign_type] = record && record.class.base_class.name
+ owner[reflection.foreign_type] = record && record.class.base_class.name
end
def different_target?(record)
super || record.class != target_klass
end
def inverse_reflection_for(record)
- @reflection.polymorphic_inverse_of(record.class)
+ reflection.polymorphic_inverse_of(record.class)
end
def target_klass
- type = @owner[@reflection.foreign_type]
+ type = owner[reflection.foreign_type]
type && type.constantize
end
@@ -27,7 +27,7 @@ def raise_on_type_mismatch(record)
end
def stale_state
- [super, @owner[@reflection.foreign_type].to_s]
+ [super, owner[reflection.foreign_type].to_s]
end
end
end
Oops, something went wrong.

0 comments on commit 1d85a73

Please sign in to comment.