Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic strong parameters detection #154

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,11 @@ p[:occupation] # => 'Rubyist'
```

### Mash and Rails 4 Strong Parameters
When using Mash with [Rails 4 Strong Parameters](http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters) Mash will automatically detect presence of strong parameters.
**No further action is required.**

Add the following initializer in config/initializers/mash.rb when using Mash with [Rails 4 Strong Parameters](http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters). This prevents Mash from responding to `:permitted?` and therefore triggering an ActiveModel `ForbiddenAttributesProtection` exception.
However, If strong parameters can't be detected automatically try to include extension manually:
Add the following initializer in config/initializers/mash.rb. This prevents Mash from responding to `:permitted?` and therefore triggering an ActiveModel `ForbiddenAttributesProtection` exception.

```ruby
class Mash
Expand Down
1 change: 1 addition & 0 deletions hashie.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Gem::Specification.new do |gem|

gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'actionpack'
end
3 changes: 2 additions & 1 deletion lib/hashie/mash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ module Hashie
#
class Mash < Hash
include Hashie::Extensions::PrettyInspect
include Hashie::Extensions::Mash::ActiveModel if defined?(ActionController::StrongParameters)

ALLOWED_SUFFIXES = %w(? ! = _)
ALLOWED_SUFFIXES = %w(? ! = _) unless defined?(ALLOWED_SUFFIXES)

alias_method :to_s, :inspect

Expand Down
12 changes: 12 additions & 0 deletions spec/hashie/mash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,16 @@ class SubMash < Hashie::Mash
expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => { '345' => 'hey' }]
end
end

describe 'it includes active model extensions' do
it 'includes active model extensions for strong parameters' do
require 'action_controller'
load 'hashie/mash.rb'
Hashie::Mash.included_modules.should include(Hashie::Extensions::Mash::ActiveModel)
end
it 'does not include active model extensions for strong parameters' do
load 'hashie/mash.rb'
Hashie::Mash.included_modules.should_not include(Hashie::Extensions::Mash::ActiveModel)
end
end
end