From 8f49c32cca518ae9ec4475923fae7c13c4706db4 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Mon, 18 Dec 2023 23:07:23 +0100 Subject: [PATCH] Mention and test has_object with multiple names --- README.md | 3 +++ .../object_association_test.rb | 20 +++++++++---------- test/boot/active_record.rb | 2 +- test/boot/associated_object.rb | 3 +++ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3d51277..40a8715 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ end > [!TIP] > `has_object` only requires a namespace and an initializer that takes a single argument. The above `Post::Publisher` is perfectly valid as an Associated Object — same goes for `class Post::Publisher < Data.define(:post); end`. +> [!TIP] +> You can pass multiple names too: `has_object :publisher, :classified, :fortification`. I recommend `-[i]er`, `-[i]ed` and `-ion` as the general naming conventions for your Associated Objects. + See how we're always expecting a link to the model, here `post`? Because of that, you can rely on `post` from the associated object: diff --git a/test/active_record/associated_object/object_association_test.rb b/test/active_record/associated_object/object_association_test.rb index c183603..e5f14f3 100644 --- a/test/active_record/associated_object/object_association_test.rb +++ b/test/active_record/associated_object/object_association_test.rb @@ -2,20 +2,20 @@ require "test_helper" -class ActiveRecord::AssociatedObject::ObjectAssociationTest < Minitest::Test - def setup - super - @post = Post.first - end +class ActiveRecord::AssociatedObject::ObjectAssociationTest < ActiveSupport::TestCase + test "standard PORO can be accessed" do + assert_kind_of Post::Mailroom, Post.first.mailroom - def test_standard_PORO_can_be_accessed - assert_kind_of Post::Mailroom, @post.mailroom + author = Author.first + assert_kind_of Author::Archiver, author.archiver + assert_kind_of Author::Classified, author.classified + assert_kind_of Author::Fortification, author.fortification end - def test_callback_passing_for_standard_PORO + test "callback passing for standard PORO" do Post::Mailroom.touched = false - @post.touch - assert @post.mailroom.touched + Post.first.touch + assert Post.first.mailroom.touched end end diff --git a/test/boot/active_record.rb b/test/boot/active_record.rb index cf00031..58f9174 100644 --- a/test/boot/active_record.rb +++ b/test/boot/active_record.rb @@ -26,7 +26,7 @@ class Author < ApplicationRecord has_many :posts, dependent: :destroy has_many :comments, dependent: :destroy, class_name: "Post::Comment" - has_object :archiver + has_object :archiver, :classified, :fortification end class Post < ApplicationRecord diff --git a/test/boot/associated_object.rb b/test/boot/associated_object.rb index e12c2cb..7922e04 100644 --- a/test/boot/associated_object.rb +++ b/test/boot/associated_object.rb @@ -11,6 +11,9 @@ class ApplicationRecord::AssociatedObject < ActiveRecord::AssociatedObject; end class Author::Archiver < ApplicationRecord::AssociatedObject end +Author::Classified = Data.define(:author) +Author::Fortification = Data.define(:author) + class Post::Publisher < ApplicationRecord::AssociatedObject mattr_accessor :performed, default: false mattr_accessor :captured_title, default: nil