Make sure conditions on associations are pluralized
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 = []
|
||||
|
||||
Reference in New Issue
Block a user