-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work-in progress for providing better join model support and polymorp…
…hic associations git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3209 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
Showing
13 changed files
with
194 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module ActiveRecord | ||
module Associations | ||
class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc: | ||
def initialize(owner, association_name, association_class_name, association_class_primary_key_name, options) | ||
@owner = owner | ||
@options = options | ||
@association_name = association_name | ||
@association_class_primary_key_name = association_class_primary_key_name | ||
|
||
proxy_extend(options[:extend]) if options[:extend] | ||
|
||
reset | ||
end | ||
|
||
def create(attributes = {}) | ||
raise ActiveRecord::ActiveRecordError, "Can't create an abstract polymorphic object" | ||
end | ||
|
||
def build(attributes = {}) | ||
raise ActiveRecord::ActiveRecordError, "Can't build an abstract polymorphic object" | ||
end | ||
|
||
def replace(obj, dont_save = false) | ||
if obj.nil? | ||
@target = @owner[@association_class_primary_key_name] = @owner[@options[:foreign_type]] = nil | ||
else | ||
@target = (AssociationProxy === obj ? obj.target : obj) | ||
|
||
unless obj.new_record? | ||
@owner[@association_class_primary_key_name] = obj.id | ||
@owner[@options[:foreign_type]] = ActiveRecord::Base.send(:class_name_of_active_record_descendant, obj.class).to_s | ||
end | ||
|
||
@updated = true | ||
end | ||
|
||
@loaded = true | ||
|
||
return (@target.nil? ? nil : self) | ||
end | ||
|
||
private | ||
def find_target | ||
return nil if association_class.nil? | ||
|
||
if @options[:conditions] | ||
association_class.find( | ||
@owner[@association_class_primary_key_name], | ||
:conditions => interpolate_sql(@options[:conditions]), | ||
:include => @options[:include] | ||
) | ||
else | ||
association_class.find(@owner[@association_class_primary_key_name], :include => @options[:include]) | ||
end | ||
end | ||
|
||
def foreign_key_present | ||
!@owner[@association_class_primary_key_name].nil? | ||
end | ||
|
||
def target_obsolete? | ||
@owner[@association_class_primary_key_name] != @target.id | ||
end | ||
|
||
def association_class | ||
@owner[@options[:foreign_type]] ? @owner[@options[:foreign_type]].constantize : nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'abstract_unit' | ||
require 'fixtures/tag' | ||
require 'fixtures/tagging' | ||
require 'fixtures/post' | ||
require 'fixtures/comment' | ||
|
||
class AssociationsInterfaceTest < Test::Unit::TestCase | ||
fixtures :posts, :comments, :tags, :taggings | ||
|
||
def test_post_having_a_single_tag_through_has_many | ||
assert_equal taggings(:welcome_general), posts(:welcome).taggings.first | ||
end | ||
|
||
def test_post_having_a_single_tag_through_belongs_to | ||
assert_equal posts(:welcome), posts(:welcome).taggings.first.taggable | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
ActiveRecord::Schema.define do | ||
|
||
create_table "taggings", :force => true do |t| | ||
t.column "tag_id", :integer | ||
t.column "taggable_type", :string | ||
t.column "taggable_id", :integer | ||
end | ||
|
||
create_table "tags", :force => true do |t| | ||
t.column "name", :string | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Tag < ActiveRecord::Base | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Tagging < ActiveRecord::Base | ||
belongs_to :tag | ||
belongs_to :taggable, :polymorphic => true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
welcome_general: | ||
id: 1 | ||
tag_id: 1 | ||
taggable_id: 1 | ||
taggable_type: Post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
general: | ||
id: 1 | ||
name: General |