Skip to content

Commit 82b4c29

Browse files
committed
Add ability to pass common options to multiple associations
The following is now possible: ```ruby has_many :posts, :comments, namespace: "::Api::V1" ``` This translates to: ```ruby association :posts, namespace: "::Api::V1" association :comments, namespace: "::Api::V1" ```
1 parent 26d962f commit 82b4c29

File tree

6 files changed

+49
-9
lines changed

6 files changed

+49
-9
lines changed

lib/transmutation/serializer.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ def association(association_name, namespace: nil, serializer: nil, &custom_block
8888
attributes_config[association_name] = { block:, association: true }
8989
end
9090

91-
alias belongs_to association
92-
alias has_one association
93-
alias has_many association
94-
9591
# Shorthand for defining multiple attributes
9692
#
9793
# @param attribute_names [Array<Symbol>] The names of the attributes to serialize
@@ -100,9 +96,9 @@ def association(association_name, namespace: nil, serializer: nil, &custom_block
10096
# class UserSerializer < Transmutation::Serializer
10197
# attributes :first_name, :last_name
10298
# end
103-
def attributes(*attribute_names)
99+
def attributes(*attribute_names, **, &)
104100
attribute_names.each do |attribute_name|
105-
attribute(attribute_name)
101+
attribute(attribute_name, **, &)
106102
end
107103
end
108104

@@ -114,11 +110,15 @@ def attributes(*attribute_names)
114110
# class UserSerializer < Transmutation::Serializer
115111
# associations :posts, :comments
116112
# end
117-
def associations(*association_names)
113+
def associations(*association_names, **, &)
118114
association_names.each do |association_name|
119-
association(association_name)
115+
association(association_name, **, &)
120116
end
121117
end
118+
119+
alias belongs_to associations
120+
alias has_one associations
121+
alias has_many associations
122122
end
123123

124124
private

spec/system/dummy/models/comment.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
class Comment
4+
attr_accessor :id, :body, :user_id
5+
6+
def initialize(id:, body:, user_id: nil)
7+
self.id = id
8+
self.body = body
9+
self.user_id = user_id
10+
end
11+
12+
# =====
13+
# Finder methods
14+
# =====
15+
16+
def self.all
17+
[
18+
Comment.new(id: 1, body: "First!", user_id: 1),
19+
Comment.new(id: 2, body: "Second!", user_id: 2),
20+
Comment.new(id: 3, body: "Third!", user_id: 1)
21+
]
22+
end
23+
end

spec/system/dummy/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def posts
1313
Post.all.select { |post| post.user_id == id }
1414
end
1515

16+
def comments
17+
Comment.all.select { |comment| comment.user_id == id }
18+
end
19+
1620
# =====
1721
# Finder methods
1822
# =====
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Api
4+
module V1
5+
class CommentSerializer < Transmutation::Serializer
6+
attributes :id, :body
7+
end
8+
end
9+
end

spec/system/dummy/serializers/api/v1/detailed/user_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Detailed
66
class UserSerializer < Api::V1::UserSerializer
77
attributes :first_name, :last_name
88

9-
has_many :posts, namespace: "::Api::V1"
9+
has_many :posts, :comments, namespace: "::Api::V1"
1010

1111
has_many :published_posts, namespace: "::Api::V1" do
1212
object.posts.reject { |post| post.published_at.nil? }

spec/system/rendering_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
{ "id" => 1, "title" => "First post" },
3333
{ "id" => 3, "title" => "Second post!?" }
3434
],
35+
"comments" => [
36+
{ "id" => 1, "body" => "First!" },
37+
{ "id" => 3, "body" => "Third!" }
38+
],
3539
"published_posts" => [
3640
{ "id" => 1, "title" => "First post" }
3741
]

0 commit comments

Comments
 (0)