rails-api 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4ea50fca1f486fc7d1d232b8295bb67c341a88c
4
- data.tar.gz: d2c349cce24922db210c93ff6b3970e221ecf3b2
3
+ metadata.gz: 77d3d89e4ce4fa7f0c861dc9cedb98d69a5b43f6
4
+ data.tar.gz: ea09b246ac549fce29ba5968fbf2bca3a82b5327
5
5
  SHA512:
6
- metadata.gz: eaab8457e8dce7dec7edf508e26f4f3bc87e17f8b0fc8183e05d96ffd9e597687e4a4d765e9ed215aea7570f6ee8e23190558e48fb05e578c536f6b891f96fbf
7
- data.tar.gz: e80d72538c688ed519c5aa35d6deb713df62580ce1621f9071e50dc406758263954cc9f255b705c78899b7d54bf885a90d480899b000898d857ac9e0de6c086c
6
+ metadata.gz: e1247598f6072111eba2674bdc23df065dca70b3bf1031c7e7ea640bf568c64464df55c1cd57652369d4e8cf73bf28b0103fb7dd11a611889ad63c03af1f7063
7
+ data.tar.gz: 3973dee3bcd9b1f1456af5fac6c1eacb328789c9deab2a14b96a45b3e7289db17f1a081f2732a0458066b10d53748d5ca35c21a366ab8c3c810d7be04165fdd2
data/README.md CHANGED
@@ -133,29 +133,33 @@ end
133
133
 
