acts_as_api 0.3.5 → 0.3.6

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.
Files changed (54) hide show
  1. data/Gemfile +1 -1
  2. data/History.txt +5 -0
  3. data/README.rdoc +9 -2
  4. data/Rakefile +15 -1
  5. data/acts_as_api.gemspec +1 -0
  6. data/examples/introduction/index.html +7 -6
  7. data/examples/introduction/index.rb +9 -8
  8. data/lib/acts_as_api.rb +12 -5
  9. data/lib/acts_as_api/adapters.rb +5 -0
  10. data/lib/acts_as_api/adapters/mongoid.rb +11 -0
  11. data/lib/acts_as_api/array.rb +4 -8
  12. data/lib/acts_as_api/responder.rb +40 -0
  13. data/lib/acts_as_api/version.rb +1 -1
  14. data/spec/controllers/respond_with_users_controller_spec.rb +30 -2
  15. data/spec/controllers/users_controller_spec.rb +17 -235
  16. data/spec/models/active_record_spec.rb +28 -0
  17. data/spec/models/mongoid_spec.rb +28 -0
  18. data/spec/rails_app/app/controllers/respond_with_users_controller.rb +19 -8
  19. data/spec/rails_app/app/controllers/users_controller.rb +13 -4
  20. data/spec/rails_app/app/models/mongo_profile.rb +10 -0
  21. data/spec/rails_app/app/models/mongo_task.rb +12 -0
  22. data/spec/rails_app/app/models/mongo_untouched.rb +7 -0
  23. data/spec/rails_app/app/models/mongo_user.rb +149 -0
  24. data/spec/rails_app/app/models/user.rb +6 -6
  25. data/spec/rails_app/config/initializers/acts_as_api_mongoid.rb +6 -0
  26. data/spec/rails_app/config/mongoid.yml +23 -0
  27. data/spec/support/controller_examples.rb +246 -0
  28. data/spec/support/it_supports.rb +3 -0
  29. data/spec/support/model_examples/associations.rb +272 -0
  30. data/spec/support/model_examples/closures.rb +49 -0
  31. data/spec/support/model_examples/conditional_if.rb +165 -0
  32. data/spec/support/model_examples/conditional_unless.rb +165 -0
  33. data/spec/support/model_examples/enabled.rb +10 -0
  34. data/spec/support/model_examples/extending.rb +112 -0
  35. data/spec/support/model_examples/methods.rb +23 -0
  36. data/spec/support/model_examples/renaming.rb +50 -0
  37. data/spec/support/model_examples/simple.rb +23 -0
  38. data/spec/support/model_examples/sub_nodes.rb +105 -0
  39. data/spec/support/model_examples/undefined.rb +7 -0
  40. data/spec/support/model_examples/untouched.rb +13 -0
  41. data/spec/support/simple_fixtures.rb +39 -8
  42. metadata +67 -28
  43. data/spec/models/base/associations_spec.rb +0 -284
  44. data/spec/models/base/closures_spec.rb +0 -62
  45. data/spec/models/base/conditional_if_spec.rb +0 -178
  46. data/spec/models/base/conditional_unless_spec.rb +0 -178
  47. data/spec/models/base/enabled_spec.rb +0 -15
  48. data/spec/models/base/extending_spec.rb +0 -125
  49. data/spec/models/base/methods_spec.rb +0 -33
  50. data/spec/models/base/renaming_spec.rb +0 -63
  51. data/spec/models/base/simple_spec.rb +0 -33
  52. data/spec/models/base/sub_nodes_spec.rb +0 -118
  53. data/spec/models/base/undefined_spec.rb +0 -20
  54. data/spec/models/base/untouched_spec.rb +0 -18
