renaming :singular resource option to :singleton

This commit is contained in:
Ryan Bates 2010-08-06 13:06:18 -07:00
parent 7d9e710f05
commit c9e0f4e3ef
4 changed files with 12 additions and 12 deletions

View File

@ -1,6 +1,6 @@
* Adding :find_by option to load_resource - see issue #19 * Adding :find_by option to load_resource - see issue #19
* Adding :singular option to load_resource - see issue #93 * Adding :singleton option to load_resource - see issue #93
* Supporting multiple resources in :through option for polymorphic * Supporting multiple resources in :through option for polymorphic
associations - see issue #73 associations - see issue #73

View File

@ -67,8 +67,8 @@ module CanCan
# [:+through+] # [:+through+]
# Load this resource through another one. This should match the name of the parent instance variable. # Load this resource through another one. This should match the name of the parent instance variable.
# #
# [:+singular+] # [:+singleton+]
# Pass +true+ if this is a singular resource through a +has_one+ association. # Pass +true+ if this is a singleton resource through a +has_one+ association.
# #
# [:+parent+] # [:+parent+]
# True or false depending on if the resource is considered a parent resource. This defaults to +true+ if a resource # True or false depending on if the resource is considered a parent resource. This defaults to +true+ if a resource

View File

@ -42,18 +42,18 @@ module CanCan
def load_resource_instance def load_resource_instance
if !parent? && new_actions.include?(@params[:action].to_sym) if !parent? && new_actions.include?(@params[:action].to_sym)
build_resource build_resource
elsif id_param || @options[:singular] elsif id_param || @options[:singleton]
find_resource find_resource
end end
end end
def build_resource def build_resource
method_name = @options[:singular] ? "build_#{name}" : "new" method_name = @options[:singleton] ? "build_#{name}" : "new"
resource_base.send(*[method_name, @params[name]].compact) resource_base.send(*[method_name, @params[name]].compact)
end end
def find_resource def find_resource
if @options[:singular] if @options[:singleton]
resource_base.send(name) resource_base.send(name)
else else
@options[:find_by] ? resource_base.send("find_by_#{@options[:find_by]}!", id_param) : resource_base.find(id_param) @options[:find_by] ? resource_base.send("find_by_#{@options[:find_by]}!", id_param) : resource_base.find(id_param)
@ -90,10 +90,10 @@ module CanCan
# The object that methods (such as "find", "new" or "build") are called on. # The object that methods (such as "find", "new" or "build") are called on.
# If the :through option is passed it will go through an association on that instance. # If the :through option is passed it will go through an association on that instance.
# If the :singular option is passed it won't use the association because it needs to be handled later. # If the :singleton option is passed it won't use the association because it needs to be handled later.
def resource_base def resource_base
if through_resource if through_resource
@options[:singular] ? through_resource : through_resource.send(name.to_s.pluralize) @options[:singleton] ? through_resource : through_resource.send(name.to_s.pluralize)
else else
resource_class resource_class
end end

View File

@ -166,22 +166,22 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@ability).should == :some_ability @controller.instance_variable_get(:@ability).should == :some_ability
end end
it "should find record through has_one association with :singular option" do it "should find record through has_one association with :singleton option" do
@params.merge!(:action => "show") @params.merge!(:action => "show")
person = Object.new person = Object.new
@controller.instance_variable_set(:@person, person) @controller.instance_variable_set(:@person, person)
stub(person).ability { :some_ability } stub(person).ability { :some_ability }
resource = CanCan::ControllerResource.new(@controller, :through => :person, :singular => true) resource = CanCan::ControllerResource.new(@controller, :through => :person, :singleton => true)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@ability).should == :some_ability @controller.instance_variable_get(:@ability).should == :some_ability
end end
it "should build record through has_one association with :singular option" do it "should build record through has_one association with :singleton option" do
@params.merge!(:action => "create", :ability => :ability_attributes) @params.merge!(:action => "create", :ability => :ability_attributes)
person = Object.new person = Object.new
@controller.instance_variable_set(:@person, person) @controller.instance_variable_set(:@person, person)
stub(person).build_ability(:ability_attributes) { :new_ability } stub(person).build_ability(:ability_attributes) { :new_ability }
resource = CanCan::ControllerResource.new(@controller, :through => :person, :singular => true) resource = CanCan::ControllerResource.new(@controller, :through => :person, :singleton => true)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@ability).should == :new_ability @controller.instance_variable_get(:@ability).should == :new_ability
end end