acts_as_api 0.4.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -7
- data/History.txt +5 -0
- data/LICENSE.txt +20 -0
- data/README.md +0 -25
- data/Rakefile +5 -12
- data/acts_as_api.gemspec +14 -13
- data/{examples/introduction → docs}/docco.css +0 -0
- data/{examples/introduction → docs}/index.html +18 -21
- data/{examples/introduction → docs}/index.rb +0 -0
- data/{examples/introduction → docs}/layout.mustache +0 -0
- data/lib/acts_as_api.rb +9 -12
- data/lib/acts_as_api/adapters.rb +1 -1
- data/lib/acts_as_api/api_template.rb +23 -25
- data/lib/acts_as_api/base.rb +11 -20
- data/lib/acts_as_api/collection.rb +1 -2
- data/lib/acts_as_api/config.rb +0 -5
- data/lib/acts_as_api/exceptions.rb +2 -2
- data/lib/acts_as_api/rails_renderer.rb +0 -2
- data/lib/acts_as_api/rendering.rb +5 -12
- data/lib/acts_as_api/responder.rb +8 -13
- data/lib/acts_as_api/version.rb +1 -1
- data/spec/README.md +15 -6
- data/spec/active_record_dummy/Gemfile +2 -10
- data/spec/active_record_dummy/app/models/user.rb +31 -32
- data/spec/active_record_dummy/config.ru +1 -1
- data/spec/active_record_dummy/config/application.rb +2 -2
- data/spec/active_record_dummy/config/boot.rb +1 -1
- data/spec/active_record_dummy/config/environments/test.rb +2 -2
- data/spec/active_record_dummy/config/initializers/session_store.rb +1 -1
- data/spec/active_record_dummy/config/initializers/wrap_parameters.rb +1 -1
- data/spec/active_record_dummy/config/routes.rb +1 -57
- data/spec/active_record_dummy/db/migrate/20110214201640_create_tables.rb +25 -26
- data/spec/active_record_dummy/db/schema.rb +27 -29
- data/spec/active_record_dummy/script/rails +2 -2
- data/spec/controllers/plain_objects_controller_spec.rb +11 -12
- data/spec/controllers/respond_with_users_controller_spec.rb +46 -67
- data/spec/controllers/users_controller_spec.rb +2 -3
- data/spec/models/model_spec.rb +17 -19
- data/spec/mongoid_dummy/Gemfile +3 -12
- data/spec/mongoid_dummy/app/models/profile.rb +6 -6
- data/spec/mongoid_dummy/app/models/task.rb +10 -10
- data/spec/mongoid_dummy/app/models/untouched.rb +4 -4
- data/spec/mongoid_dummy/app/models/user.rb +12 -14
- data/spec/mongoid_dummy/config.ru +1 -1
- data/spec/mongoid_dummy/config/application.rb +7 -7
- data/spec/mongoid_dummy/config/boot.rb +1 -1
- data/spec/mongoid_dummy/config/environments/development.rb +0 -1
- data/spec/mongoid_dummy/config/environments/production.rb +0 -1
- data/spec/mongoid_dummy/config/environments/test.rb +2 -2
- data/spec/mongoid_dummy/config/initializers/include_acts_as_api.rb +1 -1
- data/spec/mongoid_dummy/config/initializers/session_store.rb +1 -1
- data/spec/mongoid_dummy/config/initializers/wrap_parameters.rb +1 -2
- data/spec/mongoid_dummy/config/routes.rb +1 -57
- data/spec/mongoid_dummy/script/rails +2 -2
- data/spec/shared_engine/Gemfile +1 -1
- data/spec/shared_engine/Rakefile +1 -4
- data/spec/shared_engine/app/controllers/shared_engine/plain_objects_controller.rb +2 -2
- data/spec/shared_engine/app/controllers/shared_engine/respond_with_users_controller.rb +13 -15
- data/spec/shared_engine/app/controllers/shared_engine/users_controller.rb +17 -19
- data/spec/shared_engine/app/models/plain_object.rb +3 -1
- data/spec/shared_engine/app/models/user_template.rb +28 -29
- data/spec/shared_engine/lib/shared_engine.rb +1 -1
- data/spec/shared_engine/lib/shared_engine/version.rb +1 -1
- data/spec/shared_engine/shared_engine.gemspec +10 -12
- data/spec/spec_helper.rb +5 -13
- data/spec/support/api_test_helpers.rb +0 -2
- data/spec/support/controller_examples.rb +128 -173
- data/spec/support/it_supports.rb +1 -1
- data/spec/support/model_examples/associations.rb +119 -128
- data/spec/support/model_examples/callbacks.rb +17 -27
- data/spec/support/model_examples/closures.rb +21 -29
- data/spec/support/model_examples/conditional_if.rb +71 -103
- data/spec/support/model_examples/conditional_unless.rb +71 -103
- data/spec/support/model_examples/enabled.rb +5 -6
- data/spec/support/model_examples/extending.rb +49 -56
- data/spec/support/model_examples/methods.rb +11 -15
- data/spec/support/model_examples/options.rb +19 -25
- data/spec/support/model_examples/renaming.rb +21 -30
- data/spec/support/model_examples/simple.rb +10 -14
- data/spec/support/model_examples/sub_nodes.rb +47 -59
- data/spec/support/model_examples/undefined.rb +4 -6
- data/spec/support/model_examples/untouched.rb +6 -8
- data/spec/support/simple_fixtures.rb +11 -38
- metadata +22 -159
- data/spec/active_record_dummy/app/assets/images/rails.png +0 -0
- data/spec/active_record_dummy/app/assets/javascripts/application.js +0 -15
- data/spec/active_record_dummy/app/assets/stylesheets/application.css +0 -13
- data/spec/active_record_dummy/app/controllers/application_controller.rb +0 -3
- data/spec/active_record_dummy/app/helpers/application_helper.rb +0 -2
- data/spec/active_record_dummy/app/mailers/.gitkeep +0 -0
- data/spec/active_record_dummy/app/views/layouts/application.html.erb +0 -14
- data/spec/active_record_dummy/doc/README_FOR_APP +0 -2
- data/spec/active_record_dummy/lib/assets/.gitkeep +0 -0
- data/spec/active_record_dummy/lib/tasks/.gitkeep +0 -0
- data/spec/active_record_dummy/public/404.html +0 -26
- data/spec/active_record_dummy/public/422.html +0 -26
- data/spec/active_record_dummy/public/500.html +0 -25
- data/spec/active_record_dummy/public/favicon.ico +0 -0
- data/spec/active_record_dummy/public/index.html +0 -241
- data/spec/active_record_dummy/public/robots.txt +0 -5
- data/spec/active_record_dummy/vendor/assets/javascripts/.gitkeep +0 -0
- data/spec/active_record_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/active_record_dummy/vendor/plugins/.gitkeep +0 -0
- data/spec/mongoid_dummy/README.rdoc +0 -261
- data/spec/mongoid_dummy/app/assets/images/rails.png +0 -0
- data/spec/mongoid_dummy/app/assets/javascripts/application.js +0 -15
- data/spec/mongoid_dummy/app/assets/stylesheets/application.css +0 -13
- data/spec/mongoid_dummy/app/controllers/application_controller.rb +0 -3
- data/spec/mongoid_dummy/app/helpers/application_helper.rb +0 -2
- data/spec/mongoid_dummy/app/mailers/.gitkeep +0 -0
- data/spec/mongoid_dummy/app/views/layouts/application.html.erb +0 -14
- data/spec/mongoid_dummy/doc/README_FOR_APP +0 -2
- data/spec/mongoid_dummy/lib/assets/.gitkeep +0 -0
- data/spec/mongoid_dummy/lib/tasks/.gitkeep +0 -0
- data/spec/mongoid_dummy/public/404.html +0 -26
- data/spec/mongoid_dummy/public/422.html +0 -26
- data/spec/mongoid_dummy/public/500.html +0 -25
- data/spec/mongoid_dummy/public/favicon.ico +0 -0
- data/spec/mongoid_dummy/public/index.html +0 -241
- data/spec/mongoid_dummy/public/robots.txt +0 -5
- data/spec/mongoid_dummy/vendor/assets/javascripts/.gitkeep +0 -0
- data/spec/mongoid_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/mongoid_dummy/vendor/plugins/.gitkeep +0 -0
- data/spec/shared_engine/app/assets/images/shared_engine/.gitkeep +0 -0
- data/spec/shared_engine/app/assets/javascripts/shared_engine/application.js +0 -15
- data/spec/shared_engine/app/assets/stylesheets/shared_engine/application.css +0 -13
- data/spec/shared_engine/app/helpers/shared_engine/application_helper.rb +0 -4
- data/spec/shared_engine/app/views/layouts/shared_engine/application.html.erb +0 -14
- data/spec/shared_engine/dummy/README.rdoc +0 -261
- data/spec/shared_engine/dummy/Rakefile +0 -7
- data/spec/shared_engine/dummy/app/assets/javascripts/application.js +0 -15
- data/spec/shared_engine/dummy/app/assets/stylesheets/application.css +0 -13
- data/spec/shared_engine/dummy/app/controllers/application_controller.rb +0 -3
- data/spec/shared_engine/dummy/app/helpers/application_helper.rb +0 -2
- data/spec/shared_engine/dummy/app/mailers/.gitkeep +0 -0
- data/spec/shared_engine/dummy/app/models/.gitkeep +0 -0
- data/spec/shared_engine/dummy/app/views/layouts/application.html.erb +0 -14
- data/spec/shared_engine/dummy/config.ru +0 -4
- data/spec/shared_engine/dummy/config/application.rb +0 -56
- data/spec/shared_engine/dummy/config/boot.rb +0 -10
- data/spec/shared_engine/dummy/config/database.yml +0 -25
- data/spec/shared_engine/dummy/config/environment.rb +0 -5
- data/spec/shared_engine/dummy/config/environments/development.rb +0 -34
- data/spec/shared_engine/dummy/config/environments/production.rb +0 -63
- data/spec/shared_engine/dummy/config/environments/test.rb +0 -37
- data/spec/shared_engine/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/shared_engine/dummy/config/initializers/inflections.rb +0 -15
- data/spec/shared_engine/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/shared_engine/dummy/config/initializers/secret_token.rb +0 -7
- data/spec/shared_engine/dummy/config/initializers/session_store.rb +0 -8
- data/spec/shared_engine/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/spec/shared_engine/dummy/config/locales/en.yml +0 -5
- data/spec/shared_engine/dummy/config/routes.rb +0 -4
- data/spec/shared_engine/dummy/lib/assets/.gitkeep +0 -0
- data/spec/shared_engine/dummy/log/.gitkeep +0 -0
- data/spec/shared_engine/dummy/public/404.html +0 -26
- data/spec/shared_engine/dummy/public/422.html +0 -26
- data/spec/shared_engine/dummy/public/500.html +0 -25
- data/spec/shared_engine/dummy/public/favicon.ico +0 -0
- data/spec/shared_engine/dummy/script/rails +0 -6
@@ -1,22 +1,21 @@
|
|
1
1
|
module SharedEngine
|
2
2
|
class UsersController < SharedEngine::ApplicationController
|
3
|
-
|
4
3
|
def index
|
5
4
|
@users = User.all.sort_by(&:first_name)
|
6
5
|
|
7
6
|
respond_to do |format|
|
8
|
-
format.xml { render_for_api params[:api_template].to_sym, :
|
9
|
-
format.json { render_for_api params[:api_template].to_sym, :
|
7
|
+
format.xml { render_for_api params[:api_template].to_sym, xml: @users, root: :users }
|
8
|
+
format.json { render_for_api params[:api_template].to_sym, json: @users, root: :users }
|
10
9
|
end
|
11
10
|
end
|
12
11
|
|
13
12
|
def index_meta
|
14
13
|
@users = User.all.to_a
|
15
|
-
meta_hash = { :
|
14
|
+
meta_hash = { page: 1, total: 999 }
|
16
15
|
|
17
16
|
respond_to do |format|
|
18
|
-
format.xml { render_for_api params[:api_template].to_sym, :
|
19
|
-
format.json { render_for_api params[:api_template].to_sym, :
|
17
|
+
format.xml { render_for_api params[:api_template].to_sym, xml: @users, root: :users, meta: meta_hash }
|
18
|
+
format.json { render_for_api params[:api_template].to_sym, json: @users, root: :users, meta: meta_hash }
|
20
19
|
end
|
21
20
|
end
|
22
21
|
|
@@ -28,8 +27,8 @@ module SharedEngine
|
|
28
27
|
end
|
29
28
|
|
30
29
|
respond_to do |format|
|
31
|
-
format.xml { render_for_api params[:api_template].to_sym, :
|
32
|
-
format.json { render_for_api params[:api_template].to_sym, :
|
30
|
+
format.xml { render_for_api params[:api_template].to_sym, xml: @users }
|
31
|
+
format.json { render_for_api params[:api_template].to_sym, json: @users }
|
33
32
|
end
|
34
33
|
end
|
35
34
|
|
@@ -38,38 +37,37 @@ module SharedEngine
|
|
38
37
|
|
39
38
|
respond_to do |format|
|
40
39
|
# :root => :user is only used here because we need it for the node name of the MongoUser model
|
41
|
-
format.xml { render_for_api params[:api_template].to_sym, :
|
42
|
-
format.json { render_for_api params[:api_template].to_sym, :
|
40
|
+
format.xml { render_for_api params[:api_template].to_sym, xml: @user, root: :user }
|
41
|
+
format.json { render_for_api params[:api_template].to_sym, json: @user, root: :user }
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
46
45
|
def show_meta
|
47
46
|
@user = User.find(params[:id])
|
48
|
-
meta_hash = { :
|
47
|
+
meta_hash = { page: 1, total: 999 }
|
49
48
|
respond_to do |format|
|
50
49
|
# :root => :user is only used here because we need it for the node name of the MongoUser model
|
51
|
-
format.xml { render_for_api params[:api_template].to_sym, :
|
52
|
-
format.json { render_for_api params[:api_template].to_sym, :
|
50
|
+
format.xml { render_for_api params[:api_template].to_sym, xml: @user, root: :user }
|
51
|
+
format.json { render_for_api params[:api_template].to_sym, json: @user, root: :user, meta: meta_hash }
|
53
52
|
end
|
54
53
|
end
|
55
54
|
|
56
55
|
def show_default
|
57
56
|
@user = User.find(params[:id])
|
58
57
|
respond_to do |format|
|
59
|
-
format.xml
|
60
|
-
format.json { render :
|
58
|
+
format.xml { render xml: @user }
|
59
|
+
format.json { render json: @user }
|
61
60
|
end
|
62
61
|
end
|
63
62
|
|
64
63
|
def show_prefix_postfix
|
65
64
|
@user = User.find(params[:id])
|
66
|
-
template = {:
|
65
|
+
template = { template: params[:api_template], prefix: params[:api_prefix], postfix: params[:api_postfix] }
|
67
66
|
respond_to do |format|
|
68
67
|
# :root => :user is only used here because we need it for the node name of the MongoUser model
|
69
|
-
format.xml
|
70
|
-
format.json { render_for_api template, :
|
68
|
+
format.xml { render_for_api template, xml: @user, root: :user }
|
69
|
+
format.json { render_for_api template, json: @user, root: :user }
|
71
70
|
end
|
72
71
|
end
|
73
|
-
|
74
72
|
end
|
75
73
|
end
|
@@ -12,11 +12,11 @@ module UserTemplate
|
|
12
12
|
end
|
13
13
|
|
14
14
|
api_accessible :rename_last_name do |t|
|
15
|
-
t.add :last_name, :
|
15
|
+
t.add :last_name, as: :family_name
|
16
16
|
end
|
17
17
|
|
18
18
|
api_accessible :rename_full_name do |t|
|
19
|
-
t.add :full_name, :
|
19
|
+
t.add :full_name, as: :other_full_name
|
20
20
|
end
|
21
21
|
|
22
22
|
api_accessible :with_former_value do |t|
|
@@ -24,19 +24,19 @@ module UserTemplate
|
|
24
24
|
t.add :last_name
|
25
25
|
end
|
26
26
|
|
27
|
-
api_accessible :age_and_first_name, :
|
27
|
+
api_accessible :age_and_first_name, extend: :with_former_value do |t|
|
28
28
|
t.add :age
|
29
29
|
t.remove :last_name
|
30
30
|
end
|
31
31
|
|
32
32
|
api_accessible :calling_a_proc do |t|
|
33
|
-
t.add
|
34
|
-
t.add
|
33
|
+
t.add proc { |model| model.full_name.upcase }, as: :all_caps_name
|
34
|
+
t.add proc { |_| Time.now.class.to_s }, as: :without_param
|
35
35
|
end
|
36
36
|
|
37
37
|
api_accessible :calling_a_lambda do |t|
|
38
|
-
t.add
|
39
|
-
t.add
|
38
|
+
t.add ->(model) { model.full_name.upcase }, as: :all_caps_name
|
39
|
+
t.add ->(_) { Time.now.class.to_s }, as: :without_param
|
40
40
|
end
|
41
41
|
api_accessible :include_tasks do |t|
|
42
42
|
t.add :tasks
|
@@ -48,19 +48,19 @@ module UserTemplate
|
|
48
48
|
|
49
49
|
api_accessible :other_sub_template do |t|
|
50
50
|
t.add :first_name
|
51
|
-
t.add :tasks, :
|
51
|
+
t.add :tasks, template: :other_template
|
52
52
|
end
|
53
53
|
|
54
54
|
api_accessible :include_completed_tasks do |t|
|
55
|
-
t.add
|
55
|
+
t.add 'tasks.completed.all', as: :completed_tasks
|
56
56
|
end
|
57
57
|
|
58
58
|
api_accessible :sub_node do |t|
|
59
|
-
t.add Hash[:
|
59
|
+
t.add Hash[foo: :say_something], as: :sub_nodes
|
60
60
|
end
|
61
61
|
|
62
62
|
api_accessible :nested_sub_node do |t|
|
63
|
-
t.add Hash[:foo, Hash[:bar, :last_name]], :
|
63
|
+
t.add Hash[:foo, Hash[:bar, :last_name]], as: :sub_nodes
|
64
64
|
end
|
65
65
|
|
66
66
|
api_accessible :nested_sub_hash do |t|
|
@@ -69,46 +69,46 @@ module UserTemplate
|
|
69
69
|
|
70
70
|
api_accessible :if_over_thirty do |t|
|
71
71
|
t.add :first_name
|
72
|
-
t.add :last_name, :
|
72
|
+
t.add :last_name, if: :over_thirty?
|
73
73
|
end
|
74
74
|
|
75
75
|
api_accessible :if_returns_nil do |t|
|
76
76
|
t.add :first_name
|
77
|
-
t.add :last_name, :
|
77
|
+
t.add :last_name, if: :return_nil
|
78
78
|
end
|
79
79
|
|
80
80
|
api_accessible :if_over_thirty_proc do |t|
|
81
81
|
t.add :first_name
|
82
|
-
t.add :last_name, :
|
82
|
+
t.add :last_name, if: ->(model) { model.over_thirty? }
|
83
83
|
end
|
84
84
|
|
85
85
|
api_accessible :if_returns_nil_proc do |t|
|
86
86
|
t.add :first_name
|
87
|
-
t.add :last_name, :
|
87
|
+
t.add :last_name, if: ->(_) { nil }
|
88
88
|
end
|
89
89
|
|
90
90
|
api_accessible :unless_under_thirty do |t|
|
91
91
|
t.add :first_name
|
92
|
-
t.add :last_name, :
|
92
|
+
t.add :last_name, unless: :under_thirty?
|
93
93
|
end
|
94
94
|
|
95
95
|
api_accessible :unless_returns_nil do |t|
|
96
96
|
t.add :first_name
|
97
|
-
t.add :last_name, :
|
97
|
+
t.add :last_name, unless: :return_nil
|
98
98
|
end
|
99
99
|
|
100
100
|
api_accessible :unless_under_thirty_proc do |t|
|
101
101
|
t.add :first_name
|
102
|
-
t.add :last_name, :
|
102
|
+
t.add :last_name, unless: ->(model) { model.under_thirty? }
|
103
103
|
end
|
104
104
|
|
105
105
|
api_accessible :unless_returns_nil_proc do |t|
|
106
106
|
t.add :first_name
|
107
|
-
t.add :last_name, :
|
107
|
+
t.add :last_name, unless: ->(_) { nil }
|
108
108
|
end
|
109
109
|
|
110
110
|
api_accessible :with_prefix_name_only do |t|
|
111
|
-
t.add
|
111
|
+
t.add ->(_) { 'true' }, as: :prefix
|
112
112
|
t.add :first_name
|
113
113
|
t.add :last_name
|
114
114
|
end
|
@@ -116,17 +116,17 @@ module UserTemplate
|
|
116
116
|
api_accessible :name_only_with_postfix do |t|
|
117
117
|
t.add :first_name
|
118
118
|
t.add :last_name
|
119
|
-
t.add
|
119
|
+
t.add ->(_) { 'true' }, as: :postfix
|
120
120
|
end
|
121
121
|
|
122
122
|
api_accessible :with_prefix_name_only_with_postfix do |t|
|
123
|
-
t.add
|
123
|
+
t.add ->(_) { 'true' }, as: :prefix
|
124
124
|
t.add :first_name
|
125
125
|
t.add :last_name
|
126
|
-
t.add
|
126
|
+
t.add ->(_) { 'true' }, as: :postfix
|
127
127
|
end
|
128
128
|
|
129
|
-
def before_api_response(
|
129
|
+
def before_api_response(_api_response)
|
130
130
|
@before_api_response_called = true
|
131
131
|
end
|
132
132
|
|
@@ -134,7 +134,7 @@ module UserTemplate
|
|
134
134
|
!!@before_api_response_called
|
135
135
|
end
|
136
136
|
|
137
|
-
def after_api_response(
|
137
|
+
def after_api_response(_api_response)
|
138
138
|
@after_api_response_called = true
|
139
139
|
end
|
140
140
|
|
@@ -146,9 +146,8 @@ module UserTemplate
|
|
146
146
|
@skip_api_response = should_skip
|
147
147
|
end
|
148
148
|
|
149
|
-
def around_api_response(
|
150
|
-
@skip_api_response ? { :
|
149
|
+
def around_api_response(_api_response)
|
150
|
+
@skip_api_response ? { skipped: true } : yield
|
151
151
|
end
|
152
|
-
|
153
152
|
end
|
154
|
-
end
|
153
|
+
end
|
@@ -1,21 +1,19 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
2
2
|
|
3
3
|
# Maintain your gem's version:
|
4
|
-
require
|
4
|
+
require 'shared_engine/version'
|
5
5
|
|
6
6
|
# Describe your gem and declare its dependencies:
|
7
7
|
Gem::Specification.new do |s|
|
8
|
-
s.name =
|
8
|
+
s.name = 'shared_engine'
|
9
9
|
s.version = SharedEngine::VERSION
|
10
|
-
s.authors = [
|
11
|
-
s.email = [
|
12
|
-
s.summary =
|
13
|
-
s.description =
|
10
|
+
s.authors = ['Your name']
|
11
|
+
s.email = ['Your email']
|
12
|
+
s.summary = 'Summary of SharedEngine.'
|
13
|
+
s.description = 'Description of SharedEngine.'
|
14
14
|
|
15
|
-
s.files = Dir[
|
15
|
+
s.files = Dir['{app,config,db,lib}/**/*'] + ['MIT-LICENSE', 'Rakefile', 'README.rdoc']
|
16
16
|
|
17
|
-
s.add_dependency
|
18
|
-
|
19
|
-
|
20
|
-
s.add_development_dependency "sqlite3"
|
17
|
+
s.add_dependency 'rails', '~> 5.0.0.1'
|
18
|
+
s.add_development_dependency 'sqlite3'
|
21
19
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,24 +1,16 @@
|
|
1
|
-
ENV[
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
2
|
|
3
3
|
if ENV['ACTS_AS_API_ORM'] == 'active_record'
|
4
|
-
require
|
4
|
+
require 'active_record_dummy/config/environment'
|
5
5
|
|
6
|
-
load "#{Rails.root
|
6
|
+
load "#{Rails.root}/db/schema.rb" # use db agnostic schema by default
|
7
7
|
# ActiveRecord::Migrator.up('db/migrate') # use migrations
|
8
|
-
|
9
|
-
|
10
8
|
elsif ENV['ACTS_AS_API_ORM'] == 'mongoid'
|
11
|
-
|
12
|
-
require "mongoid_dummy/config/environment"
|
13
|
-
|
9
|
+
require 'mongoid_dummy/config/environment'
|
14
10
|
end
|
15
11
|
|
16
12
|
require 'rspec/rails'
|
17
13
|
|
18
14
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
19
15
|
# in spec/support/ and its subdirectories.
|
20
|
-
Dir[File.dirname(__FILE__) +
|
21
|
-
|
22
|
-
RSpec.configure do |config|
|
23
|
-
config.expect_with(:rspec) { |c| c.syntax = :should }
|
24
|
-
end
|
16
|
+
Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |file| require file }
|
@@ -1,128 +1,115 @@
|
|
1
|
-
shared_examples_for
|
1
|
+
shared_examples_for 'a controller with ActsAsApi responses' do
|
2
2
|
include ApiTestHelpers
|
3
3
|
routes { SharedEngine::Engine.routes }
|
4
4
|
|
5
5
|
describe 'xml responses' do
|
6
|
-
|
7
6
|
describe 'get all users' do
|
8
|
-
|
9
7
|
before(:each) do
|
10
8
|
get :index, format: 'xml', params: { api_template: :name_only }
|
11
9
|
end
|
12
10
|
|
13
|
-
it
|
14
|
-
response_body.
|
11
|
+
it 'should have a root node named users' do
|
12
|
+
expect(response_body).to have_selector('users')
|
15
13
|
end
|
16
14
|
|
17
|
-
it
|
18
|
-
response_body.
|
19
|
-
users.size.
|
15
|
+
it 'should contain all users' do
|
16
|
+
expect(response_body).to have_selector('users > user') do |users|
|
17
|
+
expect(users.size).to eql(3)
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
23
|
-
it
|
24
|
-
response_body.
|
25
|
-
response_body.
|
21
|
+
it 'should contain the specified attributes' do
|
22
|
+
expect(response_body).to have_selector('users > user > first-name')
|
23
|
+
expect(response_body).to have_selector('users > user > last-name')
|
26
24
|
end
|
27
|
-
|
28
25
|
end
|
29
26
|
|
30
27
|
describe 'get a single user' do
|
31
|
-
|
32
28
|
before(:each) do
|
33
29
|
get :show, format: 'xml', params: { api_template: :name_only, id: @luke.id }
|
34
30
|
end
|
35
31
|
|
36
|
-
it
|
37
|
-
response_body.
|
32
|
+
it 'should have a root node named user' do
|
33
|
+
expect(response_body).to have_selector('user')
|
38
34
|
end
|
39
35
|
|
40
|
-
it
|
41
|
-
response_body.
|
42
|
-
response_body.
|
36
|
+
it 'should contain the specified attributes' do
|
37
|
+
expect(response_body).to have_selector('user > first-name')
|
38
|
+
expect(response_body).to have_selector('user > last-name')
|
43
39
|
end
|
44
|
-
|
45
40
|
end
|
46
41
|
end
|
47
42
|
|
48
43
|
describe 'json responses' do
|
49
|
-
|
50
44
|
describe 'get all users' do
|
51
|
-
|
52
45
|
before(:each) do
|
53
46
|
get :index, format: 'json', params: { api_template: :name_only }
|
54
47
|
end
|
55
48
|
|
56
|
-
it
|
57
|
-
response_body_json.
|
49
|
+
it 'should have a root node named users' do
|
50
|
+
expect(response_body_json).to have_key('users')
|
58
51
|
end
|
59
52
|
|
60
|
-
it
|
61
|
-
response_body_json[
|
53
|
+
it 'should contain all users' do
|
54
|
+
expect(response_body_json['users']).to be_a(Array)
|
62
55
|
end
|
63
56
|
|
64
|
-
it
|
65
|
-
response_body_json[
|
66
|
-
response_body_json[
|
57
|
+
it 'should contain the specified attributes' do
|
58
|
+
expect(response_body_json['users'].first).to have_key('first_name')
|
59
|
+
expect(response_body_json['users'].first).to have_key('last_name')
|
67
60
|
end
|
68
61
|
|
69
|
-
it
|
70
|
-
response_body_json[
|
71
|
-
response_body_json[
|
62
|
+
it 'should contain the specified values' do
|
63
|
+
expect(response_body_json['users'].first['first_name']).to eql('Han')
|
64
|
+
expect(response_body_json['users'].first['last_name']).to eql('Solo')
|
72
65
|
end
|
73
|
-
|
74
66
|
end
|
75
67
|
|
76
68
|
describe 'get all users as a ActiveRecord::Relation (or similar chained) object, autodetecting the root node name' do
|
77
|
-
|
78
69
|
before(:each) do
|
79
70
|
get :index_relation, format: 'json', params: { api_template: :name_only }
|
80
71
|
end
|
81
72
|
|
82
|
-
it
|
83
|
-
response_body_json.
|
73
|
+
it 'should have a root node named users' do
|
74
|
+
expect(response_body_json).to have_key('users')
|
84
75
|
end
|
85
76
|
|
86
|
-
it
|
87
|
-
response_body_json[
|
77
|
+
it 'should contain all users' do
|
78
|
+
expect(response_body_json['users']).to be_a(Array)
|
88
79
|
end
|
89
80
|
|
90
|
-
it
|
91
|
-
response_body_json[
|
92
|
-
response_body_json[
|
81
|
+
it 'should contain the specified attributes' do
|
82
|
+
expect(response_body_json['users'].first).to have_key('first_name')
|
83
|
+
expect(response_body_json['users'].first).to have_key('last_name')
|
93
84
|
end
|
94
85
|
|
95
|
-
it
|
96
|
-
response_body_json[
|
97
|
-
response_body_json[
|
86
|
+
it 'should contain the specified values' do
|
87
|
+
expect(response_body_json['users'].first['first_name']).to eql('Han')
|
88
|
+
expect(response_body_json['users'].first['last_name']).to eql('Solo')
|
98
89
|
end
|
99
|
-
|
100
90
|
end
|
101
91
|
|
102
92
|
describe 'get a single user' do
|
103
|
-
|
104
93
|
before(:each) do
|
105
94
|
get :show, format: 'json', params: { api_template: :name_only, id: @luke.id }
|
106
95
|
end
|
107
96
|
|
108
|
-
it
|
109
|
-
response_body_json.
|
97
|
+
it 'should have a root node named user' do
|
98
|
+
expect(response_body_json).to have_key('user')
|
110
99
|
end
|
111
100
|
|
112
|
-
it
|
113
|
-
response_body_json[
|
114
|
-
response_body_json[
|
101
|
+
it 'should contain the specified attributes' do
|
102
|
+
expect(response_body_json['user']).to have_key('first_name')
|
103
|
+
expect(response_body_json['user']).to have_key('last_name')
|
115
104
|
end
|
116
105
|
|
117
|
-
it
|
118
|
-
response_body_json[
|
119
|
-
response_body_json[
|
106
|
+
it 'should contain the specified values' do
|
107
|
+
expect(response_body_json['user']['first_name']).to eql('Luke')
|
108
|
+
expect(response_body_json['user']['last_name']).to eql('Skywalker')
|
120
109
|
end
|
121
|
-
|
122
110
|
end
|
123
111
|
|
124
112
|
describe 'get a single user with a nil profile' do
|
125
|
-
|
126
113
|
before(:each) do
|
127
114
|
Profile.acts_as_api
|
128
115
|
Profile.api_accessible :include_profile do |t|
|
@@ -133,41 +120,36 @@ shared_examples_for "a controller with ActsAsApi responses" do
|
|
133
120
|
get :show, format: 'json', params: { api_template: :include_profile, id: @han.id }
|
134
121
|
end
|
135
122
|
|
136
|
-
it
|
137
|
-
response_body_json.
|
123
|
+
it 'should have a root node named user' do
|
124
|
+
expect(response_body_json).to have_key('user')
|
138
125
|
end
|
139
126
|
|
140
|
-
it
|
141
|
-
response_body_json[
|
142
|
-
response_body_json[
|
127
|
+
it 'should contain the specified attributes' do
|
128
|
+
expect(response_body_json['user']).to have(1).keys
|
129
|
+
expect(response_body_json['user']).to have_key('profile')
|
143
130
|
end
|
144
131
|
|
145
|
-
it
|
146
|
-
response_body_json[
|
132
|
+
it 'should contain the specified values' do
|
133
|
+
expect(response_body_json['user']['profile']).to be_nil
|
147
134
|
end
|
148
|
-
|
149
135
|
end
|
150
136
|
|
151
137
|
describe 'get a user without specifying an api template' do
|
152
|
-
|
153
138
|
before(:each) do
|
154
139
|
get :show_default, format: 'json', params: { id: @luke.id }
|
155
140
|
end
|
156
141
|
|
157
|
-
it
|
158
|
-
response.code.
|
142
|
+
it 'should respond with HTTP 200' do
|
143
|
+
expect(response.code).to eq('200')
|
159
144
|
end
|
160
145
|
|
161
|
-
it
|
162
|
-
response.body.
|
146
|
+
it 'should render the model with to_json' do
|
147
|
+
expect(response.body).to eq(@luke.to_json)
|
163
148
|
end
|
164
|
-
|
165
149
|
end
|
166
|
-
|
167
150
|
end
|
168
151
|
|
169
152
|
describe 'Rails 3 default style json responses' do
|
170
|
-
|
171
153
|
before(:each) do
|
172
154
|
@org_include_root_in_json_collections = ActsAsApi::Config.include_root_in_json_collections
|
173
155
|
ActsAsApi::Config.include_root_in_json_collections = true
|
@@ -178,70 +160,64 @@ shared_examples_for "a controller with ActsAsApi responses" do
|
|
178
160
|
end
|
179
161
|
|
180
162
|
describe 'get all users' do
|
181
|
-
|
182
163
|
before(:each) do
|
183
164
|
get :index, format: 'json', params: { api_template: :name_only }
|
184
165
|
end
|
185
166
|
|
186
|
-
it
|
187
|
-
response_body_json.
|
167
|
+
it 'should have a root node named users' do
|
168
|
+
expect(response_body_json).to have_key('users')
|
188
169
|
end
|
189
170
|
|
190
|
-
it
|
191
|
-
response_body_json[
|
171
|
+
it 'should contain all users' do
|
172
|
+
expect(response_body_json['users']).to be_a(Array)
|
192
173
|
end
|
193
174
|
|
194
|
-
it
|
195
|
-
response_body_json[
|
196
|
-
response_body_json[
|
175
|
+
it 'should contain the specified attributes' do
|
176
|
+
expect(response_body_json['users'].first['user']).to have_key('first_name')
|
177
|
+
expect(response_body_json['users'].first['user']).to have_key('last_name')
|
197
178
|
end
|
198
179
|
|
199
|
-
it
|
200
|
-
response_body_json[
|
180
|
+
it 'contains the user root nodes' do
|
181
|
+
expect(response_body_json['users'].collect(&:keys).flatten.uniq).to eql(['user'])
|
201
182
|
end
|
202
183
|
|
203
|
-
it
|
204
|
-
response_body_json[
|
205
|
-
response_body_json[
|
184
|
+
it 'should contain the specified values' do
|
185
|
+
expect(response_body_json['users'].first['user']['first_name']).to eql('Han')
|
186
|
+
expect(response_body_json['users'].first['user']['last_name']).to eql('Solo')
|
206
187
|
end
|
207
|
-
|
208
188
|
end
|
209
189
|
|
210
190
|
describe 'get a single user' do
|
211
|
-
|
212
191
|
before(:each) do
|
213
192
|
get :show, format: 'json', params: { api_template: :name_only, id: @luke.id }
|
214
193
|
end
|
215
194
|
|
216
|
-
it
|
217
|
-
response_body_json.
|
195
|
+
it 'should have a root node named user' do
|
196
|
+
expect(response_body_json).to have_key('user')
|
218
197
|
end
|
219
198
|
|
220
|
-
it
|
221
|
-
response_body_json[
|
222
|
-
response_body_json[
|
199
|
+
it 'should contain the specified attributes' do
|
200
|
+
expect(response_body_json['user']).to have_key('first_name')
|
201
|
+
expect(response_body_json['user']).to have_key('last_name')
|
223
202
|
end
|
224
203
|
|
225
|
-
it
|
226
|
-
response_body_json[
|
227
|
-
response_body_json[
|
204
|
+
it 'should contain the specified values' do
|
205
|
+
expect(response_body_json['user']['first_name']).to eql('Luke')
|
206
|
+
expect(response_body_json['user']['last_name']).to eql('Skywalker')
|
228
207
|
end
|
229
208
|
end
|
230
|
-
|
231
209
|
end
|
232
210
|
|
233
211
|
describe 'jsonp responses with callback' do
|
234
|
-
|
235
|
-
|
236
|
-
@callback = "mycallback"
|
212
|
+
it 'should be disabled by default' do
|
213
|
+
@callback = 'mycallback'
|
237
214
|
get :index, format: 'json', params: { api_template: :name_only, callback: @callback }
|
238
|
-
response_body_jsonp(@callback).
|
215
|
+
expect(response_body_jsonp(@callback)).to be_nil
|
239
216
|
end
|
240
217
|
|
241
|
-
describe
|
242
|
-
|
218
|
+
describe 'enabled jsonp callbacks' do
|
243
219
|
before(:each) do
|
244
|
-
@callback =
|
220
|
+
@callback = 'mycallback'
|
245
221
|
|
246
222
|
User.acts_as_api do |config|
|
247
223
|
config.allow_jsonp_callback = true
|
@@ -256,27 +232,23 @@ shared_examples_for "a controller with ActsAsApi responses" do
|
|
256
232
|
end
|
257
233
|
|
258
234
|
describe 'get all users' do
|
259
|
-
|
260
235
|
before(:each) do
|
261
236
|
get :index, format: 'json', params: { api_template: :name_only, callback: @callback }
|
262
237
|
end
|
263
238
|
|
264
|
-
it
|
265
|
-
response_body_jsonp(@callback).
|
239
|
+
it 'should wrap the response in the callback' do
|
240
|
+
expect(response_body_jsonp(@callback)).not_to be_nil
|
266
241
|
end
|
267
|
-
|
268
242
|
end
|
269
243
|
|
270
244
|
describe 'get a single user' do
|
271
|
-
|
272
245
|
before(:each) do
|
273
246
|
get :show, format: 'json', params: { api_template: :name_only, id: @luke.id, callback: @callback }
|
274
247
|
end
|
275
248
|
|
276
|
-
it
|
277
|
-
response_body_jsonp(@callback).
|
249
|
+
it 'should wrap the response in the callback' do
|
250
|
+
expect(response_body_jsonp(@callback)).not_to be_nil
|
278
251
|
end
|
279
|
-
|
280
252
|
end
|
281
253
|
|
282
254
|
describe 'Requesting the JSONP content as JavaScript' do
|
@@ -284,8 +256,8 @@ shared_examples_for "a controller with ActsAsApi responses" do
|
|
284
256
|
get :index, format: :json, params: { api_template: :name_only, callback: @callback }
|
285
257
|
end
|
286
258
|
|
287
|
-
it
|
288
|
-
response.content_type.
|
259
|
+
it 'should set the content type to JavaScript' do
|
260
|
+
expect(response.content_type).to eq(Mime[:js])
|
289
261
|
end
|
290
262
|
end
|
291
263
|
end
|
@@ -306,8 +278,8 @@ shared_examples_for "a controller with ActsAsApi responses" do
|
|
306
278
|
get :index, format: 'json', params: { api_template: :name_only, callback: @callback }
|
307
279
|
end
|
308
280
|
|
309
|
-
|
310
|
-
response_body_json.
|
281
|
+
it 'response has no named root node' do
|
282
|
+
expect(response_body_json).to be_an(Array)
|
311
283
|
end
|
312
284
|
end
|
313
285
|
|
@@ -316,32 +288,31 @@ shared_examples_for "a controller with ActsAsApi responses" do
|
|
316
288
|
get :show, format: 'json', params: { api_template: :name_only, id: @luke.id }
|
317
289
|
end
|
318
290
|
|
319
|
-
|
320
|
-
response_body_json.
|
321
|
-
response_body_json.
|
291
|
+
it 'response has no named root node' do
|
292
|
+
expect(response_body_json).to be_a(Hash)
|
293
|
+
expect(response_body_json.keys).to include('first_name')
|
322
294
|
end
|
323
295
|
end
|
324
296
|
end
|
325
297
|
|
326
298
|
describe 'pass meta information on rendering' do
|
327
|
-
|
328
299
|
describe 'get all users' do
|
329
300
|
before(:each) do
|
330
301
|
get :index_meta, format: 'json', params: { api_template: :name_only }
|
331
302
|
end
|
332
303
|
|
333
|
-
it
|
334
|
-
response_body_json.
|
335
|
-
response_body_json.
|
336
|
-
response_body_json[
|
304
|
+
it 'shows model response fields' do
|
305
|
+
expect(response_body_json).to be_a(Hash)
|
306
|
+
expect(response_body_json).to have_key('users')
|
307
|
+
expect(response_body_json['users']).to be_an(Array)
|
337
308
|
end
|
338
309
|
|
339
|
-
it
|
340
|
-
response_body_json.
|
310
|
+
it 'shows page field' do
|
311
|
+
expect(response_body_json).to have_key('page')
|
341
312
|
end
|
342
313
|
|
343
|
-
it
|
344
|
-
response_body_json.
|
314
|
+
it 'shows total field' do
|
315
|
+
expect(response_body_json).to have_key('total')
|
345
316
|
end
|
346
317
|
end
|
347
318
|
|
@@ -350,97 +321,81 @@ shared_examples_for "a controller with ActsAsApi responses" do
|
|
350
321
|
get :show_meta, format: 'json', params: { api_template: :name_only, id: @luke.id }
|
351
322
|
end
|
352
323
|
|
353
|
-
it
|
354
|
-
response_body_json.
|
355
|
-
response_body_json.
|
324
|
+
it 'shows model response fields' do
|
325
|
+
expect(response_body_json).to be_a(Hash)
|
326
|
+
expect(response_body_json).to have_key('user')
|
356
327
|
end
|
357
328
|
|
358
|
-
it
|
359
|
-
response_body_json.
|
329
|
+
it 'shows page field' do
|
330
|
+
expect(response_body_json).to have_key('page')
|
360
331
|
end
|
361
332
|
|
362
|
-
it
|
363
|
-
response_body_json.
|
333
|
+
it 'shows total field' do
|
334
|
+
expect(response_body_json).to have_key('total')
|
364
335
|
end
|
365
|
-
|
366
336
|
end
|
367
337
|
end
|
368
338
|
|
369
|
-
|
370
339
|
describe 'api prefix' do
|
371
|
-
|
372
340
|
describe 'get single user' do
|
373
|
-
|
374
341
|
before(:each) do
|
375
342
|
get :show_prefix_postfix, format: 'xml', params: { api_template: :name_only, api_prefix: :with_prefix, id: @luke.id }
|
376
|
-
|
377
343
|
end
|
378
344
|
|
379
|
-
it
|
380
|
-
response_body.
|
345
|
+
it 'should have a root node named user' do
|
346
|
+
expect(response_body).to have_selector('user')
|
381
347
|
end
|
382
348
|
|
383
|
-
it
|
384
|
-
response_body.
|
385
|
-
response_body.
|
386
|
-
response_body.
|
349
|
+
it 'should contain the specified attributes' do
|
350
|
+
expect(response_body).to have_selector('user > prefix')
|
351
|
+
expect(response_body).to have_selector('user > first-name')
|
352
|
+
expect(response_body).to have_selector('user > last-name')
|
387
353
|
end
|
388
354
|
|
389
|
-
it
|
390
|
-
response_body.
|
355
|
+
it 'should not contain the specified attributes' do
|
356
|
+
expect(response_body).not_to have_selector('user > postfix')
|
391
357
|
end
|
392
|
-
|
393
358
|
end
|
394
|
-
|
395
359
|
end
|
396
360
|
|
397
361
|
describe 'api postfix' do
|
398
|
-
|
399
362
|
describe 'get single user' do
|
400
|
-
|
401
363
|
before(:each) do
|
402
364
|
get :show_prefix_postfix, format: 'xml', params: { api_template: :name_only, api_postfix: :with_postfix, id: @luke.id }
|
403
365
|
end
|
404
366
|
|
405
|
-
it
|
406
|
-
response_body.
|
367
|
+
it 'should have a root node named user' do
|
368
|
+
expect(response_body).to have_selector('user')
|
407
369
|
end
|
408
370
|
|
409
|
-
it
|
410
|
-
response_body.
|
411
|
-
response_body.
|
412
|
-
response_body.
|
371
|
+
it 'should contain the specified attributes' do
|
372
|
+
expect(response_body).to have_selector('user > first-name')
|
373
|
+
expect(response_body).to have_selector('user > last-name')
|
374
|
+
expect(response_body).to have_selector('user > postfix')
|
413
375
|
end
|
414
376
|
|
415
|
-
it
|
416
|
-
response_body.
|
377
|
+
it 'should not contain the specified attributes' do
|
378
|
+
expect(response_body).not_to have_selector('user > prefix')
|
417
379
|
end
|
418
|
-
|
419
380
|
end
|
420
|
-
|
421
381
|
end
|
422
382
|
|
423
383
|
describe 'api prefix and api postfix' do
|
424
|
-
|
425
384
|
describe 'get single user' do
|
426
|
-
|
427
385
|
before(:each) do
|
428
|
-
get :show_prefix_postfix, format: 'xml', params: { api_template: :name_only, api_prefix: :with_prefix, api_postfix: :with_postfix, :
|
386
|
+
get :show_prefix_postfix, format: 'xml', params: { api_template: :name_only, api_prefix: :with_prefix, api_postfix: :with_postfix, id: @luke.id }
|
429
387
|
end
|
430
388
|
|
431
|
-
it
|
432
|
-
response_body.
|
389
|
+
it 'should have a root node named user' do
|
390
|
+
expect(response_body).to have_selector('user')
|
433
391
|
end
|
434
392
|
|
435
|
-
it
|
436
|
-
response_body.
|
437
|
-
response_body.
|
438
|
-
response_body.
|
439
|
-
response_body.
|
393
|
+
it 'should contain the specified attributes' do
|
394
|
+
expect(response_body).to have_selector('user > prefix')
|
395
|
+
expect(response_body).to have_selector('user > first-name')
|
396
|
+
expect(response_body).to have_selector('user > last-name')
|
397
|
+
expect(response_body).to have_selector('user > postfix')
|
440
398
|
end
|
441
|
-
|
442
399
|
end
|
443
|
-
|
444
400
|
end
|
445
|
-
|
446
401
|
end
|