Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Init new inherited resources #639

Closed
wants to merge 2 commits into from

2 participants

@mikepack
Collaborator

No description provided.

@mikepack mikepack closed this
@mikepack
Collaborator

Ment to amend. Closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 11, 2012
  1. @mccraigmccraig

    initialise attributes after a resource is created by an

    mccraigmccraig authored
    InheritedResources controller
Commits on Jun 5, 2012
  1. @mikepack

    Add specs for resource attributes.

    mikepack authored
    Remove inconsistent line breaks.
This page is out of date. Refresh to see the latest.
View
6 lib/cancan/inherited_resource.rb
@@ -6,7 +6,11 @@ def load_resource_instance
@controller.send :association_chain
@controller.instance_variable_get("@#{instance_name}")
elsif new_actions.include? @params[:action].to_sym
- @controller.send :build_resource
+ resource = @controller.send :build_resource
+ initial_attributes.each do |attr_name, value|
+ resource.send("#{attr_name}=", value)
+ end
+ resource
else
@controller.send :resource
end
View
18 spec/cancan/inherited_resource_spec.rb
@@ -39,4 +39,22 @@
CanCan::InheritedResource.new(@controller).load_resource
@controller.instance_variable_get(:@projects).should == :projects
end
+
+ it "should build a new resource with attributes from current ability" do
+ @params[:action] = "new"
+ @ability.can(:create, Project, :name => "from conditions")
+ stub(@controller).build_resource { Struct.new(:name).new }
+ resource = CanCan::InheritedResource.new(@controller)
+ resource.load_resource
+ @controller.instance_variable_get(:@project).name.should == "from conditions"
+ end
+
+ it "should override initial attributes with params" do
+ @params.merge!(:action => "new", :project => {:name => "from params"})
+ @ability.can(:create, Project, :name => "from conditions")
+ stub(@controller).build_resource { Struct.new(:name).new }
+ resource = CanCan::ControllerResource.new(@controller)
+ resource.load_resource
+ @controller.instance_variable_get(:@project).name.should == "from params"
+ end
end
Something went wrong with that request. Please try again.