Skip to content

Commit

Permalink
Mention and test has_object with multiple names
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspth committed Dec 18, 2023
1 parent e9738a4 commit 8f49c32
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 10 additions & 10 deletions test/active_record/associated_object/object_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/boot/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/boot/associated_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f49c32

Please sign in to comment.