@@ -29,65 +29,57 @@ def inherited(base)
29
29
base . _reflections = self . _reflections . try ( :dup ) || [ ]
30
30
end
31
31
32
- # @param [Array(Array<Symbol>, Hash{Symbol => Object})] attrs
32
+ # @param [Symbol] name of the association
33
+ # @param [Hash<Symbol => any>] options for the reflection
33
34
# @return [void]
34
35
#
35
36
# @example
36
37
# has_many :comments, serializer: CommentSummarySerializer
37
- # has_many :commits, authors
38
38
#
39
- def has_many ( *attrs )
40
- associate attrs do |name , options |
41
- HasManyReflection . new ( name , options )
42
- end
39
+ def has_many ( name , options = { } )
40
+ associate HasManyReflection . new ( name , options )
43
41
end
44
42
45
- # @param [Array(Array<Symbol>, Hash{Symbol => Object})] attrs
43
+ # @param [Symbol] name of the association
44
+ # @param [Hash<Symbol => any>] options for the reflection
46
45
# @return [void]
47
46
#
48
47
# @example
49
48
# belongs_to :author, serializer: AuthorSerializer
50
49
#
51
- def belongs_to ( *attrs )
52
- associate attrs do |name , options |
53
- BelongsToReflection . new ( name , options )
54
- end
50
+ def belongs_to ( name , options = { } )
51
+ associate BelongsToReflection . new ( name , options )
55
52
end
56
53
57
- # @param [Array(Array<Symbol>, Hash{Symbol => Object})] attrs
54
+ # @param [Symbol] name of the association
55
+ # @param [Hash<Symbol => any>] options for the reflection
58
56
# @return [void]
59
57
#
60
58
# @example
61
59
# has_one :author, serializer: AuthorSerializer
62
60
#
63
- def has_one ( *attrs )
64
- associate attrs do |name , options |
65
- HasOneReflection . new ( name , options )
66
- end
61
+ def has_one ( name , options = { } )
62
+ associate HasOneReflection . new ( name , options )
67
63
end
68
64
69
65
private
70
66
71
67
# Add reflection and define {name} accessor.
72
- # @param [Array<Symbol>]
73
- # @yield [Symbol] return reflection
68
+ # @param [ActiveModel::Serializer::Reflection] reflection
69
+ # @return [void]
74
70
#
75
71
# @api private
76
72
#
77
- def associate ( attrs )
78
- options = attrs . extract_options!
79
-
73
+ def associate ( reflection )
80
74
self . _reflections = _reflections . dup
81
75
82
- attrs . each do |name |
83
- unless method_defined? ( name )
84
- define_method name do
85
- object . send name
86
- end
76
+ unless method_defined? ( reflection . name )
77
+ define_method reflection . name do
78
+ object . send reflection . name
87
79
end
88
-
89
- self . _reflections << yield ( name , options )
90
80
end
81
+
82
+ self . _reflections << reflection
91
83
end
92
84
end
93
85
0 commit comments