Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Polymorphic Associated Objects: Take 2 #29

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions lib/active_record/associated_object.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
class ActiveRecord::AssociatedObject
singleton_class.attr_reader :record_klass, :attribute_name
delegate :record_klass, :attribute_name, to: :class

class << self
def inherited(klass)
record_klass = klass.module_parent
record_name = klass.module_parent_name.demodulize.underscore
attribute_name = klass.to_s.demodulize.underscore.to_sym
def polymorphic? = record_klass.nil?

unless record_klass.respond_to?(:descends_from_active_record?) && record_klass.descends_from_active_record?
raise ArgumentError, "#{record_klass} isn't valid; can only associate with ActiveRecord::Base subclasses"
end
def associate_with(record_klass)
raise ArgumentError, "#{record_klass} isn't valid; can only associate with ActiveRecord::Base subclasses" \
unless record_klass.respond_to?(:descends_from_active_record?) && record_klass.descends_from_active_record?

@record_klass = record_klass
@attribute_name = to_s.demodulize.underscore.to_sym

klass.alias_method record_name, :record
klass.define_singleton_method(:record_klass) { record_klass }
klass.define_singleton_method(:attribute_name) { attribute_name }
klass.delegate :record_klass, :attribute_name, to: :class
alias_method module_parent_name.demodulize.underscore, :record # `alias_method :post, :record` for a `Post::Publisher`.
end
def inherited(klass) = klass.module_parent_name && klass.associate_with(klass.module_parent)

def extension(&block)
raise ArgumentError, "calling `extension` on polymorphic Associated Objects isn't supported yet" if polymorphic?
record_klass.class_eval(&block)
end

Expand Down
12 changes: 12 additions & 0 deletions test/active_record/associated_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,16 @@ class ActiveRecord::AssociatedObjectTest < ActiveSupport::TestCase
assert_predicate Post::Comment.great.first, :rated_great?
assert_match /test\/boot\/associated_object/, Post::Comment.instance_method(:rated_great?).source_location.first
end

class PolymorphicTest < ActiveSupport::TestCase
test "polymorphic associated objects" do
post, comment = Post.first, Post::Comment.first

assert_kind_of Post::Pricing, post.pricing
assert_kind_of Post::Comment::Pricing, comment.pricing

assert post.pricing.post?
refute comment.pricing.post?
end
end
end
13 changes: 13 additions & 0 deletions test/boot/associated_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,16 @@ def great?
end

Post::Comment.has_object :rating

# Can locate subclasses by grepping for `< Pricing`
class Pricing < ActiveRecord::AssociatedObject
# Here, `record` will return the post for Post::Pricing objects and the comment for Post::Comment::Pricings.

def post? = record.is_a?(Post) # Probably don't do this in a real app.
end

class Post::Pricing < Pricing; end
class Post::Comment::Pricing < Pricing; end

Post.has_object :pricing
Post::Comment.has_object :pricing
Loading