mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Namespace HashWithIndifferentAccess
This commit is contained in:
parent
033248b5fb
commit
c8e632bd9f
11 changed files with 17 additions and 19 deletions
|
@ -228,7 +228,7 @@ module ActionController
|
|||
end
|
||||
|
||||
def decode_credentials(header)
|
||||
HashWithIndifferentAccess[header.to_s.gsub(/^Digest\s+/,'').split(',').map do |pair|
|
||||
ActiveSupport::HashWithIndifferentAccess[header.to_s.gsub(/^Digest\s+/, '').split(',').map do |pair|
|
||||
key, value = pair.split('=', 2)
|
||||
[key.strip, value.to_s.gsub(/^"|"$/,'').delete('\'')]
|
||||
end]
|
||||
|
|
|
@ -72,7 +72,7 @@ module ActionDispatch
|
|||
end
|
||||
end
|
||||
|
||||
# Convert nested Hash to HashWithIndifferentAccess
|
||||
# Convert nested Hash to ActiveSupport::HashWithIndifferentAccess
|
||||
def normalize_parameters(value)
|
||||
case value
|
||||
when Hash
|
||||
|
|
|
@ -75,7 +75,7 @@ module ActionDispatch
|
|||
end
|
||||
|
||||
module Upload # :nodoc:
|
||||
# Convert nested Hash to HashWithIndifferentAccess and replace
|
||||
# Convert nested Hash to ActiveSupport::HashWithIndifferentAccess and replace
|
||||
# file upload hash with UploadedFile objects
|
||||
def normalize_parameters(value)
|
||||
if Hash === value && value.has_key?(:tempfile)
|
||||
|
|
|
@ -2,7 +2,6 @@ require 'abstract_unit'
|
|||
|
||||
module AbstractController
|
||||
module Testing
|
||||
|
||||
class UrlForTest < ActionController::TestCase
|
||||
class W
|
||||
include ActionDispatch::Routing::RouteSet.new.tap { |r| r.draw { get ':controller(/:action(/:id(.:format)))' } }.url_helpers
|
||||
|
@ -350,10 +349,10 @@ module AbstractController
|
|||
def test_with_hash_with_indifferent_access
|
||||
W.default_url_options[:controller] = 'd'
|
||||
W.default_url_options[:only_path] = false
|
||||
assert_equal("/c", W.new.url_for(HashWithIndifferentAccess.new('controller' => 'c', 'only_path' => true)))
|
||||
assert_equal("/c", W.new.url_for(ActiveSupport::HashWithIndifferentAccess.new('controller' => 'c', 'only_path' => true)))
|
||||
|
||||
W.default_url_options[:action] = 'b'
|
||||
assert_equal("/c/a", W.new.url_for(HashWithIndifferentAccess.new('controller' => 'c', 'action' => 'a', 'only_path' => true)))
|
||||
assert_equal("/c/a", W.new.url_for(ActiveSupport::HashWithIndifferentAccess.new('controller' => 'c', 'action' => 'a', 'only_path' => true)))
|
||||
end
|
||||
|
||||
def test_url_params_with_nil_to_param_are_not_in_url
|
||||
|
|
|
@ -119,7 +119,7 @@ module ActiveModel
|
|||
# person.name = 'bob'
|
||||
# person.changes # => { "name" => ["bill", "bob"] }
|
||||
def changes
|
||||
HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }]
|
||||
ActiveSupport::HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }]
|
||||
end
|
||||
|
||||
# Returns a hash of attributes that were changed before the model was saved.
|
||||
|
|
|
@ -107,7 +107,7 @@ module ActiveRecord
|
|||
private
|
||||
def initialize_store_attribute(store_attribute)
|
||||
attribute = send(store_attribute)
|
||||
unless attribute.is_a?(HashWithIndifferentAccess)
|
||||
unless attribute.is_a?(ActiveSupport::HashWithIndifferentAccess)
|
||||
attribute = IndifferentCoder.as_indifferent_hash(attribute)
|
||||
send :"#{store_attribute}=", attribute
|
||||
end
|
||||
|
@ -134,12 +134,12 @@ module ActiveRecord
|
|||
|
||||
def self.as_indifferent_hash(obj)
|
||||
case obj
|
||||
when HashWithIndifferentAccess
|
||||
when ActiveSupport::HashWithIndifferentAccess
|
||||
obj
|
||||
when Hash
|
||||
obj.with_indifferent_access
|
||||
else
|
||||
HashWithIndifferentAccess.new
|
||||
ActiveSupport::HashWithIndifferentAccess.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -311,7 +311,7 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_should_also_work_with_a_HashWithIndifferentAccess
|
||||
@pirate.ship_attributes = HashWithIndifferentAccess.new(:id => @ship.id, :name => 'Davy Jones Gold Dagger')
|
||||
@pirate.ship_attributes = ActiveSupport::HashWithIndifferentAccess.new(:id => @ship.id, :name => 'Davy Jones Gold Dagger')
|
||||
|
||||
assert @pirate.ship.persisted?
|
||||
assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
|
||||
|
@ -593,7 +593,7 @@ module NestedAttributesOnACollectionAssociationTests
|
|||
end
|
||||
|
||||
def test_should_also_work_with_a_HashWithIndifferentAccess
|
||||
@pirate.send(association_setter, HashWithIndifferentAccess.new('foo' => HashWithIndifferentAccess.new(:id => @child_1.id, :name => 'Grace OMalley')))
|
||||
@pirate.send(association_setter, ActiveSupport::HashWithIndifferentAccess.new('foo' => ActiveSupport::HashWithIndifferentAccess.new(:id => @child_1.id, :name => 'Grace OMalley')))
|
||||
@pirate.save
|
||||
assert_equal 'Grace OMalley', @child_1.reload.name
|
||||
end
|
||||
|
|
|
@ -67,9 +67,9 @@ class StoreTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
test "preserve store attributes data in HashWithIndifferentAccess format without any conversion" do
|
||||
@john.json_data = HashWithIndifferentAccess.new(:height => 'tall', 'weight' => 'heavy')
|
||||
@john.json_data = ActiveSupport::HashWithIndifferentAccess.new(:height => 'tall', 'weight' => 'heavy')
|
||||
@john.height = 'low'
|
||||
assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess)
|
||||
assert_equal true, @john.json_data.instance_of?(ActiveSupport::HashWithIndifferentAccess)
|
||||
assert_equal 'low', @john.json_data[:height]
|
||||
assert_equal 'low', @john.json_data['height']
|
||||
assert_equal 'heavy', @john.json_data[:weight]
|
||||
|
@ -95,7 +95,7 @@ class StoreTest < ActiveRecord::TestCase
|
|||
test "convert store attributes from any format other than Hash or HashWithIndifferent access losing the data" do
|
||||
@john.json_data = "somedata"
|
||||
@john.height = 'low'
|
||||
assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess)
|
||||
assert_equal true, @john.json_data.instance_of?(ActiveSupport::HashWithIndifferentAccess)
|
||||
assert_equal 'low', @john.json_data[:height]
|
||||
assert_equal 'low', @john.json_data['height']
|
||||
assert_equal false, @john.json_data.delete_if { |k, v| k == 'height' }.any?
|
||||
|
@ -143,5 +143,4 @@ class StoreTest < ActiveRecord::TestCase
|
|||
assert_raise(NoMethodError) { @john.stored_attributes = Hash.new }
|
||||
assert_raise(NoMethodError) { @john.stored_attributes }
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@ require 'active_support/core_ext/object/deep_dup'
|
|||
require 'active_support/inflections'
|
||||
|
||||
class HashExtTest < ActiveSupport::TestCase
|
||||
class IndifferentHash < HashWithIndifferentAccess
|
||||
class IndifferentHash < ActiveSupport::HashWithIndifferentAccess
|
||||
end
|
||||
|
||||
class SubclassingArray < Array
|
||||
|
|
|
@ -114,7 +114,7 @@ To send a hash you include the key name inside the brackets:
|
|||
|
||||
When this form is submitted, the value of `params[:client]` will be `{"name" => "Acme", "phone" => "12345", "address" => {"postcode" => "12345", "city" => "Carrot City"}}`. Note the nested hash in `params[:client][:address]`.
|
||||
|
||||
Note that the `params` hash is actually an instance of `HashWithIndifferentAccess` from Active Support, which acts like a hash that lets you use symbols and strings interchangeably as keys.
|
||||
Note that the `params` hash is actually an instance of `ActiveSupport::HashWithIndifferentAccess`, which acts like a hash that lets you use symbols and strings interchangeably as keys.
|
||||
|
||||
### JSON/XML parameters
|
||||
|
||||
|
|
|
@ -417,7 +417,7 @@ def create
|
|||
end
|
||||
```
|
||||
|
||||
The `render` method here is taking a very simple hash with a key of `text` and value of `params[:post].inspect`. The `params` method is the object which represents the parameters (or fields) coming in from the form. The `params` method returns a `HashWithIndifferentAccess` object, which allows you to access the keys of the hash using either strings or symbols. In this situation, the only parameters that matter are the ones from the form.
|
||||
The `render` method here is taking a very simple hash with a key of `text` and value of `params[:post].inspect`. The `params` method is the object which represents the parameters (or fields) coming in from the form. The `params` method returns an `ActiveSupport::HashWithIndifferentAccess` object, which allows you to access the keys of the hash using either strings or symbols. In this situation, the only parameters that matter are the ones from the form.
|
||||
|
||||
If you re-submit the form one more time you'll now no longer get the missing template error. Instead, you'll see something that looks like the following:
|
||||
|
||||
|
|
Loading…
Reference in a new issue