Skip to content

Commit 7bfd542

Browse files
author
Артём Большаков
committed
--wip--
1 parent f690789 commit 7bfd542

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module ActiveModel
22
class Serializer
3+
# This class hold all information about serializer's association.
4+
#
35
Association = Struct.new(:name, :serializer, :options)
46
end
57
end

lib/active_model/serializer/associations.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,24 @@ def inherited(base)
2222
base._reflections = self._reflections.try(:dup) || []
2323
end
2424

25+
# alias_method :reflections, :_reflections
26+
2527
# Defines an association in the object should be rendered.
2628
#
2729
# The serializer object should implement the association name
2830
# as a method which should return an array when invoked. If a method
2931
# with the association name does not exist, the association name is
3032
# dispatched to the serialized object.
31-
def has_many(*attrs)
32-
associate attrs do |name, options|
33+
#
34+
# @param [Array<Symbol>] names association names
35+
# @param [Hash<Symbol,Object>] options for given association
36+
#
37+
# @example
38+
# has_many :comments, serializer: CommentSummarySerializer
39+
# has_many :commits, authors
40+
#
41+
def has_many(*names, **options)
42+
associate names do |name|
3343
HasManyReflection.new(name, options)
3444
end
3545
end
@@ -40,8 +50,8 @@ def has_many(*attrs)
4050
# as a method which should return an object when invoked. If a method
4151
# with the association name does not exist, the association name is
4252
# dispatched to the serialized object.
43-
def belongs_to(*attrs)
44-
associate attrs do |name, options|
53+
def belongs_to(*names, **options)
54+
associate names do |name|
4555
BelongsToReflection.new(name, options)
4656
end
4757
end
@@ -52,26 +62,25 @@ def belongs_to(*attrs)
5262
# as a method which should return an object when invoked. If a method
5363
# with the association name does not exist, the association name is
5464
# dispatched to the serialized object.
55-
def has_one(*attrs)
56-
associate attrs do |name, options|
65+
def has_one(*names, **options)
66+
associate names do |name|
5767
HasOneReflection.new(name, options)
5868
end
5969
end
6070

6171
private
6272

63-
def associate(attrs, &block) #:nodoc:
64-
options = attrs.extract_options!
73+
def associate(names, &block) #:nodoc:
6574
self._reflections = _reflections.dup
6675

67-
attrs.each do |name|
76+
names.each do |name|
6877
unless method_defined?(name)
6978
define_method name do
7079
object.send name
7180
end
7281
end
7382

74-
self._reflections << block.call(name, options)
83+
self._reflections << block.call(name)
7584
end
7685
end
7786
end

lib/active_model/serializer/reflection.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ module ActiveModel
22
class Serializer
33
# Holds all the meta-data about an association as it was specified in the
44
# ActiveModel::Serializer class.
5-
Reflection = Struct.new(:name, :options)
5+
#
6+
# @example
7+
# class PostSerializer < ActiveModel::Serializer
8+
# has_one :author, serializer: AuthorSerializer
9+
# has_many :comments
10+
# end
11+
#
12+
# PostSerializer.reflections #=>
13+
# # [
14+
# # HasOneReflection.new(:author, serializer: AuthorSerializer),
15+
# # HasManyReflection.new(:comments)
16+
# # ]
17+
#
18+
# So you can inspect reflections in your Adapters.
19+
#
20+
Reflection = Struct.new(:name, :options) do
21+
def self.build(macro, *names)
22+
macro
23+
end
24+
end
625
end
726
end

test/serializers/associations_test.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ def test_serializer_options_are_passed_into_associations_serializers
8484
serializer = association.serializer
8585

8686
if name == :comments
87-
puts serializer.first
88-
8987
assert serializer.first.custom_options[:custom_options]
9088
end
9189
end

0 commit comments

Comments
 (0)