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 11module ActiveModel
22 class Serializer
3- Attribute = Struct . new ( :name , :block ) do
3+ Attribute = Struct . new ( :name , :options , : block) do
44 def value ( serializer )
55 if block
66 serializer . instance_eval ( &block )
77 else
88 serializer . read_attribute_for_serialization ( name )
99 end
1010 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
1138 end
1239 end
1340end
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ module Attributes
1717 def attributes ( requested_attrs = nil , reload = false )
1818 @attributes = nil if reload
1919 @attributes ||= self . class . _attributes_data . each_with_object ( { } ) do |( key , attr ) , hash |
20+ next unless attr . included? ( self )
2021 next unless requested_attrs . nil? || requested_attrs . include? ( key )
2122 hash [ key ] = attr . value ( self )
2223 end
@@ -54,7 +55,7 @@ def attributes(*attrs)
5455 # end
5556 def attribute ( attr , options = { } , &block )
5657 key = options . fetch ( :key , attr )
57- _attributes_data [ key ] = Attribute . new ( attr , block )
58+ _attributes_data [ key ] = Attribute . new ( attr , options , block )
5859 end
5960
6061 # @api private
You can’t perform that action at this time.
0 commit comments