File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed
lib/active_model/serializer Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change 1
1
module ActiveModel
2
2
class Serializer
3
- Attribute = Struct . new ( :name , :block ) do
3
+ Attribute = Struct . new ( :name , :options , : block) do
4
4
def value ( serializer )
5
5
if block
6
6
serializer . instance_eval ( &block )
7
7
else
8
8
serializer . read_attribute_for_serialization ( name )
9
9
end
10
10
end
11
+
12
+ def included? ( serializer )
13
+ case condition
14
+ when :if
15
+ serializer . public_send ( condition )
16
+ when :unless
17
+ !serializer . public_send ( condition )
18
+ else
19
+ true
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def condition_type
26
+ if options . key? ( :if )
27
+ :if
28
+ elsif options . key? ( :unless )
29
+ :unless
30
+ else
31
+ :none
32
+ end
33
+ end
34
+
35
+ def condition
36
+ options [ condition_type ]
37
+ end
11
38
end
12
39
end
13
40
end
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ module Attributes
17
17
def attributes ( requested_attrs = nil , reload = false )
18
18
@attributes = nil if reload
19
19
@attributes ||= self . class . _attributes_data . each_with_object ( { } ) do |( key , attr ) , hash |
20
+ next unless attr . included? ( self )
20
21
next unless requested_attrs . nil? || requested_attrs . include? ( key )
21
22
hash [ key ] = attr . value ( self )
22
23
end
@@ -54,7 +55,7 @@ def attributes(*attrs)
54
55
# end
55
56
def attribute ( attr , options = { } , &block )
56
57
key = options . fetch ( :key , attr )
57
- _attributes_data [ key ] = Attribute . new ( attr , block )
58
+ _attributes_data [ key ] = Attribute . new ( attr , options , block )
58
59
end
59
60
60
61
# @api private
You can’t perform that action at this time.
0 commit comments