bringing up to date with master branch

This commit is contained in:
Ryan Bates
2012-06-26 17:10:01 -07:00
17 changed files with 269 additions and 24 deletions

View File

@@ -254,6 +254,13 @@ module CanCan
@fully_authorized << [action.to_sym, subject.to_sym]
end
def merge(ability)
ability.send(:rules).each do |rule|
rules << rule.dup
end
self
end
private
def unauthorized_message_keys(action, subject)

View File

@@ -94,7 +94,7 @@ module CanCan
# [:+find_by+]
# Find using a different attribute other than id. For example.
#
# load_resource :find_by => :permalink # will use find_by_permlink!(params[:id])
# load_resource :find_by => :permalink # will use find_by_permalink!(params[:id])
#
# [:+collection+]
# Specify which actions are resource collection actions in addition to :+index+. This
@@ -152,6 +152,9 @@ module CanCan
# [:+except+]
# Does not apply before filter to given actions.
#
# [:+singleton+]
# Pass +true+ if this is a singleton resource through a +has_one+ association.
#
# [:+parent+]
# True or false depending on if the resource is considered a parent resource. This defaults to +true+ if a resource
# name is given which does not match the controller.
@@ -382,7 +385,7 @@ module CanCan
end
end
if defined? ActionController
if defined? ActionController::Base
ActionController::Base.class_eval do
include CanCan::ControllerAdditions
end

View File

@@ -6,8 +6,8 @@ module CanCan
options = args.extract_options!.merge(behavior)
resource_name = args.first
before_filter_method = options.delete(:prepend) ? :prepend_before_filter : :before_filter
controller_class.send(before_filter_method, options.slice(:only, :except)) do |controller|
controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except)).process
controller_class.send(before_filter_method, options.slice(:only, :except, :if, :unless)) do |controller|
controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except, :if, :unless)).process
end
end
@@ -81,6 +81,10 @@ module CanCan
def build_resource
resource = resource_base.new(resource_params || {})
assign_attributes(resource)
end
def assign_attributes(resource)
resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource
initial_attributes.each do |attr_name, value|
resource.send("#{attr_name}=", value)
@@ -225,12 +229,19 @@ module CanCan
end
def resource_params
# since Rails includes the namespace in the params sent by the form (issue #349)
@params[namespaced_name.to_s.underscore.gsub("/", "_")]
if @options[:class]
@params[@options[:class].to_s.underscore.gsub('/', '_')]
else
@params[namespaced_name.to_s.underscore.gsub("/", "_")]
end
end
def namespace
@params[:controller].split("::")[0..-2]
end
def namespaced_name
(@name || @params[:controller].sub("Controller", "")).singularize.camelize.constantize
[namespace, name.camelize].join('::').singularize.camelize.constantize
rescue NameError
name
end

View File

@@ -6,7 +6,8 @@ module CanCan
@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
assign_attributes(resource)
else
@controller.send :resource
end

View File

@@ -89,7 +89,12 @@ module CanCan
if override_scope
@model_class.scoped.merge(override_scope)
elsif @model_class.respond_to?(:where) && @model_class.respond_to?(:joins)
@model_class.where(conditions).joins(joins)
mergeable_conditions = @rules.select {|rule| rule.unmergeable? }.blank?
if mergeable_conditions
@model_class.where(conditions).joins(joins)
else
@model_class.where(*(@rules.map(&:conditions))).joins(joins)
end
else
@model_class.scoped(:conditions => conditions, :joins => joins)
end

View File

@@ -30,8 +30,9 @@ module CanCan
else
# we only need to process can rules if
# there are no rules with empty conditions
rules = @rules.reject { |rule| rule.conditions.empty? }
rules = @rules.reject { |rule| rule.conditions.empty? && rule.base_behavior }
process_can_rules = @rules.count == rules.count
rules.inject(@model_class.all) do |records, rule|
if process_can_rules && rule.base_behavior
records.or rule.conditions

View File

@@ -63,6 +63,10 @@ module CanCan
@block || conditions?
end
def unmergeable?
@conditions.respond_to?(:keys) && (! @conditions.keys.first.kind_of? Symbol)
end
def associations_hash(conditions = @conditions)
hash = {}
conditions.map do |name, value|
@@ -139,7 +143,7 @@ module CanCan
else
attribute && matches_conditions_hash?(attribute, value)
end
elsif value.kind_of?(Array) || value.kind_of?(Range)
elsif value.kind_of?(Enumerable)
value.include? attribute
else
attribute == value
@@ -151,7 +155,7 @@ module CanCan
end
def nested_subject_matches_conditions?(subject_hash)
parent, child = subject_hash.shift
parent, child = subject_hash.first
matches_conditions_hash?(parent, @conditions[parent.class.name.downcase.to_sym] || {})
end
@@ -168,7 +172,7 @@ module CanCan
end
def model_adapter(subject)
ModelAdapters::AbstractAdapter.adapter_class(subject_object?(subject) ? subject.class : subject)
CanCan::ModelAdapters::AbstractAdapter.adapter_class(subject_object?(subject) ? subject.class : subject)
end
end
end