Make sure conditions on associations are pluralized

This commit is contained in:
Logan Raarup
2010-05-11 19:30:28 +08:00
committed by Ryan Bates
parent 06296b0a40
commit 605063b974
5 changed files with 26 additions and 6 deletions

View File

@@ -190,11 +190,11 @@ module CanCan
# If the ability is not defined then false is returned so be sure to take that into consideration.
# If the ability is defined using a block then this will raise an exception since a hash of conditions cannot be
# determined from that.
def conditions(action, subject)
def conditions(action, subject, options = {})
can_definition = matching_can_definition(action, subject)
if can_definition
raise Error, "Cannot determine ability conditions from block for #{action.inspect} #{subject.inspect}" if can_definition.block
can_definition.conditions || {}
can_definition.conditions(options) || {}
else
false
end

View File

@@ -20,7 +20,7 @@ module CanCan
# Here only the articles which the user can update are returned. This
# internally uses Ability#conditions method, see that for more information.
def accessible_by(ability, action = :read)
conditions = ability.conditions(action, self) || {:id => nil}
conditions = ability.conditions(action, self, :tableize => true) || {:id => nil}
joins = ability.association_joins(action, self)
if respond_to? :where
where(conditions).joins(joins)

View File

@@ -1,7 +1,8 @@
module CanCan
# This class is used internally and should only be called through Ability.
class CanDefinition # :nodoc:
attr_reader :conditions, :block
include ActiveSupport::Inflector
attr_reader :block
def initialize(base_behavior, action, subject, conditions, block)
@base_behavior = base_behavior
@@ -25,6 +26,19 @@ module CanCan
result = can_without_base_behavior?(action, subject, extra_args)
@base_behavior ? result : !result
end
def conditions(options = {})
if options[:tableize] and @conditions.kind_of? Hash
@conditions.inject({}) do |tableized_conditions, (name, value)|
name = tableize(name).to_sym if value.kind_of? Hash
tableized_conditions[name] = value
tableized_conditions
end
else
@conditions
end
end
def association_joins(conditions = @conditions)
joins = []