to_api 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/to_api/version.rb +1 -1
- data/lib/to_api.rb +6 -5
- data/spec/to_api_spec.rb +44 -1
- metadata +1 -1
data/lib/to_api/version.rb
CHANGED
data/lib/to_api.rb
CHANGED
@@ -62,7 +62,7 @@ if Object.const_defined? :ActiveRecord
|
|
62
62
|
include_hash.delete_if{|k,v| !valid_includes.include?(k.to_s)}
|
63
63
|
to_api_attributes.each do |k, v|
|
64
64
|
unless hash[k]
|
65
|
-
v.to_api_filters = to_api_filters if v.respond_to?
|
65
|
+
v.to_api_filters = to_api_filters if to_api_filters.any? && v.respond_to?(:to_api_filters)
|
66
66
|
|
67
67
|
attribute_includes = include_hash[k] || []
|
68
68
|
to_api_v = v.to_api(*[attribute_includes].flatten.compact)
|
@@ -74,7 +74,7 @@ if Object.const_defined? :ActiveRecord
|
|
74
74
|
unless hash[relation]
|
75
75
|
relation_includes = include_hash[relation] || []
|
76
76
|
api_obj = self.send(relation)
|
77
|
-
api_obj.to_api_filters = to_api_filters if api_obj.respond_to?
|
77
|
+
api_obj.to_api_filters = to_api_filters if api_obj.respond_to?(:to_api_filters)
|
78
78
|
|
79
79
|
hash[relation.to_s] = api_obj.respond_to?(:to_api) ? api_obj.to_api(*[relation_includes].flatten.compact) : api_obj
|
80
80
|
end
|
@@ -115,7 +115,8 @@ if Object.const_defined? :ActiveRecord
|
|
115
115
|
include ToApiFilter
|
116
116
|
def to_api(*includes)
|
117
117
|
map{|e|
|
118
|
-
e.to_api_filters = to_api_filters if e.respond_to?
|
118
|
+
e.to_api_filters = to_api_filters if to_api_filters.any? && e.respond_to?(:to_api_filters)
|
119
|
+
e.to_api_filters = to_api_filters if e.respond_to?(:to_api_filters)
|
119
120
|
e.to_api(*includes)
|
120
121
|
}
|
121
122
|
end
|
@@ -127,7 +128,7 @@ class Array
|
|
127
128
|
include ToApiFilter
|
128
129
|
def to_api(*includes)
|
129
130
|
map{|e|
|
130
|
-
e.to_api_filters = to_api_filters if e.respond_to?
|
131
|
+
e.to_api_filters = to_api_filters if to_api_filters.any? && e.respond_to?(:to_api_filters)
|
131
132
|
|
132
133
|
e.to_api(*includes)
|
133
134
|
}
|
@@ -151,7 +152,7 @@ class Hash
|
|
151
152
|
|
152
153
|
(keys-values.keys).each do |k|
|
153
154
|
val = self[k]
|
154
|
-
val.to_api_filters = to_api_filters if val.respond_to?
|
155
|
+
val.to_api_filters = to_api_filters if to_api_filters.any? && val.respond_to?(:to_api_filters)
|
155
156
|
child_includes = include_hash[k] || []
|
156
157
|
values[k] = val.to_api(*[child_includes].flatten.compact)
|
157
158
|
end
|
data/spec/to_api_spec.rb
CHANGED
@@ -27,10 +27,14 @@ class AssociatedRecord < ActiveRecord::Base
|
|
27
27
|
end
|
28
28
|
|
29
29
|
class OtherRecord < ActiveRecord::Base
|
30
|
+
attr_accessor :foo
|
31
|
+
|
32
|
+
def to_api_attributes
|
33
|
+
attributes.merge("foo" => foo)
|
34
|
+
end
|
30
35
|
end
|
31
36
|
|
32
37
|
class Category < ActiveRecord::Base
|
33
|
-
|
34
38
|
end
|
35
39
|
|
36
40
|
describe '#to_api' do
|
@@ -58,6 +62,16 @@ describe '#to_api' do
|
|
58
62
|
a.should_receive(:to_api).with('bar').and_return(:apiz)
|
59
63
|
{:one => a}.to_api(:one => ['bar']).should == {:one => :apiz}
|
60
64
|
end
|
65
|
+
|
66
|
+
it "can have frozen stuff inside it" do
|
67
|
+
hash = {
|
68
|
+
:key => {:foo => "bar"}
|
69
|
+
}
|
70
|
+
hash[:key].freeze
|
71
|
+
hash.freeze
|
72
|
+
|
73
|
+
hash.to_api.should == {:key => {:foo => "bar"}}.to_api
|
74
|
+
end
|
61
75
|
end
|
62
76
|
|
63
77
|
describe String do
|
@@ -182,6 +196,13 @@ describe '#to_api' do
|
|
182
196
|
a.should_receive(:to_api).with(['bar']).and_return(:apiz)
|
183
197
|
[a].to_api(['bar']).should == [:apiz]
|
184
198
|
end
|
199
|
+
|
200
|
+
it "can be frozen" do
|
201
|
+
a = [["foo"], ["bar"]]
|
202
|
+
a.each { |e| e.freeze }
|
203
|
+
a.freeze
|
204
|
+
a.to_api.should == [["foo"], ["bar"]].to_api
|
205
|
+
end
|
185
206
|
end
|
186
207
|
|
187
208
|
describe nil do
|
@@ -254,6 +275,28 @@ describe '#to_api' do
|
|
254
275
|
@base.to_api("group")["group"].should be_nil
|
255
276
|
end
|
256
277
|
|
278
|
+
it "can handle frozen attributes" do
|
279
|
+
obj = OtherRecord.new
|
280
|
+
obj.foo = ["bar"]
|
281
|
+
obj.to_api["foo"].should == ["bar"]
|
282
|
+
|
283
|
+
obj.freeze
|
284
|
+
obj.foo.freeze
|
285
|
+
obj.to_api["foo"].should == ["bar"]
|
286
|
+
end
|
287
|
+
|
288
|
+
it "can handle frozen associations" do
|
289
|
+
obj = ChildRecord.new(:name => "a child")
|
290
|
+
category = Category.new(:name => "cat 1")
|
291
|
+
obj.category = category
|
292
|
+
|
293
|
+
obj.to_api("category")["category"].should == category.to_api
|
294
|
+
|
295
|
+
obj.freeze
|
296
|
+
category.freeze
|
297
|
+
obj.to_api("category")["category"].should == category.to_api
|
298
|
+
end
|
299
|
+
|
257
300
|
describe "versions of params" do
|
258
301
|
|
259
302
|
before do
|