134
134
  And comment out the `protect_from_forgery` call if you are using it. (You aren't using cookie-based authentication for your API, are you?)
135
135
 
136
- If you want to use the Rails default middleware stack (avoid the reduction that rails-api does), you can just add config.api_only = false to config/application.rb file.
136
+ If you want to use the Rails default middleware stack (avoid the reduction that rails-api does), you can just add `config.api_only = false` to `config/application.rb` file.
137
137
 
138
138
  ### Serialization
139
139
 
140
140
  We suggest using [ActiveModel::Serializers][ams] to serialize your ActiveModel/ActiveRecord objects into the desired response format (e.g. JSON).
141
+ In `ApplicationController` you need to add `include ActionController::Serialization` to make ActiveModelSerializers work.
142
+
143
+
141
144
 
142
145
  ### Choosing Middlewares
143
146
 
144
147
  An API application comes with the following middlewares by default.
145
148
 
146
149
  * *ActionDispatch::DebugExceptions*: Log exceptions.
147
- * *ActionDispatch::Head*: Dispatch *HEAD* requests as *GET* requests, and return only the status code and headers.
148
150
  * *ActionDispatch::ParamsParser*: Parse XML, YAML and JSON parameters when the request's *Content-Type* is one of those.
149
151
  * *ActionDispatch::Reloader*: In development mode, support code reloading.
150
152
  * *ActionDispatch::RemoteIp*: Protect against IP spoofing attacks.
151
153
  * *ActionDispatch::RequestId*: Makes a unique request id available, sending the id to the client via the X-Request-Id header. The unique request id can be used to trace a request end-to-end and would typically end up being part of log files from multiple pieces of the stack.
152
154
  * *ActionDispatch::ShowExceptions*: Rescue exceptions and re-dispatch them to an exception handling application.
153
155
  * *Rack::Cache*: Caches responses with public *Cache-Control* headers using HTTP caching semantics.
156
+ * *Rack::Head*: Dispatch *HEAD* requests as *GET* requests, and return only the status code and headers.
154
157
  * *Rack::ConditionalGet*: Supports the `stale?` feature in Rails controllers.
155
158
  * *Rack::ETag*: Automatically set an *ETag* on all string responses. This means that if the same response is returned from a controller for the same URL, the server will return a *304 Not Modified*, even if no additional caching steps are taken. This is primarily a client-side optimization; it reduces bandwidth costs but not server processing time.
156
159
  * *Rack::Lock*: If your application is not marked as threadsafe (`config.threadsafe!`), this middleware will add a mutex around your requests.
157
160
  * *Rack::Runtime*: Adds a header to the response listing the total runtime of the request.
158
161
  * *Rack::Sendfile*: Uses a front-end server's file serving support from your Rails application.
162
+ * *Rack::Head*: Dispatch *HEAD* requests as *GET* requests, and return only the status code and headers.
159
163
  * *Rails::Rack::Logger*: Log the request started and flush all loggers after it.
160
164
 
161
165
  Other plugins, including *ActiveRecord*, may add additional middlewares. In general, these middlewares are agnostic to the type of app you are building, and make sense in an API-only Rails application.
@@ -5,8 +5,8 @@ require 'rails-api/application'
5
5
 
6
6
  module Rails
7
7
  module API
8
- def self.rails4?
9
- Rails::VERSION::MAJOR == 4
8
+ def self.rails3?
9
+ Rails::VERSION::MAJOR == 3
10
10
  end
11
11
  end
12
12
  end
@@ -123,7 +123,6 @@ module ActionController
123
123
  end
124
124
 
125
125
  MODULES = [
126
- HideActions,
127
126
  UrlFor,
128
127
  Redirecting,
129
128
  Rendering,
@@ -146,17 +145,25 @@ module ActionController
146
145
  Instrumentation
147
146
  ]
148
147
 
149
- if Rails::VERSION::MAJOR >= 4 && Rails::VERSION::MINOR > 0
148
+ if Rails::VERSION::MAJOR == 5 || (Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR > 0)
150
149
  include AbstractController::Rendering
151
150
  include ActionView::Rendering
152
151
  end
153
152
 
153
+ if Rails::VERSION::MAJOR < 5
154
+ include ActionController::HideActions
155
+ end
156
+
154
157
  MODULES.each do |mod|
155
158
  include mod
156
159
  end
157
160
 
158
161
  DEFAULT_PROTECTED_INSTANCE_VARIABLES = begin
159
- (Rails::VERSION::MAJOR < 4 || Rails::VERSION::MINOR < 1) ? Array : Set
162
+ if Rails::VERSION::MAJOR == 5 || (Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR > 0)
163
+ Set
164
+ else
165
+ Array
166
+ end
160
167
  end.new
161
168
 
162
169
  def self.protected_instance_variables
@@ -7,10 +7,10 @@ module Rails
7
7
  alias_method :rails_default_middleware_stack, :default_middleware_stack
8
8
 
9
9
  def default_middleware_stack
10
- if Rails::API.rails4?
11
- DefaultRailsFourMiddlewareStack.new(self, config, paths).build_stack
10
+ if Rails::API.rails3?
11
+ rails3_stack
12
12
  else
13
- rails_3_stack
13
+ DefaultRailsFourMiddlewareStack.new(self, config, paths).build_stack
14
14
  end
15
15
  end
16
16
 
@@ -37,8 +37,16 @@ module Rails
37
37
  config.api_only = true
38
38
  setup_generators!
39
39
  end
40
+
41
+ def check_serve_static_files
42
+ if Rails::VERSION::MAJOR >= 5 || (Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR > 1)
43
+ config.serve_static_files
44
+ else
45
+ config.serve_static_assets
46
+ end
47
+ end
40
48
 
41
- def rails_3_stack
49
+ def rails3_stack
42
50
  ActionDispatch::MiddlewareStack.new.tap do |middleware|
43
51
  if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache
44
52
  require "action_dispatch/http/rack_cache"
@@ -53,11 +61,12 @@ module Rails
53
61
  if config.action_dispatch.x_sendfile_header.present?
54
62
  middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
55
63
  end
64
+
56
65
 
57
- if config.serve_static_assets
66
+ if check_serve_static_files
58
67
  middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
59
68
  end
60
-
69
+
61
70
  middleware.use ::Rack::Lock unless config.allow_concurrency
62
71
  middleware.use ::Rack::Runtime
63
72
  middleware.use ::Rack::MethodOverride unless config.api_only
@@ -24,7 +24,7 @@ module Rails
24
24
  middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
25
25
  end
26
26
 
27
- if config.serve_static_assets
27
+ if serve_static_files?
28
28
  middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
29
29
  end
30
30
 
@@ -63,6 +63,17 @@ module Rails
63
63
 
64
64
  private
65
65
 
66
+ # `config.serve_static_assets` will be removed in Rails 5.0, and throws
67
+ # a deprecation warning in Rails >= 4.2. The new option is
68
+ # `config.serve_static_files`.
69
+ def serve_static_files?
70
+ if config.respond_to?(:serve_static_files)
71
+ config.serve_static_files
72
+ else
73
+ config.serve_static_assets
74
+ end
75
+ end
76
+
66
77
  def reload_dependencies?
67
78
  config.reload_classes_only_on_change != true || app.reloaders.map(&:updated?).any?
68
79
  end
@@ -27,7 +27,7 @@ gem '<%= database_gemfile_entry.name -%>'
27
27
  <%= "gem 'json'\n" if RUBY_VERSION < "1.9.2" -%>
28
28
 
29
29
  # To use ActiveModel has_secure_password
30
- # gem 'bcrypt-ruby', '~> 3.1.2'
30
+ # gem 'bcrypt', '~> 3.1.7'
31
31
 
32
32
  # To use Jbuilder templates for JSON
33
33
  # gem 'jbuilder'
@@ -0,0 +1,4 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Configure sensitive parameters which will be filtered from the log file.
4
+ Rails.application.config.filter_parameters += [:password]
@@ -1,5 +1,7 @@
1
1
  <% module_namespacing do -%>
2
2
  class <%= controller_class_name %>Controller < ApplicationController
3
+ before_action <%= ":set_#{singular_table_name}" %>, only: [:show, :update, :destroy]
4
+
3
5
  # GET <%= route_url %>
4
6
  # GET <%= route_url %>.json
5
7
  def index
@@ -11,8 +13,6 @@ class <%= controller_class_name %>Controller < ApplicationController
11
13
  # GET <%= route_url %>/1
12
14
  # GET <%= route_url %>/1.json
13
15
  def show
14
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
15
-
16
16
  render json: <%= "@#{singular_table_name}" %>
17
17
  end
18
18
 
@@ -33,7 +33,7 @@ class <%= controller_class_name %>Controller < ApplicationController
33
33
  def update
34
34
  @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
35
35
 
36
- if @<%= Rails::API.rails4? ? orm_instance.update("#{singular_table_name}_params") : orm_instance.update_attributes("params[:#{singular_table_name}]") %>
36
+ if @<%= Rails::API.rails3? ? orm_instance.update_attributes("params[:#{singular_table_name}]") : orm_instance.update("#{singular_table_name}_params") %>
37
37
  head :no_content
38
38
  else
39
39
  render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
@@ -43,14 +43,17 @@ class <%= controller_class_name %>Controller < ApplicationController
43
43
  # DELETE <%= route_url %>/1
44
44
  # DELETE <%= route_url %>/1.json
45
45
  def destroy
46
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
47
46
  @<%= orm_instance.destroy %>
48
47
 
49
48
  head :no_content
50
49
  end
51
50
 
52
51
  private
53
-
52
+
53
+ def <%= "set_#{singular_table_name}" %>
54
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
55
+ end
56
+
54
57
  def <%= "#{singular_table_name}_params" %>
55
58
  <%- if attributes_names.empty? -%>
56
59
  params[:<%= singular_table_name %>]
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module API
3
- VERSION = '0.3.1'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -29,7 +29,7 @@ class ApiApplicationTest < ActiveSupport::TestCase
29
29
 
30
30
  def test_api_middleware_stack
31
31
  expected_middleware_stack =
32
- rails4? ? expected_middleware_stack_rails4 : expected_middleware_stack_rails3
32
+ rails3? ? expected_middleware_stack_rails3 : expected_middleware_stack_rails
33
33
 
34
34
  assert_equal expected_middleware_stack, app.middleware.map(&:klass).map(&:name)
35
35
  end
@@ -56,7 +56,7 @@ class ApiApplicationTest < ActiveSupport::TestCase
56
56
  ]
