Skip to content

Commit c4af610

Browse files
committed
Merge pull request #1031 from bolshakov/feature/disaplow_to_define_multiple_associations_at_once
Disallow to define multiple associations at once
2 parents 9ace77e + 424a053 commit c4af610

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

lib/active_model/serializer/associations.rb

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,65 +29,57 @@ def inherited(base)
2929
base._reflections = self._reflections.try(:dup) || []
3030
end
3131

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
3334
# @return [void]
3435
#
3536
# @example
3637
# has_many :comments, serializer: CommentSummarySerializer
37-
# has_many :commits, authors
3838
#
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)
4341
end
4442

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
4645
# @return [void]
4746
#
4847
# @example
4948
# belongs_to :author, serializer: AuthorSerializer
5049
#
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)
5552
end
5653

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
5856
# @return [void]
5957
#
6058
# @example
6159
# has_one :author, serializer: AuthorSerializer
6260
#
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)
6763
end
6864

6965
private
7066

7167
# Add reflection and define {name} accessor.
72-
# @param [Array<Symbol>]
73-
# @yield [Symbol] return reflection
68+
# @param [ActiveModel::Serializer::Reflection] reflection
69+
# @return [void]
7470
#
7571
# @api private
7672
#
77-
def associate(attrs)
78-
options = attrs.extract_options!
79-
73+
def associate(reflection)
8074
self._reflections = _reflections.dup
8175

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
8779
end
88-
89-
self._reflections << yield(name, options)
9080
end
81+
82+
self._reflections << reflection
9183
end
9284
end
9385

0 commit comments

Comments
 (0)