File tree Expand file tree Collapse file tree 3 files changed +32
-4
lines changed
unit/active_model/serializer Expand file tree Collapse file tree 3 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -79,10 +79,12 @@ def root_name
79
79
end
80
80
81
81
def attributes ( *attrs )
82
- @_attributes . concat attrs
83
-
84
82
attrs . each do |attr |
85
- define_method attr do
83
+ striped_attr = strip_attribute attr
84
+
85
+ @_attributes << striped_attr
86
+
87
+ define_method striped_attr do
86
88
object . read_attribute_for_serialization attr
87
89
end unless method_defined? ( attr )
88
90
end
@@ -98,6 +100,14 @@ def has_many(*attrs)
98
100
99
101
private
100
102
103
+ def strip_attribute ( attr )
104
+ symbolized = attr . is_a? ( Symbol )
105
+
106
+ attr = attr . to_s . gsub ( /\? \Z / , '' )
107
+ attr = attr . to_sym if symbolized
108
+ attr
109
+ end
110
+
101
111
def build_serializer_class ( resource , options )
102
112
"" . tap do |klass_name |
103
113
klass_name << "#{ options [ :namespace ] } ::" if options [ :namespace ]
Original file line number Diff line number Diff line change 1
1
class Model
2
- def initialize ( hash = { } )
2
+ def initialize ( hash = { } )
3
3
@attributes = hash
4
4
end
5
5
6
6
def read_attribute_for_serialization ( name )
7
7
if name == :id || name == 'id'
8
8
object_id
9
+ elsif respond_to? ( name )
10
+ send name
9
11
else
10
12
@attributes [ name ]
11
13
end
Original file line number Diff line number Diff line change @@ -36,6 +36,22 @@ def test_attributes_inheritance
36
36
assert_equal ( [ :name , :description ] ,
37
37
another_inherited_serializer_klass . _attributes )
38
38
end
39
+
40
+ def tests_query_attributes_strip_question_mark
41
+ model = Class . new ( ::Model ) do
42
+ def strip?
43
+ true
44
+ end
45
+ end
46
+
47
+ serializer = Class . new ( ActiveModel ::Serializer ) do
48
+ attributes :strip?
49
+ end
50
+
51
+ actual = serializer . new ( model . new ) . as_json
52
+
53
+ assert_equal ( { strip : true } , actual )
54
+ end
39
55
end
40
56
end
41
57
end
You can’t perform that action at this time.
0 commit comments