improve support for rspec scaffolding (thanks voxik) - closes #176

This commit is contained in:
Ryan Bates 2010-12-21 14:18:20 -08:00
parent 5e1e6e182b
commit 4339ac6546
2 changed files with 6 additions and 8 deletions

View File

@ -61,20 +61,18 @@ module CanCan
end end
def build_resource def build_resource
if @options[:singleton] && resource_base.respond_to?("build_#{name}") method_name = @options[:singleton] && resource_base.respond_to?("build_#{name}") ? "build_#{name}" : "new"
resource = resource_base.send("build_#{name}") resource = resource_base.send(method_name, @params[name] || {})
else
resource = resource_base.send("new")
end
initial_attributes.each do |name, value| initial_attributes.each do |name, value|
resource.send("#{name}=", value) resource.send("#{name}=", value)
end end
resource.attributes = @params[name] if @params[name]
resource resource
end end
def initial_attributes def initial_attributes
current_ability.attributes_for(@params[:action].to_sym, resource_class) current_ability.attributes_for(@params[:action].to_sym, resource_class).delete_if do |key, value|
@params[name] && @params[name].include?(key)
end
end end
def find_resource def find_resource

View File

@ -245,7 +245,7 @@ describe CanCan::ControllerResource do
@params.merge!(:action => "create", :project => {:name => "foobar"}) @params.merge!(:action => "create", :project => {:name => "foobar"})
category = Object.new category = Object.new
@controller.instance_variable_set(:@category, category) @controller.instance_variable_set(:@category, category)
stub(category).build_project { Project.new } stub(category).build_project { |attributes| Project.new(attributes) }
resource = CanCan::ControllerResource.new(@controller, :through => :category, :singleton => true) resource = CanCan::ControllerResource.new(@controller, :through => :category, :singleton => true)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).name.should == "foobar" @controller.instance_variable_get(:@project).name.should == "foobar"