acts_as_api 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +15 -1
- data/README.rdoc +3 -0
- data/lib/acts_as_api/api_template.rb +118 -12
- data/lib/acts_as_api/array.rb +1 -1
- data/lib/acts_as_api/base.rb +8 -67
- data/lib/acts_as_api/config.rb +4 -0
- data/lib/acts_as_api/rails_renderer.rb +3 -1
- data/lib/acts_as_api/version.rb +1 -2
- data/lib/acts_as_api.rb +2 -3
- data/spec/controllers/users_controller_spec.rb +4 -5
- data/spec/models/base/associations_spec.rb +284 -0
- data/spec/models/base/closures_spec.rb +62 -0
- data/spec/models/base/conditional_if_spec.rb +178 -0
- data/spec/models/base/conditional_unless_spec.rb +178 -0
- data/spec/models/base/enabled_spec.rb +15 -0
- data/spec/models/base/extending_spec.rb +74 -0
- data/spec/models/base/methods_spec.rb +33 -0
- data/spec/models/base/renaming_spec.rb +63 -0
- data/spec/models/base/simple_spec.rb +33 -0
- data/spec/models/base/sub_nodes_spec.rb +118 -0
- data/spec/models/base/undefined_spec.rb +20 -0
- data/spec/models/base/untouched_spec.rb +18 -0
- data/spec/rails_app/app/models/user.rb +52 -0
- data/spec/support/simple_fixtures.rb +26 -0
- metadata +30 -6
- data/spec/models/base_spec.rb +0 -635
@@ -0,0 +1,118 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper.rb'
|
2
|
+
|
3
|
+
describe ActsAsApi::Base do
|
4
|
+
|
5
|
+
describe "creating a sub hash in the api template", :orm => :active_record do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
setup_models
|
9
|
+
end
|
10
|
+
|
11
|
+
after(:each) do
|
12
|
+
clean_up
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "and putting an attribute in it" do
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
@response = @luke.as_api_response(:sub_node)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns a hash" do
|
22
|
+
@response.should be_kind_of(Hash)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns the correct number of fields" do
|
26
|
+
@response.should have(1).key
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns all specified fields" do
|
30
|
+
@response.keys.should include(:sub_nodes)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns the correct values for the specified fields" do
|
34
|
+
@response[:sub_nodes].should be_a Hash
|
35
|
+
end
|
36
|
+
|
37
|
+
it "provides the correct number of sub nodes" do
|
38
|
+
@response[:sub_nodes].should have(1).keys
|
39
|
+
end
|
40
|
+
|
41
|
+
it "provides the correct sub nodes values" do
|
42
|
+
@response[:sub_nodes][:foo].should eql("something")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "multiple times and putting an attribute in it" do
|
47
|
+
|
48
|
+
before(:each) do
|
49
|
+
@response = @luke.as_api_response(:nested_sub_node)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns a hash" do
|
53
|
+
@response.should be_kind_of(Hash)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns the correct number of fields" do
|
57
|
+
@response.should have(1).key
|
58
|
+
end
|
59
|
+
|
60
|
+
it "returns all specified fields" do
|
61
|
+
@response.keys.should include(:sub_nodes)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "returns the correct values for the specified fields" do
|
65
|
+
@response[:sub_nodes].should be_a Hash
|
66
|
+
end
|
67
|
+
|
68
|
+
it "provides the correct number of sub nodes" do
|
69
|
+
@response[:sub_nodes].should have(1).keys
|
70
|
+
end
|
71
|
+
|
72
|
+
it "provides the correct number of sub nodes in the second level" do
|
73
|
+
@response[:sub_nodes][:foo].should have(1).keys
|
74
|
+
end
|
75
|
+
|
76
|
+
it "provides the correct sub nodes values" do
|
77
|
+
@response[:sub_nodes][:foo].tap do |foo|
|
78
|
+
foo[:bar].tap do |bar|
|
79
|
+
bar.should eql(@luke.last_name)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "using a method" do
|
87
|
+
|
88
|
+
before(:each) do
|
89
|
+
@response = @luke.as_api_response(:nested_sub_hash)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "returns a hash" do
|
93
|
+
@response.should be_kind_of(Hash)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "returns the correct number of fields" do
|
97
|
+
@response.should have(1).key
|
98
|
+
end
|
99
|
+
|
100
|
+
it "returns all specified fields" do
|
101
|
+
@response.keys.should include(:sub_hash)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "provides the correct number of sub nodes" do
|
105
|
+
@response[:sub_hash].should have(2).keys
|
106
|
+
end
|
107
|
+
|
108
|
+
it "provides the correct sub nodes" do
|
109
|
+
@response[:sub_hash].keys.should include(:foo, :hello)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "provides the correct values in its sub nodes" do
|
113
|
+
@response[:sub_hash].values.should include("bar", "world")
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper.rb'
|
2
|
+
|
3
|
+
describe ActsAsApi::Base do
|
4
|
+
|
5
|
+
describe "trying to render an api template that is not defined", :orm => :active_record do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
setup_models
|
9
|
+
end
|
10
|
+
|
11
|
+
after(:each) do
|
12
|
+
clean_up
|
13
|
+
end
|
14
|
+
|
15
|
+
it "raises an descriptive error" do
|
16
|
+
lambda{ @luke.as_api_response(:does_not_exist) }.should raise_error(RuntimeError)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper.rb'
|
2
|
+
|
3
|
+
describe ActsAsApi::Base do
|
4
|
+
|
5
|
+
describe Untouched, :orm => :active_record do
|
6
|
+
|
7
|
+
describe "has disabled acts_as_api by default" do
|
8
|
+
it "indicates that acts_as_api is disabled" do
|
9
|
+
Untouched.acts_as_api?.should be_false
|
10
|
+
end
|
11
|
+
|
12
|
+
it "does not respond to api_accessible" do
|
13
|
+
Untouched.should_not respond_to :api_accessible
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -70,6 +70,58 @@ class User < ActiveRecord::Base
|
|
70
70
|
api_accessible :nested_sub_hash do |t|
|
71
71
|
t.add :sub_hash
|
72
72
|
end
|
73
|
+
|
74
|
+
api_accessible :if_over_thirty do |t|
|
75
|
+
t.add :first_name
|
76
|
+
t.add :last_name, :if => :over_thirty?
|
77
|
+
end
|
78
|
+
|
79
|
+
api_accessible :if_returns_nil do |t|
|
80
|
+
t.add :first_name
|
81
|
+
t.add :last_name, :if => :return_nil
|
82
|
+
end
|
83
|
+
|
84
|
+
api_accessible :if_over_thirty_proc do |t|
|
85
|
+
t.add :first_name
|
86
|
+
t.add :last_name, :if => lambda{|u| u.over_thirty? }
|
87
|
+
end
|
88
|
+
|
89
|
+
api_accessible :if_returns_nil_proc do |t|
|
90
|
+
t.add :first_name
|
91
|
+
t.add :last_name, :if => lambda{ nil }
|
92
|
+
end
|
93
|
+
|
94
|
+
api_accessible :unless_under_thirty do |t|
|
95
|
+
t.add :first_name
|
96
|
+
t.add :last_name, :unless => :under_thirty?
|
97
|
+
end
|
98
|
+
|
99
|
+
api_accessible :unless_returns_nil do |t|
|
100
|
+
t.add :first_name
|
101
|
+
t.add :last_name, :unless => :return_nil
|
102
|
+
end
|
103
|
+
|
104
|
+
api_accessible :unless_under_thirty_proc do |t|
|
105
|
+
t.add :first_name
|
106
|
+
t.add :last_name, :unless => lambda{|u| u.under_thirty? }
|
107
|
+
end
|
108
|
+
|
109
|
+
api_accessible :unless_returns_nil_proc do |t|
|
110
|
+
t.add :first_name
|
111
|
+
t.add :last_name, :unless => lambda{ nil }
|
112
|
+
end
|
113
|
+
|
114
|
+
def over_thirty?
|
115
|
+
age > 30
|
116
|
+
end
|
117
|
+
|
118
|
+
def under_thirty?
|
119
|
+
age < 30
|
120
|
+
end
|
121
|
+
|
122
|
+
def return_nil
|
123
|
+
nil
|
124
|
+
end
|
73
125
|
|
74
126
|
def full_name
|
75
127
|
'' << first_name.to_s << ' ' << last_name.to_s
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module SimpleFixtures
|
2
|
+
|
3
|
+
def setup_models
|
4
|
+
@luke = User.create({ :first_name => 'Luke', :last_name => 'Skywalker', :age => 25, :active => true })
|
5
|
+
@han = User.create({ :first_name => 'Han', :last_name => 'Solo', :age => 35, :active => true })
|
6
|
+
@leia = User.create({ :first_name => 'Princess', :last_name => 'Leia', :age => 25, :active => false })
|
7
|
+
|
8
|
+
@luke.profile = Profile.create({ :avatar => 'picard.jpg', :homepage => 'lukasarts.com' })
|
9
|
+
|
10
|
+
@destroy_deathstar = @luke.tasks.create({ :heading => "Destroy Deathstar", :description => "XWing, Shoot, BlowUp", :time_spent => 30, :done => true })
|
11
|
+
@study_with_yoda = @luke.tasks.create({ :heading => "Study with Yoda", :description => "Jedi Stuff, ya know", :time_spent => 60, :done => true })
|
12
|
+
@win_rebellion = @luke.tasks.create({ :heading => "Win Rebellion", :description => "no idea yet...", :time_spent => 180, :done => false })
|
13
|
+
end
|
14
|
+
|
15
|
+
def clean_up
|
16
|
+
User.delete_all
|
17
|
+
Task.delete_all
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
RSpec.configure do |c|
|
25
|
+
c.include SimpleFixtures
|
26
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 3
|
10
|
+
version: 0.3.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Christian B\xC3\xA4uerlein"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-24 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -112,7 +112,18 @@ files:
|
|
112
112
|
- lib/acts_as_api/version.rb
|
113
113
|
- spec/controllers/respond_with_users_controller_spec.rb
|
114
114
|
- spec/controllers/users_controller_spec.rb
|
115
|
-
- spec/models/
|
115
|
+
- spec/models/base/associations_spec.rb
|
116
|
+
- spec/models/base/closures_spec.rb
|
117
|
+
- spec/models/base/conditional_if_spec.rb
|
118
|
+
- spec/models/base/conditional_unless_spec.rb
|
119
|
+
- spec/models/base/enabled_spec.rb
|
120
|
+
- spec/models/base/extending_spec.rb
|
121
|
+
- spec/models/base/methods_spec.rb
|
122
|
+
- spec/models/base/renaming_spec.rb
|
123
|
+
- spec/models/base/simple_spec.rb
|
124
|
+
- spec/models/base/sub_nodes_spec.rb
|
125
|
+
- spec/models/base/undefined_spec.rb
|
126
|
+
- spec/models/base/untouched_spec.rb
|
116
127
|
- spec/rails_app/.gitignore
|
117
128
|
- spec/rails_app/Rakefile
|
118
129
|
- spec/rails_app/app/controllers/application_controller.rb
|
@@ -161,6 +172,7 @@ files:
|
|
161
172
|
- spec/spec.opts
|
162
173
|
- spec/spec_helper.rb
|
163
174
|
- spec/support/api_test_helpers.rb
|
175
|
+
- spec/support/simple_fixtures.rb
|
164
176
|
has_rdoc: true
|
165
177
|
homepage: https://github.com/fabrik42/acts_as_api
|
166
178
|
licenses: []
|
@@ -200,7 +212,18 @@ summary: Makes creating XML/JSON responses in Rails 3 easy and fun.
|
|
200
212
|
test_files:
|
201
213
|
- spec/controllers/respond_with_users_controller_spec.rb
|
202
214
|
- spec/controllers/users_controller_spec.rb
|
203
|
-
- spec/models/
|
215
|
+
- spec/models/base/associations_spec.rb
|
216
|
+
- spec/models/base/closures_spec.rb
|
217
|
+
- spec/models/base/conditional_if_spec.rb
|
218
|
+
- spec/models/base/conditional_unless_spec.rb
|
219
|
+
- spec/models/base/enabled_spec.rb
|
220
|
+
- spec/models/base/extending_spec.rb
|
221
|
+
- spec/models/base/methods_spec.rb
|
222
|
+
- spec/models/base/renaming_spec.rb
|
223
|
+
- spec/models/base/simple_spec.rb
|
224
|
+
- spec/models/base/sub_nodes_spec.rb
|
225
|
+
- spec/models/base/undefined_spec.rb
|
226
|
+
- spec/models/base/untouched_spec.rb
|
204
227
|
- spec/rails_app/.gitignore
|
205
228
|
- spec/rails_app/Rakefile
|
206
229
|
- spec/rails_app/app/controllers/application_controller.rb
|
@@ -249,3 +272,4 @@ test_files:
|
|
249
272
|
- spec/spec.opts
|
250
273
|
- spec/spec_helper.rb
|
251
274
|
- spec/support/api_test_helpers.rb
|
275
|
+
- spec/support/simple_fixtures.rb
|