57
57
  end
58
58
 
59
- def expected_middleware_stack_rails4
59
+ def expected_middleware_stack_rails
60
60
  [
61
61
  "ActionDispatch::Static",
62
62
  "Rack::Lock",
@@ -3,15 +3,24 @@ require 'test_helper'
3
3
  class ActionMethodsApiController < ActionController::API
4
4
  def one; end
5
5
  def two; end
6
- hide_action :two
6
+ # Rails 5 does not have method hide_action
7
+ if Rails::VERSION::MAJOR < 5
8
+ hide_action :two
9
+ end
7
10
  end
8
11
 
9
12
  class ActionMethodsApiTest < ActionController::TestCase
10
13
  tests ActionMethodsApiController
11
14
 
12
15
  def test_action_methods
13
- assert_equal Set.new(%w(one)),
14
- @controller.class.action_methods,
15
- "#{@controller.controller_path} should not be empty!"
16
+ if Rails::VERSION::MAJOR < 5
17
+ assert_equal Set.new(%w(one)),
18
+ @controller.class.action_methods,
19
+ "#{@controller.controller_path} should not be empty!"
20
+ else
21
+ assert_equal Set.new(%w(one two)),
22
+ @controller.class.action_methods,
23
+ "#{@controller.controller_path} should not be empty!"
24
+ end
16
25
  end
17
26
  end
@@ -49,7 +49,7 @@ class AppGeneratorTest < Rails::Generators::TestCase
49
49
  test/integration
50
50
  test/#{generated_test_unit_dir}
51
51
  )
