Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'resource_params' into strong-parameters
- Loading branch information
Showing
5 changed files
with
88 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module ResourceParams | ||
def resource_params | ||
params.require(resource_name).permit *resource_class.permitted_params | ||
end | ||
|
||
private | ||
|
||
def resource_name | ||
@_resource_name ||= resource_class_name.underscore.to_sym | ||
end | ||
|
||
def resource_class | ||
@_resource_class ||= Module.const_get resource_class_name | ||
end | ||
|
||
def resource_class_name | ||
@_resource_class_name ||= self.class.name.chomp("Controller").singularize | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'spec_helper' | ||
require 'resource_params' | ||
|
||
describe ResourceParams do | ||
describe '#resource_params' do | ||
it "returns the model's list of permitted params" do | ||
resource_name = Faker::Lorem.words(1).first | ||
|
||
model_name = resource_name.titleize | ||
|
||
model_class = Class.new | ||
|
||
controller_name = "#{model_name.pluralize}Controller" | ||
controller_class = Class.new ApplicationController | ||
controller_class.class_eval { include ResourceParams } | ||
|
||
permitted_params = Faker::Lorem.words.map &:to_sym | ||
model_class.stub permitted_params: permitted_params | ||
|
||
with_consts controller_name => controller_class, model_name => model_class do | ||
controller = controller_class.new | ||
|
||
params = mock 'Params' | ||
required = mock 'Required' | ||
params.should_receive(:require).with(resource_name.to_sym).and_return required | ||
required.should_receive(:permit).with *permitted_params | ||
controller.stub params: params | ||
|
||
controller.resource_params | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def with_consts(definitions, &block) | ||
old_consts = {} | ||
definitions.each do |name, value| | ||
old_consts.name = value if Object.const_defined? name | ||
Object.const_set name, value | ||
end | ||
yield | ||
ensure | ||
definitions.keys.each do |name| | ||
Object.send :remove_const, name | ||
end | ||
old_consts.each do |name, value| | ||
Object.const_set name, value | ||
end | ||
end | ||
end |