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

Integrate with strong_parameters #260

Merged
merged 3 commits into from Mar 25, 2013
Merged
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
10 changes: 10 additions & 0 deletions README.rdoc
Expand Up @@ -539,6 +539,16 @@ And then you can rewrite the last example as:
end
end

== Strong Parameters

If your controller defines a method named permitted_params, Inherited Resources will call it where it would normally call params. This allows for easy integration with the strong_parameters gem:

def permitted_params
params.permit(:widget => [:permitted_field, :other_permitted_field])
end

Note that this doesn't work if you use strong_parameters' require method instead of permit, because whereas permit returns the entire sanitized parameter hash, require returns only the sanitized params below the parameter you required.

== Bugs and Feedback

If you discover any bugs, please describe it in the issues tracker, including Rails and Inherited Resources versions.
Expand Down
3 changes: 2 additions & 1 deletion lib/inherited_resources/base_helpers.rb
Expand Up @@ -305,7 +305,8 @@ def resource_params

# extract attributes from params
def build_resource_params
rparams = [params[resource_request_name] || params[resource_instance_name] || {}]
parameters = respond_to?(:permitted_params) ? permitted_params : params
rparams = [parameters[resource_request_name] || parameters[resource_instance_name] || {}]
if without_protection_given?
rparams << without_protection
else
Expand Down
34 changes: 34 additions & 0 deletions test/strong_parameters_test.rb
@@ -0,0 +1,34 @@
require File.expand_path('test_helper', File.dirname(__FILE__))

class Widget
extend ActiveModel::Naming
end

class WidgetsController < InheritedResources::Base
end

class StrongParametersTest < ActionController::TestCase
def setup
@controller = WidgetsController.new
@controller.stubs(:widget_url).returns("/")
@controller.stubs(:permitted_params).returns(:widget => {:permitted => 'param'})
end

def test_permitted_params_from_new
Widget.expects(:new).with(:permitted => 'param')
get :new, :widget => { :permitted => 'param', :prohibited => 'param' }
end

def test_permitted_params_from_create
Widget.expects(:new).with(:permitted => 'param').returns(mock(:save => true))
post :create, :widget => { :permitted => 'param', :prohibited => 'param' }
end

def test_permitted_params_from_update
mock_widget = mock
mock_widget.stubs(:class).returns(Widget)
mock_widget.expects(:update_attributes).with(:permitted => 'param')
Widget.expects(:find).with('42').returns(mock_widget)
put :update, :id => '42', :widget => {:permitted => 'param', :prohibited => 'param'}
end
end
Empty file added test/views/widgets/new.html.erb
Empty file.