@@ -0,0 +1,165 @@
1
+ shared_examples_for "conditional unless statements" do
2
+
3
+ describe "using the :unless option" do
4
+
5
+ describe "passing a symbol" do
6
+
7
+ describe "that returns false" do
8
+
9
+ before(:each) do
10
+ @response = @luke.as_api_response(:unless_under_thirty)
11
+ end
12
+
13
+ it "returns a hash" do
14
+ @response.should be_kind_of(Hash)
15
+ end
16
+
17
+ it "returns the correct number of fields" do
18
+ @response.should have(1).keys
19
+ end
20
+
21
+ it "won't add the conditional field but all others" do
22
+ @response.keys.should include(:first_name)
23
+ @response.keys.should_not include(:full_name)
24
+ end
25
+
26
+ it "the other specified fields have the correct value" do
27
+ @response.values.should include(@luke.first_name)
28
+ end
29
+
30
+ end
31
+
32
+ describe "that returns nil" do
33
+
34
+ before(:each) do
35
+ @response = @luke.as_api_response(:unless_returns_nil)
36
+ end
37
+
38
+ it "returns a hash" do
39
+ @response.should be_kind_of(Hash)
40
+ end
41
+
42
+ it "returns the correct number of fields" do
43
+ @response.should have(2).keys
44
+ end
45
+
46
+ it "won't add the conditional field but all others" do
47
+ @response.keys.should include(:first_name)
48
+ @response.keys.should include(:last_name)
49
+ end
50
+
51
+ it "the other specified fields have the correct value" do
52
+ @response.values.should include(@luke.first_name, @luke.last_name)
53
+ end
54
+
55
+ end
56
+
57
+ describe "that returns true" do
58
+
59
+ before(:each) do
60
+ @response = @han.as_api_response(:unless_under_thirty)
61
+ end
62
+
63
+ it "returns a hash" do
64
+ @response.should be_kind_of(Hash)
65
+ end
66
+
67
+ it "returns the correct number of fields" do
68
+ @response.should have(2).keys
69
+ end
70
+
71
+ it "won't add the conditional field but all others" do
72
+ @response.keys.should include(:first_name)
73
+ @response.keys.should include(:last_name)
74
+ end
75
+
76
+ it "the other specified fields have the correct value" do
77
+ @response.values.should include(@han.first_name, @han.last_name)
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+ describe "passing a proc" do
87
+
88
+ describe "that returns false" do
89
+
90
+ before(:each) do
91
+ @response = @luke.as_api_response(:unless_under_thirty_proc)
92
+ end
93
+
94
+ it "returns a hash" do
95
+ @response.should be_kind_of(Hash)
96
+ end
97
+
98
+ it "returns the correct number of fields" do
99
+ @response.should have(1).keys
100
+ end
101
+
102
+ it "won't add the conditional field but all others" do
103
+ @response.keys.should include(:first_name)
104
+ @response.keys.should_not include(:full_name)
105
+ end
106
+
107
+ it "the other specified fields have the correct value" do
108
+ @response.values.should include(@luke.first_name)
109
+ end
110
+
111
+ end
112
+
113
+ describe "that returns nil" do
114
+
115
+ before(:each) do
116
+ @response = @luke.as_api_response(:if_returns_nil_proc)
117
+ end
118
+
119
+ it "returns a hash" do
120
+ @response.should be_kind_of(Hash)
121
+ end
122
+
123
+ it "returns the correct number of fields" do
124
+ @response.should have(1).keys
125
+ end
126
+
127
+ it "won't add the conditional field but all others" do
128
+ @response.keys.should include(:first_name)
129
+ @response.keys.should_not include(:full_name)
130
+ end
131
+
132
+ it "the other specified fields have the correct value" do
133
+ @response.values.should include(@luke.first_name)
134
+ end
135
+
136
+ end
137
+
138
+ describe "that returns true" do
139
+
140
+ before(:each) do
141
+ @response = @han.as_api_response(:unless_under_thirty_proc)
142
+ end
143
+
144
+ it "returns a hash" do
145
+ @response.should be_kind_of(Hash)
146
+ end
147
+
148
+ it "returns the correct number of fields" do
149
+ @response.should have(2).keys
150
+ end
151
+
152
+ it "won't add the conditional field but all others" do
153
+ @response.keys.should include(:first_name)
154
+ @response.keys.should include(:last_name)
155
+ end
156
+
157
+ it "the other specified fields have the correct value" do
158
+ @response.values.should include(@han.first_name, @han.last_name)
159
+ end
160
+
161
+ end
162
+
163
+ end
164
+
165
+ end
@@ -0,0 +1,10 @@
1
+ shared_examples_for "acts_as_api is enabled" do
2
+
3
+ it "indicates that acts_as_api is enabled" do
4
+ @user_model.acts_as_api?.should be_true
5
+ end
6
+
7
+ it "does respond to api_accessible" do
8
+ @user_model.should respond_to :api_accessible
9
+ end
10
+ end
@@ -0,0 +1,112 @@
1
+ shared_examples_for "extending a given api template" do
2
+
3
+ describe "multiple times" do
4
+
5
+ before(:each) do
6
+ @user_model.api_accessible :public do |t|
7
+ t.add :first_name
8
+ end
9
+
10
+ @user_model.api_accessible :for_buddies, :extend => :public do |t|
11
+ t.add :age
12
+ end
13
+
14
+ @user_model.api_accessible :private, :extend => :for_buddies do |t|
15
+ t.add :last_name
16
+ end
17
+ @response = @luke.as_api_response(:private)
18
+ end
19
+
20
+ it "returns a hash" do
21
+ @response.should be_kind_of(Hash)
22
+ end
23
+
24
+ it "returns the correct number of fields" do
25
+ @response.should have(3).keys
26
+ end
27
+
28
+ it "returns all specified fields" do
29
+ @response.keys.sort_by(&:to_s).should eql([:age, :first_name, :last_name])
30
+ end
31
+
32
+ it "returns the correct values for the specified fields" do
33
+ @response.values.sort_by(&:to_s).should eql([@luke.age, @luke.first_name, @luke.last_name].sort_by(&:to_s))
34
+ end
35
+
36
+ end
37
+
38
+ describe "and removing a former added value" do
39
+
40
+ before(:each) do
41
+ @response = @luke.as_api_response(:age_and_first_name)
42
+ end
43
+
44
+ it "returns a hash" do
45
+ @response.should be_kind_of(Hash)
46
+ end
47
+
48
+ it "returns the correct number of fields" do
49
+ @response.should have(2).keys
50
+ end
51
+
52
+ it "returns all specified fields" do
53
+ @response.keys.sort_by(&:to_s).should eql([:first_name, :age].sort_by(&:to_s))
54
+ end
55
+
56
+ it "returns the correct values for the specified fields" do
57
+ @response.values.sort_by(&:to_s).should eql([@luke.first_name, @luke.age].sort_by(&:to_s))
58
+ end
59
+
60
+ end
61
+
62
+ describe "and inherit a field using another template name", :meow => true do
63
+
64
+ before(:each) do
65
+ @task_model.acts_as_api
66
+ @task_model.api_accessible :other_template do |t|
67
+ t.add :description
68
+ t.add :time_spent
69
+ end
70
+ @user_model.api_accessible :extending_other_template, :extend => :other_sub_template
71
+ @response = @luke.as_api_response(:extending_other_template)
72
+ end
73
+
74
+ it "returns a hash" do
75
+ @response.should be_kind_of(Hash)
76
+ end
77
+
78
+ it "returns the correct number of fields" do
79
+ @response.should have(2).keys
80
+ end
81
+
82
+ it "returns all specified fields" do
83
+ @response.keys.should include(:first_name)
84
+ end
85
+
86
+ it "returns the correct values for the specified fields" do
87
+ @response.values.should include(@luke.first_name)
88
+ end
89
+
90
+ it "returns all specified fields" do
91
+ @response.keys.should include(:tasks)
92
+ end
93
+
94
+ it "returns the correct values for the specified fields" do
95
+ @response[:tasks].should be_an Array
96
+ @response[:tasks].should have(3).tasks
97
+ end
98
+
99
+ it "contains the associated child models with the determined api template" do
100
+ @response[:tasks].each do |task|
101
+ task.keys.should include(:description, :time_spent)
102
+ task.keys.should have(2).attributes
103
+ end
104
+ end
105
+
106
+ it "contains the correct data of the child models" do
107
+ task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :description => t.description, :time_spent => t.time_spent } }
108
+ @response[:tasks].should eql task_hash
109
+ end
110
+ end
111
+
112
+ end
@@ -0,0 +1,23 @@
1
+ shared_examples_for "calling a method in the api template" do
2
+
3
+ before(:each) do
4
+ @response = @luke.as_api_response(:only_full_name)
5
+ end
6
+
7
+ it "returns a hash" do
8
+ @response.should be_kind_of(Hash)
9
+ end
10
+
11
+ it "returns the correct number of fields" do
12
+ @response.should have(1).keys
13
+ end
14
+
15
+ it "returns all specified fields by name" do
16
+ @response.keys.should include(:full_name)
17
+ end
18
+
19
+ it "returns the correct values for the specified fields" do
20
+ @response.values.should include(@luke.full_name)
21
+ end
22
+
23
+ end
@@ -0,0 +1,50 @@
1
+ shared_examples_for "renaming" do
2
+
3
+ describe "an attribute in the api template" do
4
+
5
+ before(:each) do
6
+ @response = @luke.as_api_response(:rename_last_name)
7
+ end
8
+
9
+ it "returns a hash" do
10
+ @response.should be_kind_of(Hash)
11
+ end
12
+
13
+ it "returns the correct number of fields" do
14
+ @response.should have(1).keys
15
+ end
16
+
17
+ it "returns all specified fields" do
18
+ @response.keys.should include(:family_name)
19
+ end
20
+
21
+ it "returns the correct values for the specified fields" do
22
+ @response.values.should include(@luke.last_name)
23
+ end
24
+
25
+ end
26
+
27
+ describe "the node/key of a method in the api template" do
28
+
29
+ before(:each) do
30
+ @response = @luke.as_api_response(:rename_full_name)
31
+ end
32
+
33
+ it "returns a hash" do
34
+ @response.should be_kind_of(Hash)
35
+ end
36
+
37
+ it "returns the correct number of fields" do
38
+ @response.should have(1).keys
39
+ end
40
+
41
+ it "returns all specified fields" do
42
+ @response.keys.should include(:other_full_name)
43
+ end
44
+
45
+ it "returns the correct values for the specified fields" do
46
+ @response.values.should include(@luke.full_name)
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,23 @@
1
+ shared_examples_for "listing attributes in the api template" do
2
+
3
+ before(:each) do
4
+ @response = @luke.as_api_response(:name_only)
5
+ end
6
+
7
+ it "returns a hash" do
8
+ @response.should be_kind_of(Hash)
9
+ end
10
+
11
+ it "returns the correct number of fields" do
12
+ @response.should have(2).keys
13
+ end
14
+
15
+ it "returns the specified fields only" do
16
+ @response.keys.should include(:first_name, :last_name)
17
+ end
18
+
19
+ it "the specified fields have the correct value" do
20
+ @response.values.should include(@luke.first_name, @luke.last_name)
21
+ end
22
+
23
+ end
@@ -0,0 +1,105 @@
1
+ shared_examples_for "creating a sub hash in the api template" do
2
+
3
+ describe "and putting an attribute in it" do
4
+
5
+ before(:each) do
6
+ @response = @luke.as_api_response(:sub_node)
7
+ end
8
+
9
+ it "returns a hash" do
10
+ @response.should be_kind_of(Hash)
11
+ end
12
+
13
+ it "returns the correct number of fields" do
14
+ @response.should have(1).keys
15
+ end
16
+
17
+ it "returns all specified fields" do
18
+ @response.keys.should include(:sub_nodes)
19
+ end
20
+
21
+ it "returns the correct values for the specified fields" do
22
+ @response[:sub_nodes].should be_a Hash
23
+ end
24
+
25
+ it "provides the correct number of sub nodes" do
26
+ @response[:sub_nodes].should have(1).keys
27
+ end
28
+
29
+ it "provides the correct sub nodes values" do
30
+ @response[:sub_nodes][:foo].should eql("something")
31
+ end
32
+ end
33
+
34
+ describe "multiple times and putting an attribute in it" do
35
+
36
+ before(:each) do
37
+ @response = @luke.as_api_response(:nested_sub_node)
38
+ end
39
+
40
+ it "returns a hash" do
41
+ @response.should be_kind_of(Hash)
42
+ end
43
+
44
+ it "returns the correct number of fields" do
45
+ @response.should have(1).keys
46
+ end
47
+
48
+ it "returns all specified fields" do
49
+ @response.keys.should include(:sub_nodes)
50
+ end
51
+
52
+ it "returns the correct values for the specified fields" do
53
+ @response[:sub_nodes].should be_a Hash
54
+ end
55
+
56
+ it "provides the correct number of sub nodes" do
57
+ @response[:sub_nodes].should have(1).keys
58
+ end
59
+
60
+ it "provides the correct number of sub nodes in the second level" do
61
+ @response[:sub_nodes][:foo].should have(1).keys
62
+ end
63
+
64
+ it "provides the correct sub nodes values" do
65
+ @response[:sub_nodes][:foo].tap do |foo|
66
+ foo[:bar].tap do |bar|
67
+ bar.should eql(@luke.last_name)
68
+ end
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ describe "using a method" do
75
+
76
+ before(:each) do
77
+ @response = @luke.as_api_response(:nested_sub_hash)
78
+ end
79
+
80
+ it "returns a hash" do
81
+ @response.should be_kind_of(Hash)
82
+ end
83
+
84
+ it "returns the correct number of fields" do
85
+ @response.should have(1).keys
86
+ end
87
+
88
+ it "returns all specified fields" do
89
+ @response.keys.should include(:sub_hash)
90
+ end
91
+
92
+ it "provides the correct number of sub nodes" do
93
+ @response[:sub_hash].should have(2).keys
94
+ end
95
+
96
+ it "provides the correct sub nodes" do
97
+ @response[:sub_hash].keys.should include(:foo, :hello)
98
+ end
99
+
100
+ it "provides the correct values in its sub nodes" do
101
+ @response[:sub_hash].values.should include("bar", "world")
102
+ end
103
+
104
+ end
105
+ end