acts_as_api 0.4.4 → 1.0.0
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.
- 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
data/lib/acts_as_api/base.rb
CHANGED
|
@@ -10,7 +10,6 @@ module ActsAsApi
|
|
|
10
10
|
# When invoked, it enriches the current model with the
|
|
11
11
|
# class and instance methods to act as api.
|
|
12
12
|
def acts_as_api
|
|
13
|
-
|
|
14
13
|
class_eval do
|
|
15
14
|
include ActsAsApi::Base::InstanceMethods
|
|
16
15
|
extend ActsAsApi::Base::ClassMethods
|
|
@@ -19,13 +18,11 @@ module ActsAsApi
|
|
|
19
18
|
if block_given?
|
|
20
19
|
yield ActsAsApi::Config
|
|
21
20
|
end
|
|
22
|
-
|
|
23
21
|
end
|
|
24
22
|
|
|
25
23
|
module ClassMethods
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
self.included_modules.include?(InstanceMethods)
|
|
24
|
+
def acts_as_api? #:nodoc:
|
|
25
|
+
included_modules.include?(InstanceMethods)
|
|
29
26
|
end
|
|
30
27
|
|
|
31
28
|
# Determines the attributes, methods of the model that are accessible in the api response.
|
|
@@ -33,9 +30,7 @@ module ActsAsApi
|
|
|
33
30
|
# So once the model acts as api, you have to determine all attributes here that should
|
|
34
31
|
# be contained in the api responses.
|
|
35
32
|
def api_accessible(api_template, options = {}, &block)
|
|
36
|
-
|
|
37
33
|
attributes = api_accessible_attributes(api_template).try(:dup) || ApiTemplate.new(api_template)
|
|
38
|
-
|
|
39
34
|
attributes.merge!(api_accessible_attributes(options[:extend])) if options[:extend]
|
|
40
35
|
|
|
41
36
|
if block_given?
|
|
@@ -53,12 +48,11 @@ module ActsAsApi
|
|
|
53
48
|
end
|
|
54
49
|
|
|
55
50
|
module InstanceMethods
|
|
56
|
-
|
|
57
51
|
# Creates the api response of the model and returns it as a Hash.
|
|
58
52
|
# Will raise an exception if the passed api template is not defined for the model
|
|
59
53
|
def as_api_response(api_template, options = {})
|
|
60
54
|
api_attributes = self.class.api_accessible_attributes(api_template)
|
|
61
|
-
raise ActsAsApi::TemplateNotFoundError.new("acts_as_api template :#{api_template
|
|
55
|
+
raise ActsAsApi::TemplateNotFoundError.new("acts_as_api template :#{api_template} was not found for model #{self.class}") if api_attributes.nil?
|
|
62
56
|
|
|
63
57
|
before_api_response(api_template)
|
|
64
58
|
response_hash = around_api_response(api_template) do
|
|
@@ -69,20 +63,17 @@ module ActsAsApi
|
|
|
69
63
|
response_hash
|
|
70
64
|
end
|
|
71
65
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def before_api_response(api_remplate)
|
|
75
|
-
end
|
|
66
|
+
protected
|
|
76
67
|
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
def before_api_response(_api_template)
|
|
69
|
+
end
|
|
79
70
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
end
|
|
71
|
+
def after_api_response(_api_template)
|
|
72
|
+
end
|
|
83
73
|
|
|
74
|
+
def around_api_response(_api_template)
|
|
75
|
+
yield
|
|
76
|
+
end
|
|
84
77
|
end
|
|
85
|
-
|
|
86
78
|
end
|
|
87
|
-
|
|
88
79
|
end
|
|
@@ -6,12 +6,11 @@ module ActsAsApi
|
|
|
6
6
|
def as_api_response(api_template, options = {})
|
|
7
7
|
collect do |item|
|
|
8
8
|
if item.respond_to?(:as_api_response)
|
|
9
|
-
item.as_api_response(api_template,options)
|
|
9
|
+
item.as_api_response(api_template, options)
|
|
10
10
|
else
|
|
11
11
|
item
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
|
-
|
|
16
15
|
end
|
|
17
16
|
end
|
data/lib/acts_as_api/config.rb
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
module ActsAsApi
|
|
2
|
-
|
|
3
2
|
module Config
|
|
4
|
-
|
|
5
3
|
class << self
|
|
6
|
-
|
|
7
4
|
attr_writer :accepted_api_formats, :dasherize_for, :include_root_in_json_collections, :add_root_node_for, :default_root, :allow_jsonp_callback, :add_http_status_to_jsonp_response
|
|
8
5
|
|
|
9
6
|
# The accepted response formats
|
|
@@ -52,7 +49,5 @@ module ActsAsApi
|
|
|
52
49
|
@add_http_status_to_jsonp_response.nil? ? true : @add_http_status_to_jsonp_response
|
|
53
50
|
end
|
|
54
51
|
end
|
|
55
|
-
|
|
56
52
|
end
|
|
57
|
-
|
|
58
53
|
end
|
|
@@ -3,7 +3,6 @@ module ActsAsApi
|
|
|
3
3
|
#
|
|
4
4
|
# See ActsAsApi::Config about the possible configurations
|
|
5
5
|
module RailsRenderer
|
|
6
|
-
|
|
7
6
|
def self.setup
|
|
8
7
|
ActionController.add_renderer :acts_as_api_jsonp do |json, options|
|
|
9
8
|
json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
|
|
@@ -16,6 +15,5 @@ module ActsAsApi
|
|
|
16
15
|
self.response_body = json
|
|
17
16
|
end
|
|
18
17
|
end
|
|
19
|
-
|
|
20
18
|
end
|
|
21
19
|
end
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
module ActsAsApi
|
|
2
|
-
|
|
3
2
|
# The methods of this module are included into the AbstractController::Rendering
|
|
4
3
|
# module.
|
|
5
4
|
module Rendering
|
|
6
|
-
|
|
7
5
|
# Provides an alternative to the +render+ method used within the controller
|
|
8
6
|
# to simply generate API outputs.
|
|
9
7
|
#
|
|
@@ -23,13 +21,13 @@ module ActsAsApi
|
|
|
23
21
|
api_format_options = {}
|
|
24
22
|
|
|
25
23
|
ActsAsApi::Config.accepted_api_formats.each do |item|
|
|
26
|
-
if render_options.
|
|
24
|
+
if render_options.key?(item)
|
|
27
25
|
api_format_options[item] = render_options[item]
|
|
28
26
|
render_options.delete item
|
|
29
27
|
end
|
|
30
28
|
end
|
|
31
29
|
|
|
32
|
-
meta_hash = render_options[:meta] if render_options.
|
|
30
|
+
meta_hash = render_options[:meta] if render_options.key?(:meta)
|
|
33
31
|
|
|
34
32
|
api_format = api_format_options.keys.first
|
|
35
33
|
api_model = api_format_options.values.first
|
|
@@ -64,17 +62,14 @@ module ActsAsApi
|
|
|
64
62
|
|
|
65
63
|
output_params[:root] = api_root_name
|
|
66
64
|
|
|
67
|
-
#output_params[:root] = output_params[:root].camelize if render_options.has_key?(:camelize) && render_options[:camelize]
|
|
68
|
-
#output_params[:root] = output_params[:root].dasherize if !render_options.has_key?(:dasherize) || render_options[:dasherize]
|
|
69
|
-
|
|
70
65
|
api_response = api_model.as_api_response(api_template)
|
|
71
66
|
|
|
72
67
|
if api_response.is_a?(Array) && api_format.to_sym == :json && ActsAsApi::Config.include_root_in_json_collections
|
|
73
|
-
api_response = api_response.collect{|f| { api_root_name.singularize => f } }
|
|
68
|
+
api_response = api_response.collect { |f| { api_root_name.singularize => f } }
|
|
74
69
|
end
|
|
75
70
|
|
|
76
|
-
if meta_hash
|
|
77
|
-
api_response = { api_root_name.to_sym =>
|
|
71
|
+
if meta_hash || ActsAsApi::Config.add_root_node_for.include?(api_format)
|
|
72
|
+
api_response = { api_root_name.to_sym => api_response }
|
|
78
73
|
end
|
|
79
74
|
|
|
80
75
|
api_response = meta_hash.merge api_response if meta_hash
|
|
@@ -89,7 +84,5 @@ module ActsAsApi
|
|
|
89
84
|
|
|
90
85
|
render output_params
|
|
91
86
|
end
|
|
92
|
-
|
|
93
87
|
end
|
|
94
|
-
|
|
95
88
|
end
|
|
@@ -18,22 +18,17 @@ module ActsAsApi
|
|
|
18
18
|
# end
|
|
19
19
|
#
|
|
20
20
|
# The `:api_template` parameter is required so the responder knows which api template it should render.
|
|
21
|
-
|
|
22
21
|
class Responder < ActionController::Responder
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
api_template = options[:api_template]
|
|
22
|
+
# Overrides the base implementation of display, replacing it with
|
|
23
|
+
# the render_for_api method whenever api_template is specified.
|
|
24
|
+
def display(resource, given_options = {})
|
|
25
|
+
api_template = options[:api_template]
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
end
|
|
27
|
+
if api_template.nil? || !resource.respond_to?(:as_api_response)
|
|
28
|
+
controller.render given_options.merge!(options).merge!(format => resource)
|
|
29
|
+
else
|
|
30
|
+
controller.render_for_api api_template, given_options.merge!(options).merge!(format => resource)
|
|
34
31
|
end
|
|
35
32
|
end
|
|
36
|
-
|
|
37
|
-
include Module
|
|
38
33
|
end
|
|
39
34
|
end
|
data/lib/acts_as_api/version.rb
CHANGED
data/spec/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Every ORM has a rake task. Run `rake -T` to see them all.
|
|
|
10
10
|
|
|
11
11
|
## Working with the specs
|
|
12
12
|
|
|
13
|
-
acts_as_api can be used with lots of different configurations, depending e.g. on the ORM (ActiveRecord, Mongoid, vanilla ruby) or the way the content is rendered (usual Rails controller
|
|
13
|
+
acts_as_api can be used with lots of different configurations, depending e.g. on the ORM (ActiveRecord, Mongoid, vanilla ruby) or the way the content is rendered (usual Rails controller vs. Responder).
|
|
14
14
|
|
|
15
15
|
A goal of the lib is to stay consistent in its behaviour over these different configurations, so it won't get in your way, once you change other parts of your application.
|
|
16
16
|
|
|
@@ -28,15 +28,25 @@ There used to be one Rails app that contained all supported ORMs. But multiple t
|
|
|
28
28
|
|
|
29
29
|
Now there are multiple Rails apps, one for every supported ORM: `./active_record_dummy` and `./mongoid_dummy`.
|
|
30
30
|
|
|
31
|
-
These are very simple apps,
|
|
31
|
+
These are very simple apps, they contain **no controllers** to be tested, just models that match the tested ones.
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
### Debugging in the dummy apps
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
Even though they are simple, you can still use the `rails console` of the dummy apps to debug acts_as_api.
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
$ cd spec/active_record_dummy
|
|
39
|
+
$ rails c
|
|
40
|
+
irb> User.last.as_api_response(:name_only)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
This can come in very handy sometimes.
|
|
44
|
+
|
|
45
|
+
### Adding a new dummy Rails app (in case you want to add another ORM)
|
|
36
46
|
|
|
37
47
|
* Create a new Rails app in the folder `./spec/#{orm_name}_dummy`.
|
|
38
48
|
|
|
39
|
-
* Create
|
|
49
|
+
* Create the models used in the spec (`User, Profile, Untouched, Task`).
|
|
40
50
|
|
|
41
51
|
* Include `UserTemplate` in your `User` model.
|
|
42
52
|
|
|
@@ -51,5 +61,4 @@ gem 'acts_as_api', :path => '../../'
|
|
|
51
61
|
|
|
52
62
|
* Add your dummy app to the `Rakefile` in the root folder by adding it to the `supported_orms` array.
|
|
53
63
|
|
|
54
|
-
|
|
55
64
|
If you have to do some special setup (e.g. creating a schema) you can do this in `./spec_helper.rb`.
|
|
@@ -2,15 +2,7 @@ source 'https://rubygems.org'
|
|
|
2
2
|
|
|
3
3
|
gem 'rails', '5.0.0.1'
|
|
4
4
|
|
|
5
|
-
# Bundle edge Rails instead:
|
|
6
|
-
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
|
7
|
-
|
|
8
5
|
gem 'sqlite3'
|
|
9
6
|
|
|
10
|
-
gem 'shared_engine', :
|
|
11
|
-
gem 'acts_as_api', :
|
|
12
|
-
|
|
13
|
-
group :test do
|
|
14
|
-
gem 'rspec-rails', '>= 2.5.0'
|
|
15
|
-
gem 'webrat'
|
|
16
|
-
end
|
|
7
|
+
gem 'shared_engine', path: '../shared_engine'
|
|
8
|
+
gem 'acts_as_api', path: '../../'
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
class User < ActiveRecord::Base
|
|
2
|
-
validates :first_name, :last_name, :
|
|
2
|
+
validates :first_name, :last_name, presence: true
|
|
3
3
|
|
|
4
4
|
has_many :tasks
|
|
5
5
|
|
|
@@ -17,11 +17,11 @@ class User < ActiveRecord::Base
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
api_accessible :rename_last_name do |t|
|
|
20
|
-
t.add :last_name, :
|
|
20
|
+
t.add :last_name, as: :family_name
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
api_accessible :rename_full_name do |t|
|
|
24
|
-
t.add :full_name, :
|
|
24
|
+
t.add :full_name, as: :other_full_name
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
api_accessible :with_former_value do |t|
|
|
@@ -29,19 +29,19 @@ class User < ActiveRecord::Base
|
|
|
29
29
|
t.add :last_name
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
api_accessible :age_and_first_name, :
|
|
32
|
+
api_accessible :age_and_first_name, extend: :with_former_value do |t|
|
|
33
33
|
t.add :age
|
|
34
34
|
t.remove :last_name
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
api_accessible :calling_a_proc do |t|
|
|
38
|
-
t.add
|
|
39
|
-
t.add
|
|
38
|
+
t.add proc { |model| model.full_name.upcase }, as: :all_caps_name
|
|
39
|
+
t.add proc { |_| Time.now.class.to_s }, as: :without_param
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
api_accessible :calling_a_lambda do |t|
|
|
43
|
-
t.add
|
|
44
|
-
t.add
|
|
43
|
+
t.add ->(model) { model.full_name.upcase }, as: :all_caps_name
|
|
44
|
+
t.add ->(_) { Time.now.class.to_s }, as: :without_param
|
|
45
45
|
end
|
|
46
46
|
api_accessible :include_tasks do |t|
|
|
47
47
|
t.add :tasks
|
|
@@ -53,19 +53,19 @@ class User < ActiveRecord::Base
|
|
|
53
53
|
|
|
54
54
|
api_accessible :other_sub_template do |t|
|
|
55
55
|
t.add :first_name
|
|
56
|
-
t.add :tasks, :
|
|
56
|
+
t.add :tasks, template: :other_template
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
api_accessible :include_completed_tasks do |t|
|
|
60
|
-
t.add
|
|
60
|
+
t.add 'tasks.completed.all', as: :completed_tasks
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
api_accessible :sub_node do |t|
|
|
64
|
-
t.add Hash[:
|
|
64
|
+
t.add Hash[foo: :say_something], as: :sub_nodes
|
|
65
65
|
end
|
|
66
66
|
|
|
67
67
|
api_accessible :nested_sub_node do |t|
|
|
68
|
-
t.add Hash[:foo, Hash[:bar, :last_name]], :
|
|
68
|
+
t.add Hash[:foo, Hash[:bar, :last_name]], as: :sub_nodes
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
api_accessible :nested_sub_hash do |t|
|
|
@@ -74,46 +74,46 @@ class User < ActiveRecord::Base
|
|
|
74
74
|
|
|
75
75
|
api_accessible :if_over_thirty do |t|
|
|
76
76
|
t.add :first_name
|
|
77
|
-
t.add :last_name, :
|
|
77
|
+
t.add :last_name, if: :over_thirty?
|
|
78
78
|
end
|
|
79
79
|
|
|
80
80
|
api_accessible :if_returns_nil do |t|
|
|
81
81
|
t.add :first_name
|
|
82
|
-
t.add :last_name, :
|
|
82
|
+
t.add :last_name, if: :return_nil
|
|
83
83
|
end
|
|
84
84
|
|
|
85
85
|
api_accessible :if_over_thirty_proc do |t|
|
|
86
86
|
t.add :first_name
|
|
87
|
-
t.add :last_name, :
|
|
87
|
+
t.add :last_name, if: ->(model) { model.over_thirty? }
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
api_accessible :if_returns_nil_proc do |t|
|
|
91
91
|
t.add :first_name
|
|
92
|
-
t.add :last_name, :
|
|
92
|
+
t.add :last_name, if: ->(_) { nil }
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
api_accessible :unless_under_thirty do |t|
|
|
96
96
|
t.add :first_name
|
|
97
|
-
t.add :last_name, :
|
|
97
|
+
t.add :last_name, unless: :under_thirty?
|
|
98
98
|
end
|
|
99
99
|
|
|
100
100
|
api_accessible :unless_returns_nil do |t|
|
|
101
101
|
t.add :first_name
|
|
102
|
-
t.add :last_name, :
|
|
102
|
+
t.add :last_name, unless: :return_nil
|
|
103
103
|
end
|
|
104
104
|
|
|
105
105
|
api_accessible :unless_under_thirty_proc do |t|
|
|
106
106
|
t.add :first_name
|
|
107
|
-
t.add :last_name, :
|
|
107
|
+
t.add :last_name, unless: ->(model) { model.under_thirty? }
|
|
108
108
|
end
|
|
109
109
|
|
|
110
110
|
api_accessible :unless_returns_nil_proc do |t|
|
|
111
111
|
t.add :first_name
|
|
112
|
-
t.add :last_name, :
|
|
112
|
+
t.add :last_name, unless: ->(_) { nil }
|
|
113
113
|
end
|
|
114
114
|
|
|
115
115
|
api_accessible :with_prefix_name_only do |t|
|
|
116
|
-
t.add
|
|
116
|
+
t.add ->(_) { 'true' }, as: :prefix
|
|
117
117
|
t.add :first_name
|
|
118
118
|
t.add :last_name
|
|
119
119
|
end
|
|
@@ -121,17 +121,17 @@ class User < ActiveRecord::Base
|
|
|
121
121
|
api_accessible :name_only_with_postfix do |t|
|
|
122
122
|
t.add :first_name
|
|
123
123
|
t.add :last_name
|
|
124
|
-
t.add
|
|
124
|
+
t.add ->(_) { 'true' }, as: :postfix
|
|
125
125
|
end
|
|
126
126
|
|
|
127
127
|
api_accessible :with_prefix_name_only_with_postfix do |t|
|
|
128
|
-
t.add
|
|
128
|
+
t.add ->(_) { 'true' }, as: :prefix
|
|
129
129
|
t.add :first_name
|
|
130
130
|
t.add :last_name
|
|
131
|
-
t.add
|
|
131
|
+
t.add ->(_) { 'true' }, as: :postfix
|
|
132
132
|
end
|
|
133
133
|
|
|
134
|
-
def before_api_response(
|
|
134
|
+
def before_api_response(_api_response)
|
|
135
135
|
@before_api_response_called = true
|
|
136
136
|
end
|
|
137
137
|
|
|
@@ -139,7 +139,7 @@ class User < ActiveRecord::Base
|
|
|
139
139
|
!!@before_api_response_called
|
|
140
140
|
end
|
|
141
141
|
|
|
142
|
-
def after_api_response(
|
|
142
|
+
def after_api_response(_api_response)
|
|
143
143
|
@after_api_response_called = true
|
|
144
144
|
end
|
|
145
145
|
|
|
@@ -151,8 +151,8 @@ class User < ActiveRecord::Base
|
|
|
151
151
|
@skip_api_response = should_skip
|
|
152
152
|
end
|
|
153
153
|
|
|
154
|
-
def around_api_response(
|
|
155
|
-
@skip_api_response ? { :
|
|
154
|
+
def around_api_response(_api_response)
|
|
155
|
+
@skip_api_response ? { skipped: true } : yield
|
|
156
156
|
end
|
|
157
157
|
|
|
158
158
|
def over_thirty?
|
|
@@ -172,14 +172,13 @@ class User < ActiveRecord::Base
|
|
|
172
172
|
end
|
|
173
173
|
|
|
174
174
|
def say_something
|
|
175
|
-
|
|
175
|
+
'something'
|
|
176
176
|
end
|
|
177
177
|
|
|
178
178
|
def sub_hash
|
|
179
179
|
{
|
|
180
|
-
:
|
|
181
|
-
:
|
|
180
|
+
foo: 'bar',
|
|
181
|
+
hello: 'world'
|
|
182
182
|
}
|
|
183
183
|
end
|
|
184
|
-
|
|
185
184
|
end
|