Skip to content

Commit

Permalink
Merge pull request #15483 from eileencodes/reuse-available-reflection…
Browse files Browse the repository at this point in the history
…-polymorphic-methods

fix polymorphic? method and reuse it
  • Loading branch information
senny committed Jun 3, 2014
2 parents e2a97ad + 46acd8b commit b79e22c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
Expand Up @@ -106,7 +106,7 @@ def add_constraints(scope, owner, assoc_klass, refl, tracker)
table, foreign_table = tables.shift, tables.first

if reflection.source_macro == :belongs_to
if reflection.options[:polymorphic]
if reflection.polymorphic?
key = reflection.association_primary_key(assoc_klass)
else
key = reflection.association_primary_key
Expand Down
Expand Up @@ -217,7 +217,7 @@ def build(associations, base_klass)
reflection.check_validity!
reflection.check_eager_loadable!

if reflection.options[:polymorphic]
if reflection.polymorphic?
raise EagerLoadPolymorphicError.new(reflection)
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/fixtures.rb
Expand Up @@ -656,7 +656,7 @@ def table_rows
fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s

if association.name.to_s != fk_name && value = row.delete(association.name.to_s)
if association.options[:polymorphic] && value.sub!(/\s*\(([^\)]*)\)\s*$/, "")
if association.polymorphic? && value.sub!(/\s*\(([^\)]*)\)\s*$/, "")
# support polymorphic belongs_to as "label (Type)"
row[association.foreign_type] = $1
end
Expand Down
16 changes: 8 additions & 8 deletions activerecord/lib/active_record/reflection.rb
Expand Up @@ -239,7 +239,7 @@ def initialize(macro, name, scope, options, active_record)

def association_scope_cache(conn, owner)
key = conn.prepared_statements
if options[:polymorphic]
if polymorphic?
key = [key, owner.read_attribute(@foreign_type)]
end
@association_scope_cache[key] ||= @scope_lock.synchronize {
Expand Down Expand Up @@ -303,7 +303,7 @@ def check_validity!
end

def check_validity_of_inverse!
unless options[:polymorphic]
unless polymorphic?
if has_inverse? && inverse_of.nil?
raise InverseOfAssociationNotFoundError.new(self)
end
Expand Down Expand Up @@ -403,7 +403,7 @@ def belongs_to?
def association_class
case macro
when :belongs_to
if options[:polymorphic]
if polymorphic?
Associations::BelongsToPolymorphicAssociation
else
Associations::BelongsToAssociation
Expand All @@ -424,7 +424,7 @@ def association_class
end

def polymorphic?
options.key? :polymorphic
options[:polymorphic]
end

VALID_AUTOMATIC_INVERSE_MACROS = [:has_many, :has_one, :belongs_to]
Expand All @@ -441,7 +441,7 @@ def actual_source_reflection # FIXME: this is a horrible name
def calculate_constructable(macro, options)
case macro
when :belongs_to
!options[:polymorphic]
!polymorphic?
when :has_one
!options[:through]
else
Expand Down Expand Up @@ -723,19 +723,19 @@ def check_validity!
raise HasManyThroughAssociationNotFoundError.new(active_record.name, self)
end

if through_reflection.options[:polymorphic]
if through_reflection.polymorphic?
raise HasManyThroughAssociationPolymorphicThroughError.new(active_record.name, self)
end

if source_reflection.nil?
raise HasManyThroughSourceAssociationNotFoundError.new(self)
end

if options[:source_type] && source_reflection.options[:polymorphic].nil?
if options[:source_type] && !source_reflection.polymorphic?
raise HasManyThroughAssociationPointlessSourceTypeError.new(active_record.name, self, source_reflection)
end

if source_reflection.options[:polymorphic] && options[:source_type].nil?
if source_reflection.polymorphic? && options[:source_type].nil?
raise HasManyThroughAssociationPolymorphicSourceError.new(active_record.name, self, source_reflection)
end

Expand Down

0 comments on commit b79e22c

Please sign in to comment.