52
- files.concat rails4? ? default_files_rails4 : default_files_rails3
52
+ files.concat rails3? ? default_files_rails3 : default_files_rails
53
53
  files
54
54
  end
55
55
 
@@ -57,7 +57,7 @@ class AppGeneratorTest < Rails::Generators::TestCase
57
57
  %w(script/rails)
58
58
  end
59
59
 
60
- def default_files_rails4
60
+ def default_files_rails
61
61
  %w(bin/bundle bin/rails bin/rake)
62
62
  end
63
63
 
@@ -25,11 +25,11 @@ module GeneratorsTestHelper
25
25
  end
26
26
 
27
27
  def generated_test_unit_dir
28
- rails4? ? 'models' : 'unit'
28
+ rails3? ? 'unit' : 'models'
29
29
  end
30
30
 
31
31
  def generated_test_functional_dir
32
- rails4? ? 'controllers' : 'functional'
32
+ rails3? ? 'functional' : 'controllers'
33
33
  end
34
34
 
35
35
  def remove_destination
@@ -15,16 +15,16 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
15
15
  assert_file "test/#{generated_test_unit_dir}/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/
16
16
  assert_file "test/fixtures/product_lines.yml"
17
17
 
18
- if rails4?
19
- assert_migration "db/migrate/create_product_lines.rb",
20
- /belongs_to :product, index: true/,
21
- /references :user, index: true/
22
- else
18
+ if rails3?
23
19
  assert_migration "db/migrate/create_product_lines.rb",
24
20
  /belongs_to :product/,
25
21
  /add_index :product_lines, :product_id/,
26
22
  /references :user/,
27
23
  /add_index :product_lines, :user_id/
24
+ else
25
+ assert_migration "db/migrate/create_product_lines.rb",
26
+ /belongs_to :product, index: true/,
27
+ /references :user, index: true/
28
28
  end
29
29
 
30
30
  # Route
@@ -38,12 +38,15 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
38
38
  assert_match(/class ProductLinesController < ApplicationController/, content)
39
39
  assert_no_match(/respond_to/, content)
40
40
 
41
+ assert_match(/before_action :set_product_line, only: \[:show, :update, :destroy\]/, content)
42
+
41
43
  assert_instance_method :index, content do |m|
42
44
  assert_match(/@product_lines = ProductLine\.all/, m)
45
+ assert_match(/render json: @product_lines/, m)
43
46
  end
44
47
 
45
48
  assert_instance_method :show, content do |m|
46
- assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m)
49
+ assert_match(/render json: @product_line/, m)
47
50
  end
48
51
 
49
52
  assert_instance_method :create, content do |m|
@@ -54,19 +57,22 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
54
57
 
55
58
  assert_instance_method :update, content do |m|
56
59
  assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m)
57
- if rails4?
58
- assert_match(/@product_line\.update\(product_line_params\)/, m)
59
- else
60
+ if rails3?
60
61
  assert_match(/@product_line\.update_attributes\(product_line_params\)/, m)
62
+ else
63
+ assert_match(/@product_line\.update\(product_line_params\)/, m)
61
64
  end
62
65
  assert_match(/@product_line\.errors/, m)
63
66
  end
64
67
 
