mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge branch 'master' of github.com:rails/rails
This commit is contained in:
commit
6d30219c27
10 changed files with 46 additions and 92 deletions
|
@ -10,26 +10,14 @@ module ActionController
|
||||||
# params = ActionController::Parameters.new(a: {})
|
# params = ActionController::Parameters.new(a: {})
|
||||||
# params.fetch(:b)
|
# params.fetch(:b)
|
||||||
# # => ActionController::ParameterMissing: param not found: b
|
# # => ActionController::ParameterMissing: param not found: b
|
||||||
|
# params.require(:a)
|
||||||
|
# # => ActionController::ParameterMissing: param not found: a
|
||||||
class ParameterMissing < KeyError
|
class ParameterMissing < KeyError
|
||||||
attr_reader :param # :nodoc:
|
attr_reader :param # :nodoc:
|
||||||
|
|
||||||
def initialize(param) # :nodoc:
|
def initialize(param) # :nodoc:
|
||||||
@param = param
|
@param = param
|
||||||
super("param not found: #{param}")
|
super("param is missing or the value is empty: #{param}")
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Raised when a required parameter value is empty.
|
|
||||||
#
|
|
||||||
# params = ActionController::Parameters.new(a: {})
|
|
||||||
# params.require(:a)
|
|
||||||
# # => ActionController::EmptyParameter: value is empty for required key: a
|
|
||||||
class EmptyParameter < IndexError
|
|
||||||
attr_reader :param
|
|
||||||
|
|
||||||
def initialize(param)
|
|
||||||
@param = param
|
|
||||||
super("value is empty for required key: #{param}")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -169,22 +157,20 @@ module ActionController
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ensures that a parameter is present. If it's present and not empty,
|
# Ensures that a parameter is present. If it's present, returns
|
||||||
# returns the parameter at the given +key+, if it's empty raises
|
# the parameter at the given +key+, otherwise raises an
|
||||||
# an <tt>ActionController::EmptyParameter</tt> error, otherwise
|
# <tt>ActionController::ParameterMissing</tt> error.
|
||||||
# raises an <tt>ActionController::ParameterMissing</tt> error.
|
|
||||||
#
|
#
|
||||||
# ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person)
|
# ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person)
|
||||||
# # => {"name"=>"Francesco"}
|
# # => {"name"=>"Francesco"}
|
||||||
#
|
#
|
||||||
# ActionController::Parameters.new(person: {}).require(:person)
|
# ActionController::Parameters.new(person: nil).require(:person)
|
||||||
# # => ActionController::EmptyParameter: value is empty for required key: person
|
# # => ActionController::ParameterMissing: param not found: person
|
||||||
#
|
#
|
||||||
# ActionController::Parameters.new(name: {}).require(:person)
|
# ActionController::Parameters.new(person: {}).require(:person)
|
||||||
# # => ActionController::ParameterMissing: param not found: person
|
# # => ActionController::ParameterMissing: param not found: person
|
||||||
def require(key)
|
def require(key)
|
||||||
raise(ActionController::ParameterMissing.new(key)) unless self.key?(key)
|
self[key].presence || raise(ParameterMissing.new(key))
|
||||||
self[key].presence || raise(ActionController::EmptyParameter.new(key))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Alias of #require.
|
# Alias of #require.
|
||||||
|
|
|
@ -15,8 +15,7 @@ module ActionDispatch
|
||||||
'ActionController::InvalidAuthenticityToken' => :unprocessable_entity,
|
'ActionController::InvalidAuthenticityToken' => :unprocessable_entity,
|
||||||
'ActionDispatch::ParamsParser::ParseError' => :bad_request,
|
'ActionDispatch::ParamsParser::ParseError' => :bad_request,
|
||||||
'ActionController::BadRequest' => :bad_request,
|
'ActionController::BadRequest' => :bad_request,
|
||||||
'ActionController::ParameterMissing' => :bad_request,
|
'ActionController::ParameterMissing' => :bad_request
|
||||||
'ActionController::EmptyParameter' => :bad_request
|
|
||||||
)
|
)
|
||||||
|
|
||||||
cattr_accessor :rescue_templates
|
cattr_accessor :rescue_templates
|
||||||
|
|
|
@ -2,14 +2,8 @@ require 'abstract_unit'
|
||||||
require 'action_controller/metal/strong_parameters'
|
require 'action_controller/metal/strong_parameters'
|
||||||
|
|
||||||
class ParametersRequireTest < ActiveSupport::TestCase
|
class ParametersRequireTest < ActiveSupport::TestCase
|
||||||
test "required parameters must be present" do
|
test "required parameters must be present not merely not nil" do
|
||||||
assert_raises(ActionController::ParameterMissing) do
|
assert_raises(ActionController::ParameterMissing) do
|
||||||
ActionController::Parameters.new(name: {}).require(:person)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "required parameters can't be blank" do
|
|
||||||
assert_raises(ActionController::EmptyParameter) do
|
|
||||||
ActionController::Parameters.new(person: {}).require(:person)
|
ActionController::Parameters.new(person: {}).require(:person)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,11 +5,6 @@ class BooksController < ActionController::Base
|
||||||
params.require(:book).require(:name)
|
params.require(:book).require(:name)
|
||||||
head :ok
|
head :ok
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
|
||||||
params.require(:book)
|
|
||||||
head :ok
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class ActionControllerRequiredParamsTest < ActionController::TestCase
|
class ActionControllerRequiredParamsTest < ActionController::TestCase
|
||||||
|
@ -25,20 +20,6 @@ class ActionControllerRequiredParamsTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "empty required parameters will raise an exception" do
|
|
||||||
assert_raise ActionController::EmptyParameter do
|
|
||||||
put :update, {book: {}}
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_raise ActionController::EmptyParameter do
|
|
||||||
put :update, {book: ''}
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_raise ActionController::EmptyParameter do
|
|
||||||
put :update, {book: nil}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "required parameters that are present will not raise" do
|
test "required parameters that are present will not raise" do
|
||||||
post :create, { book: { name: "Mjallo!" } }
|
post :create, { book: { name: "Mjallo!" } }
|
||||||
assert_response :ok
|
assert_response :ok
|
||||||
|
|
|
@ -43,8 +43,6 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
|
||||||
raise ActionController::UrlGenerationError, "No route matches"
|
raise ActionController::UrlGenerationError, "No route matches"
|
||||||
when "/parameter_missing"
|
when "/parameter_missing"
|
||||||
raise ActionController::ParameterMissing, :missing_param_key
|
raise ActionController::ParameterMissing, :missing_param_key
|
||||||
when "/required_key_empty_value"
|
|
||||||
raise ActionController::EmptyParameter, :empty_param_key
|
|
||||||
else
|
else
|
||||||
raise "puke!"
|
raise "puke!"
|
||||||
end
|
end
|
||||||
|
@ -128,10 +126,6 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
|
||||||
get "/parameter_missing", {}, {'action_dispatch.show_exceptions' => true}
|
get "/parameter_missing", {}, {'action_dispatch.show_exceptions' => true}
|
||||||
assert_response 400
|
assert_response 400
|
||||||
assert_match(/ActionController::ParameterMissing/, body)
|
assert_match(/ActionController::ParameterMissing/, body)
|
||||||
|
|
||||||
get "/required_key_empty_value", {}, {'action_dispatch.show_exceptions' => true}
|
|
||||||
assert_response 400
|
|
||||||
assert_match(/ActionController::EmptyParameter/, body)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "rescue with text error for xhr request" do
|
test "rescue with text error for xhr request" do
|
||||||
|
|
|
@ -1,27 +1,27 @@
|
||||||
* Added ActiveRecord::Base#enum for declaring enum attributes where the values map to integers in the database, but can be queried by name.
|
* Added ActiveRecord::Base#enum for declaring enum attributes where the values map to integers in the database, but can be queried by name.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
class Conversation < ActiveRecord::Base
|
|
||||||
enum status: %i( active archived )
|
|
||||||
end
|
|
||||||
|
|
||||||
Conversation::STATUS # => { active: 0, archived: 1 }
|
class Conversation < ActiveRecord::Base
|
||||||
|
enum status: [:active, :archived]
|
||||||
|
end
|
||||||
|
|
||||||
# conversation.update! status: 0
|
Conversation::STATUS # => { active: 0, archived: 1 }
|
||||||
conversation.active!
|
|
||||||
conversation.active? # => true
|
|
||||||
conversation.status # => :active
|
|
||||||
|
|
||||||
# conversation.update! status: 1
|
# conversation.update! status: 0
|
||||||
conversation.archived!
|
conversation.active!
|
||||||
conversation.archived? # => true
|
conversation.active? # => true
|
||||||
conversation.status # => :archived
|
conversation.status # => :active
|
||||||
|
|
||||||
# conversation.update! status: 1
|
# conversation.update! status: 1
|
||||||
conversation.status = :archived
|
conversation.archived!
|
||||||
|
conversation.archived? # => true
|
||||||
|
conversation.status # => :archived
|
||||||
|
|
||||||
*DHH*
|
# conversation.update! status: 1
|
||||||
|
conversation.status = :archived
|
||||||
|
|
||||||
|
*DHH*
|
||||||
|
|
||||||
* ActiveRecord::Base#attribute_for_inspect now truncates long arrays (more than 10 elements)
|
* ActiveRecord::Base#attribute_for_inspect now truncates long arrays (more than 10 elements)
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@
|
||||||
Fixed bug when providing `includes` in through association scope, and fetching targets.
|
Fixed bug when providing `includes` in through association scope, and fetching targets.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
class Vendor < ActiveRecord::Base
|
class Vendor < ActiveRecord::Base
|
||||||
has_many :relationships, -> { includes(:user) }
|
has_many :relationships, -> { includes(:user) }
|
||||||
has_many :users, through: :relationships
|
has_many :users, through: :relationships
|
||||||
|
@ -50,7 +51,6 @@
|
||||||
|
|
||||||
vendor.users.to_a # => No exception is raised
|
vendor.users.to_a # => No exception is raised
|
||||||
|
|
||||||
|
|
||||||
Fixes #12242, #9517, #10240.
|
Fixes #12242, #9517, #10240.
|
||||||
|
|
||||||
*Paul Nikitochkin*
|
*Paul Nikitochkin*
|
||||||
|
|
|
@ -2,7 +2,7 @@ module ActiveRecord
|
||||||
# Declare an enum attribute where the values map to integers in the database, but can be queried by name. Example:
|
# Declare an enum attribute where the values map to integers in the database, but can be queried by name. Example:
|
||||||
#
|
#
|
||||||
# class Conversation < ActiveRecord::Base
|
# class Conversation < ActiveRecord::Base
|
||||||
# enum status: %i( active archived )
|
# enum status: [:active, :archived]
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# Conversation::STATUS # => { active: 0, archived: 1 }
|
# Conversation::STATUS # => { active: 0, archived: 1 }
|
||||||
|
@ -22,7 +22,7 @@ module ActiveRecord
|
||||||
#
|
#
|
||||||
# You can set the default value from the database declaration, like:
|
# You can set the default value from the database declaration, like:
|
||||||
#
|
#
|
||||||
# create_table :conversation do
|
# create_table :conversations do |t|
|
||||||
# t.column :status, :integer, default: 0
|
# t.column :status, :integer, default: 0
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
|
|
|
@ -5,7 +5,7 @@ class StoreTest < ActiveRecord::TestCase
|
||||||
fixtures :books
|
fixtures :books
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
@book = Book.create! name: 'REMOTE'
|
@book = books(:awdr)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "query state by predicate" do
|
test "query state by predicate" do
|
||||||
|
|
|
@ -7,5 +7,5 @@ class Book < ActiveRecord::Base
|
||||||
has_many :subscriptions
|
has_many :subscriptions
|
||||||
has_many :subscribers, through: :subscriptions
|
has_many :subscribers, through: :subscriptions
|
||||||
|
|
||||||
enum status: %i( proposed written published )
|
enum status: [:proposed, :written, :published]
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
</p>
|
</p>
|
||||||
<% end %>
|
<% end %>
|
||||||
<p>
|
<p>
|
||||||
The guides for Rails 3.2.x are available at <a href="http://guides.rubyonrails.org/v3.2.14/">http://guides.rubyonrails.org/v3.2.14/</a>.
|
The guides for Rails 3.2.x are available at <a href="http://guides.rubyonrails.org/v3.2.15/">http://guides.rubyonrails.org/v3.2.15/</a>.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
The guides for Rails 2.3.x are available at <a href="http://guides.rubyonrails.org/v2.3.11/">http://guides.rubyonrails.org/v2.3.11/</a>.
|
The guides for Rails 2.3.x are available at <a href="http://guides.rubyonrails.org/v2.3.11/">http://guides.rubyonrails.org/v2.3.11/</a>.
|
||||||
|
|
Loading…
Reference in a new issue