Skip to content

Commit

Permalink
Allow extension from within Associated Object
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspth committed Dec 23, 2023
1 parent e988e6a commit d5db1b1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/active_record/associated_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def inherited(klass)
klass.delegate :record_klass, :attribute_name, to: :class
end

def extension(&block)
record_klass.class_eval(&block)
end

def respond_to_missing?(...) = record_klass.respond_to?(...) || super
delegate :unscoped, :transaction, :primary_key, to: :record_klass

Expand Down
5 changes: 5 additions & 0 deletions test/active_record/associated_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,9 @@ def test_active_job_integration
def test_calling_method
assert @rating.great?
end

def test_record_klass_extension
assert_predicate Post::Comment.great.first, :rated_great?
assert_match /test\/boot\/associated_object/, Post::Comment.instance_method(:rated_great?).source_location.first
end
end
6 changes: 6 additions & 0 deletions test/boot/associated_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def publish
Post.has_object :publisher, after_update_commit: true, before_destroy: :prevent_errant_post_destroy

class Post::Comment::Rating < ActiveRecord::AssociatedObject
extension do
scope :great, -> { where(body: "First!!!!") }

def rated_great? = rating.great?
end

kredis_flag :moderated

def great?
Expand Down

0 comments on commit d5db1b1

Please sign in to comment.