65
68
  assert_instance_method :destroy, content do |m|
66
- assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m)
67
69
  assert_match(/@product_line\.destroy/, m)
68
70
  end
69
71
 
72
+ assert_instance_method :set_product_line, content do |m|
73
+ assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m)
74
+ end
75
+
70
76
  assert_instance_method :product_line_params, content do |m|
71
77
  assert_match(/params\.require\(:product_line\)\.permit\(:title, :product_id, :user_id\)/, m)
72
78
  end
@@ -74,12 +80,12 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
74
80
 
75
81
  assert_file "test/#{generated_test_functional_dir}/product_lines_controller_test.rb" do |test|
76
82
  assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, test)
77
- if rails4?
78
- assert_match(/post :create, product_line: \{ product_id: @product_line.product_id, title: @product_line.title, user_id: @product_line.user_id \}/, test)
79
- assert_match(/put :update, id: @product_line, product_line: \{ product_id: @product_line.product_id, title: @product_line.title, user_id: @product_line.user_id \}/, test)
80
- else
83
+ if rails3?
81
84
  assert_match(/post :create, product_line: \{ title: @product_line.title \}/, test)
82
85
  assert_match(/put :update, id: @product_line, product_line: \{ title: @product_line.title \}/, test)
86
+ else
87
+ assert_match(/post :create, product_line: \{ product_id: @product_line.product_id, title: @product_line.title, user_id: @product_line.user_id \}/, test)
88
+ assert_match(/put :update, id: @product_line, product_line: \{ product_id: @product_line.product_id, title: @product_line.title, user_id: @product_line.user_id \}/, test)
83
89
  end
84
90
  assert_no_match(/assert_redirected_to/, test)
85
91
  end
@@ -6,8 +6,8 @@ require 'rails'
6
6
  require 'rails/test_help'
7
7
  require 'rails-api'
8
8
 
9
- def rails4?
10
- Rails::API.rails4?
9
+ def rails3?
10
+ Rails::API.rails3?
11
11
  end
12
12
 
13
13
  class ActiveSupport::TestCase
@@ -25,7 +25,7 @@ class ActiveSupport::TestCase
25
25
  c.performance_tool :test_unit
26
26
  end
27
27
 
28
- if rails4?
28
+ unless rails3?
29
29
  config.eager_load = false
30
30
  config.secret_key_base = 'abc123'
31
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Santiago Pastorino and Carlos Antonio da Silva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-23 00:00:00.000000000 Z
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -76,6 +76,7 @@ files:
76
76
  - lib/rails-api/public_exceptions.rb
77
77
  - lib/rails-api/templates/rails/app/Gemfile
78
78
  - lib/rails-api/templates/rails/app/app/controllers/application_controller.rb.tt
79
+ - lib/rails-api/templates/rails/app/config/initializers/filter_parameter_logging.rb.tt
79
80
  - lib/rails-api/templates/rails/app/config/initializers/secret_token.rb.tt
80
81
  - lib/rails-api/templates/rails/app/config/initializers/wrap_parameters.rb.tt
81
82
  - lib/rails-api/templates/rails/scaffold_controller/controller.rb
@@ -122,15 +123,15 @@ summary: Rails for API only Applications
122
123
  test_files:
123
124
  - test/test_helper.rb
124
125
  - test/api_application/api_application_test.rb
125
- - test/generators/fixtures/routes.rb
126
126
  - test/generators/resource_generator_test.rb
127
127
  - test/generators/generators_test_helper.rb
128
- - test/generators/app_generator_test.rb
129
128
  - test/generators/scaffold_generator_test.rb
130
- - test/api_controller/conditional_get_test.rb
129
+ - test/generators/fixtures/routes.rb
130
+ - test/generators/app_generator_test.rb
131
131
  - test/api_controller/url_for_test.rb
132
- - test/api_controller/renderers_test.rb
133
132
  - test/api_controller/force_ssl_test.rb
134
- - test/api_controller/redirect_to_test.rb
135
133
  - test/api_controller/data_streaming_test.rb
134
+ - test/api_controller/redirect_to_test.rb
135
+ - test/api_controller/conditional_get_test.rb
136
+ - test/api_controller/renderers_test.rb
136
137
  - test/api_controller/action_methods_test.rb