bringing up to date with master branch

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

1
.rbenv-version Normal file
View File

@ -0,0 +1 @@
1.9.3-p194

View File

@ -1,3 +1,48 @@
1.6.8 (June 25, 2012)
* improved support for namespaced controllers and models
* pass :if and :unless options for load and authorize resource (thanks mauriciozaffari)
* Travis CI badge (thanks plentz)
* adding Ability#merge for combining multiple abilities (thanks rogercampos)
* support for multiple MetaWhere rules (thanks andhapp)
* various fixes for DataMapper, Mongoid, and Inherited Resource integration
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.7...1.6.8]
1.6.7 (October 4, 2011)
* fixing nested resource problem caused by namespace addition - issue #482
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.6...1.6.7]
1.6.6 (September 28, 2011)
* correct "return cant jump across threads" error when using check_authorization (thanks codeprimate) - issues #463, #469
* fixing tests in development by specifying with_model version (thanks kirkconnell) - issue #476
* added travis.yml file for TravisCI support (thanks bai) - issue #427
* better support for namespaced models (thanks whilefalse) - issues #424
* adding :id_param option to load_and_authorize_resource (thanks skhisma) - issue #425
* make default unauthorized message translatable text (thanks nhocki) - issue #409
* improving DataMapper behavior (thanks psanford, maxsum-corin) - issue #410, #373
* allow :find_by option to be full find method name - issue #335
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.5...1.6.6]
1.6.5 (May 18, 2011) 1.6.5 (May 18, 2011)
* pass action and subject through AccessDenied exception when :through isn't found - issue #366 * pass action and subject through AccessDenied exception when :through isn't found - issue #366
@ -14,12 +59,15 @@
* improve scope merging - issue #328 * improve scope merging - issue #328
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.4...1.6.5]
1.6.4 (March 29, 2011) 1.6.4 (March 29, 2011)
* Fixed mongoid 'or' error - see issue #322 * Fixed mongoid 'or' error - see issue #322
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.3...1.6.4]
1.6.3 (March 25, 2011) 1.6.3 (March 25, 2011)
@ -27,11 +75,15 @@
* Return subject passed to authorize! - see issue #314 * Return subject passed to authorize! - see issue #314
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.2...1.6.3]
1.6.2 (March 18, 2011) 1.6.2 (March 18, 2011)
* Fixed instance loading when :singleton option is used - see issue #310 * Fixed instance loading when :singleton option is used - see issue #310
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.1...1.6.2]
1.6.1 (March 15, 2011) 1.6.1 (March 15, 2011)
@ -41,6 +93,8 @@
* Reverted Inherited Resources "collection" override since it doesn't seem to be working - see issue #305 * Reverted Inherited Resources "collection" override since it doesn't seem to be working - see issue #305
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.0...1.6.1]
1.6.0 (March 11, 2011) 1.6.0 (March 11, 2011)
@ -62,6 +116,8 @@
* Raise an exception when trying to make a Ability condition with both a hash of conditions and a block - see issue #269 * Raise an exception when trying to make a Ability condition with both a hash of conditions and a block - see issue #269
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.5.1...1.6.0]
1.5.1 (January 20, 2011) 1.5.1 (January 20, 2011)
@ -69,6 +125,8 @@
* Improving Mongoid support for multiple can and cannot definitions (thanks stellard) - see issue #239 * Improving Mongoid support for multiple can and cannot definitions (thanks stellard) - see issue #239
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.5.0...1.5.1]
1.5.0 (January 11, 2011) 1.5.0 (January 11, 2011)
@ -90,6 +148,8 @@
* Internal: added .rvmrc to auto-switch to 1.8.7 with gemset - see issue #231 * Internal: added .rvmrc to auto-switch to 1.8.7 with gemset - see issue #231
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.4.1...1.5.0]
1.4.1 (November 12, 2010) 1.4.1 (November 12, 2010)
@ -103,6 +163,8 @@
* Fix odd behavior when "cache_classes = false" (thanks mphalliday) - see issue #174 * Fix odd behavior when "cache_classes = false" (thanks mphalliday) - see issue #174
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.4.0...1.4.1]
1.4.0 (October 5, 2010) 1.4.0 (October 5, 2010)
@ -142,11 +204,15 @@
* No longer calling block in +can+ definition when checking on class - see issue #116 * No longer calling block in +can+ definition when checking on class - see issue #116
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.4...1.4.0]
1.3.4 (August 31, 2010) 1.3.4 (August 31, 2010)
* Don't stop at +cannot+ with hash conditions when checking class (thanks tamoya) - see issue #131 * Don't stop at +cannot+ with hash conditions when checking class (thanks tamoya) - see issue #131
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.3...1.3.4]
1.3.3 (August 20, 2010) 1.3.3 (August 20, 2010)
@ -154,16 +220,22 @@
* Pluralize nested associations for conditions in accessible_by (thanks mlooney) - see issue #123 * Pluralize nested associations for conditions in accessible_by (thanks mlooney) - see issue #123
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.2...1.3.3]
1.3.2 (August 7, 2010) 1.3.2 (August 7, 2010)
* Fixing slice error when passing in custom resource name - see issue #112 * Fixing slice error when passing in custom resource name - see issue #112
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.1...1.3.2]
1.3.1 (August 6, 2010) 1.3.1 (August 6, 2010)
* Fixing protected sanitize_sql error - see issue #111 * Fixing protected sanitize_sql error - see issue #111
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.0...1.3.1]
1.3.0 (August 6, 2010) 1.3.0 (August 6, 2010)
@ -191,6 +263,8 @@
* Supporting deeply nested aliases - see issue #98 * Supporting deeply nested aliases - see issue #98
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.2.0...1.3.0]
1.2.0 (July 16, 2010) 1.2.0 (July 16, 2010)
@ -206,11 +280,15 @@
* Adding joins clause to accessible_by when conditions are across associations * Adding joins clause to accessible_by when conditions are across associations
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.1.1...1.2.0]
1.1.1 (April 17, 2010) 1.1.1 (April 17, 2010)
* Fixing behavior in Rails 3 by properly initializing ResourceAuthorization * Fixing behavior in Rails 3 by properly initializing ResourceAuthorization
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.1...1.1.1]
1.1.0 (April 17, 2010) 1.1.0 (April 17, 2010)
@ -234,6 +312,8 @@
* Support additional arguments to can? which get passed to the block - see issue #48 * Support additional arguments to can? which get passed to the block - see issue #48
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.0.2...1.1]
1.0.2 (Dec 30, 2009) 1.0.2 (Dec 30, 2009)
@ -243,6 +323,8 @@
* Adding custom message argument to unauthorized! method (thanks tjwallace) - see issue #18 * Adding custom message argument to unauthorized! method (thanks tjwallace) - see issue #18
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.0.1...1.0.2]
1.0.1 (Dec 14, 2009) 1.0.1 (Dec 14, 2009)
@ -250,6 +332,8 @@
* Don't fetch parent of nested resource if *_id parameter is missing so it works with shallow nested routes - see issue #14 * Don't fetch parent of nested resource if *_id parameter is missing so it works with shallow nested routes - see issue #14
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.0.0...1.0.1]
1.0.0 (Dec 13, 2009) 1.0.0 (Dec 13, 2009)
@ -265,6 +349,8 @@
* BACKWARDS INCOMPATIBLE: turning load and authorize resource methods into class methods which set up the before filter so they can accept additional arguments. * BACKWARDS INCOMPATIBLE: turning load and authorize resource methods into class methods which set up the before filter so they can accept additional arguments.
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/0.2.1...1.0.0]
0.2.1 (Nov 26, 2009) 0.2.1 (Nov 26, 2009)
@ -274,6 +360,8 @@
* support custom objects (usually symbols) in can definition - see issue #8 * support custom objects (usually symbols) in can definition - see issue #8
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/0.2.0...0.2.1]
0.2.0 (Nov 17, 2009) 0.2.0 (Nov 17, 2009)
@ -285,6 +373,8 @@
* BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' to set up abilities - see issue #4 * BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' to set up abilities - see issue #4
* {see the full list of changes}[https://github.com/ryanb/cancan/compare/0.1.0...0.2.0]
0.1.0 (Nov 16, 2009) 0.1.0 (Nov 16, 2009)

View File

@ -1,4 +1,4 @@
= CanCan = CanCan {<img src="https://secure.travis-ci.org/ryanb/cancan.png" />}[http://travis-ci.org/ryanb/cancan]
This is the branch for CanCan 2.0 which is in very early development. For a stable release please check out the {master branch}[https://github.com/ryanb/cancan] This is the branch for CanCan 2.0 which is in very early development. For a stable release please check out the {master branch}[https://github.com/ryanb/cancan]

View File

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

View File

@ -94,7 +94,7 @@ module CanCan
# [:+find_by+] # [:+find_by+]
# Find using a different attribute other than id. For example. # 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+] # [:+collection+]
# Specify which actions are resource collection actions in addition to :+index+. This # Specify which actions are resource collection actions in addition to :+index+. This
@ -152,6 +152,9 @@ module CanCan
# [:+except+] # [:+except+]
# Does not apply before filter to given actions. # Does not apply before filter to given actions.
# #
# [:+singleton+]
# 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
# name is given which does not match the controller. # name is given which does not match the controller.
@ -382,7 +385,7 @@ module CanCan
end end
end end
if defined? ActionController if defined? ActionController::Base
ActionController::Base.class_eval do ActionController::Base.class_eval do
include CanCan::ControllerAdditions include CanCan::ControllerAdditions
end end

View File

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

View File

@ -6,7 +6,8 @@ module CanCan
@controller.send :association_chain @controller.send :association_chain
@controller.instance_variable_get("@#{instance_name}") @controller.instance_variable_get("@#{instance_name}")
elsif new_actions.include? @params[:action].to_sym elsif new_actions.include? @params[:action].to_sym
@controller.send :build_resource resource = @controller.send :build_resource
assign_attributes(resource)
else else
@controller.send :resource @controller.send :resource
end end

View File

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

View File

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

View File

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

View File

@ -290,6 +290,16 @@ describe CanCan::Ability do
@ability.should be_fully_authorized(:update, :ranges) @ability.should be_fully_authorized(:update, :ranges)
end end
it "should accept a set as a condition value" do
object_with_foo_2 = Object.new
object_with_foo_2.should_receive(:foo) { 2 }
object_with_foo_3 = Object.new
object_with_foo_3.should_receive(:foo) { 3 }
@ability.can :read, :objects, :foo => [1, 2, 5].to_set
@ability.can?(:read, object_with_foo_2).should be_true
@ability.can?(:read, object_with_foo_3).should be_false
end
it "does not match subjects return nil for methods that must match nested a nested conditions hash" do it "does not match subjects return nil for methods that must match nested a nested conditions hash" do
object_with_foo = Object.new object_with_foo = Object.new
object_with_foo.should_receive(:foo) { :bar } object_with_foo.should_receive(:foo) { :bar }
@ -353,7 +363,6 @@ describe CanCan::Ability do
@ability.can?(:update, :books, :author).should be_false @ability.can?(:update, :books, :author).should be_false
end end
# Hash Association # Hash Association
it "checks permission through association when hash is passed as subject" do it "checks permission through association when hash is passed as subject" do
@ -363,6 +372,15 @@ describe CanCan::Ability do
@ability.can?(:read, 123 => :books).should be_true @ability.can?(:read, 123 => :books).should be_true
end end
it "checks permissions on association hash with multiple rules" do
@ability.can :read, :books, :range => {:begin => 3}
@ability.can :read, :books, :range => {:end => 6}
@ability.can?(:read, (1..4) => :books).should be_false
@ability.can?(:read, (3..5) => :books).should be_true
@ability.can?(:read, (1..6) => :books).should be_true
@ability.can?(:read, 123 => :books).should be_true
end
it "checks ability on hash subclass" do it "checks ability on hash subclass" do
class Container < Hash; end class Container < Hash; end
@ability.can :read, :containers @ability.can :read, :containers
@ -509,4 +527,15 @@ describe CanCan::Ability do
# @ability.unauthorized_message(:update, ArgumentError).should == "update argument error" # @ability.unauthorized_message(:update, ArgumentError).should == "update argument error"
end end
end end
it "merges the rules from another ability" do
@ability.can :use, :tools
another_ability = Object.new
another_ability.extend(CanCan::Ability)
another_ability.can :use, :search
@ability.merge(another_ability)
@ability.can?(:use, :search).should be_true
@ability.send(:rules).size.should == 2
end
end end

View File

@ -32,7 +32,7 @@ describe CanCan::ControllerAdditions do
@controller.cannot?(:foo, :bar).should be_true @controller.cannot?(:foo, :bar).should be_true
end end
it "load_and_authorize_resource should setup a before filter which passes call to ControllerResource" do it "load_and_authorize_resource adds a before filter which passes call to ControllerResource" do
controller_resource = double("controller_resource") controller_resource = double("controller_resource")
controller_resource.should_receive(:process) controller_resource.should_receive(:process)
CanCan::ControllerResource.stub(:new).with(@controller, nil, :load => true, :authorize => true, :foo => :bar) { controller_resource } CanCan::ControllerResource.stub(:new).with(@controller, nil, :load => true, :authorize => true, :foo => :bar) { controller_resource }
@ -40,7 +40,7 @@ describe CanCan::ControllerAdditions do
@controller_class.load_and_authorize_resource :foo => :bar @controller_class.load_and_authorize_resource :foo => :bar
end end
it "load_and_authorize_resource should properly pass first argument as the resource name" do it "load_and_authorize_resource passes first argument as the resource name" do
controller_resource = double("controller_resource") controller_resource = double("controller_resource")
controller_resource.should_receive(:process) controller_resource.should_receive(:process)
CanCan::ControllerResource.stub(:new).with(@controller, :project, :load => true, :authorize => true, :foo => :bar) { controller_resource } CanCan::ControllerResource.stub(:new).with(@controller, :project, :load => true, :authorize => true, :foo => :bar) { controller_resource }
@ -48,7 +48,15 @@ describe CanCan::ControllerAdditions do
@controller_class.load_and_authorize_resource :project, :foo => :bar @controller_class.load_and_authorize_resource :project, :foo => :bar
end end
it "load_and_authorize_resource with :prepend should prepend the before filter" do it "load_and_authorize_resource passes :only, :except, :if, :unless options to before filter" do
controller_resource = double("controller_resource")
controller_resource.should_receive(:process)
CanCan::ControllerResource.stub(:new).with(@controller, nil, :load => true, :authorize => true) { controller_resource }
@controller_class.should_receive(:before_filter).with(:only => 1, :except => 2, :if => 3, :unless => 4).and_yield(@controller)
@controller_class.load_and_authorize_resource :only => 1, :except => 2, :if => 3, :unless => 4
end
it "load_and_authorize_resource with :prepend prepends the before filter" do
@controller_class.should_receive(:prepend_before_filter).with({}) @controller_class.should_receive(:prepend_before_filter).with({})
@controller_class.load_and_authorize_resource :foo => :bar, :prepend => true @controller_class.load_and_authorize_resource :foo => :bar, :prepend => true
end end

View File

@ -35,6 +35,26 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "attempts to load a resource with the same namespace as the controller when using :: for namespace" do
module SomeEngine
class Project < ::Project; end
end
project = SomeEngine::Project.create!
@params.merge!(:controller => "SomeEngine::ProjectsController", :action => "show", :id => project.id)
CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should == project
end
# Rails includes namespace in params, see issue #349
it "creates through the namespaced params" do
module SomeEngine
class Project < ::Project; end
end
@params.merge!(:controller => "SomeEngine::ProjectsController", :action => "create", :some_engine_project => {:name => "foobar"})
CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).name.should == "foobar"
end
it "loads resource for namespaced controller when using '::' for namespace" do it "loads resource for namespaced controller when using '::' for namespace" do
project = Project.create! project = Project.create!
@params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id) @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
@ -48,6 +68,15 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).name.should == "foobar" @controller.instance_variable_get(:@project).name.should == "foobar"
end end
it "builds a new resource for namespaced model with hash if params[:id] is not specified" do
module SomeEngine
class Project < ::Project; end
end
@params.merge!(:action => "create", :some_engine_project => {:name => "foobar"})
CanCan::ControllerResource.new(@controller, :load => true, :class => SomeEngine::Project).process
@controller.instance_variable_get(:@project).name.should == "foobar"
end
it "builds a new resource with attributes from current ability" do it "builds a new resource with attributes from current ability" do
@params.merge!(:action => "new") @params.merge!(:action => "new")
@ability.can(:create, :projects, :name => "from conditions") @ability.can(:create, :projects, :name => "from conditions")
@ -169,6 +198,11 @@ describe CanCan::ControllerResource do
resource.should_not be_parent resource.should_not be_parent
end end
it "has the specified resource_class if name is passed to load_resource" do
resource = CanCan::ControllerResource.new(@controller, :category)
resource.send(:resource_class).should == Category
end
it "loads parent resource through proper id parameter" do it "loads parent resource through proper id parameter" do
project = Project.create! project = Project.create!
@params.merge!(:action => "index", :project_id => project.id) @params.merge!(:action => "index", :project_id => project.id)
@ -226,12 +260,9 @@ describe CanCan::ControllerResource do
it "named resources should be loaded independently of the controller name" do it "named resources should be loaded independently of the controller name" do
category = Category.create! category = Category.create!
@params.merge!(:action => "new", :category_id => category.id) @params.merge!(:action => "new", :category_id => category.id)
CanCan::ControllerResource.new(@controller, :category, :load => true).process CanCan::ControllerResource.new(@controller, :category, :load => true).process
CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process
@controller.instance_variable_get(:@category).should eq(category) @controller.instance_variable_get(:@category).should eq(category)
project = @controller.instance_variable_get(:@project) project = @controller.instance_variable_get(:@project)
project.category.should eq(category) project.category.should eq(category)
end end
@ -239,10 +270,8 @@ describe CanCan::ControllerResource do
it "parent resources shouldn't be altered" do it "parent resources shouldn't be altered" do
category = Category.create! category = Category.create!
@params.merge!(:action => "create", :category_id => category.id, :project => { :name => 'foo' }) @params.merge!(:action => "create", :category_id => category.id, :project => { :name => 'foo' })
CanCan::ControllerResource.new(@controller, :category, :load => true).process CanCan::ControllerResource.new(@controller, :category, :load => true).process
CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process
project = @controller.instance_variable_get(:@project) project = @controller.instance_variable_get(:@project)
project.new_record?.should eq(true) project.new_record?.should eq(true)
project.name.should eq('foo') project.name.should eq('foo')
@ -329,6 +358,16 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "loads the model using a custom namespaced class" do
module SomeEngine
class Project < ::Project; end
end
project = SomeEngine::Project.create!
@params.merge!(:action => "show", :id => project.id)
CanCan::ControllerResource.new(@controller, :load => true, :class => SomeEngine::Project).process
@controller.instance_variable_get(:@project).should == project
end
it "does not authorize based on resource name if class is false because we don't do class level authorization anymore" do it "does not authorize based on resource name if class is false because we don't do class level authorization anymore" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
@controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized }

View File

@ -39,4 +39,20 @@ describe CanCan::InheritedResource do
CanCan::InheritedResource.new(@controller, :load => true).process CanCan::InheritedResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@projects).should == :projects @controller.instance_variable_get(:@projects).should == :projects
end end
it "should build a new resource with attributes from current ability" do
@params[:action] = "new"
@ability.can(:create, :projects, :name => "from conditions")
@controller.stub(:build_resource) { Struct.new(:name).new }
CanCan::InheritedResource.new(@controller, :load => true).process
@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, :projects, :name => "from conditions")
@controller.stub(:build_resource) { Struct.new(:name).new }
CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).name.should == "from params"
end
end end

View File

@ -228,6 +228,17 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
@ability.should_not be_able_to(:read, article2) @ability.should_not be_able_to(:read, article2)
end end
it "should merge MetaWhere and non-MetaWhere conditions" do
pending
@ability.can :read, Article, :priority.lt => 2
@ability.can :read, Article, :priority => 1
article1 = Article.create!(:priority => 1)
article2 = Article.create!(:priority => 3)
Article.accessible_by(@ability).should == [article1]
@ability.should be_able_to(:read, article1)
@ability.should_not be_able_to(:read, article2)
end
it "matches any MetaWhere condition" do it "matches any MetaWhere condition" do
pending pending
adapter = CanCan::ModelAdapters::ActiveRecordAdapter adapter = CanCan::ModelAdapters::ActiveRecordAdapter

View File

@ -71,6 +71,18 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
MongoidProject.accessible_by(@ability, :read).entries.should == [sir] MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
end end
it "returns the correct records when a mix of can and cannot rules in defined ability" do
pending "TODO figure out why this isn't working"
@ability.can :manage, :mongoid_projects, :title => 'Sir'
@ability.cannot :destroy, :mongoid_projects
sir = MongoidProject.create(:title => 'Sir')
lord = MongoidProject.create(:title => 'Lord')
dude = MongoidProject.create(:title => 'Dude')
MongoidProject.accessible_by(@ability, :destroy).entries.should == [sir]
end
it "is able to mix empty conditions and hashes" do it "is able to mix empty conditions and hashes" do
pending "TODO figure out why this isn't working" pending "TODO figure out why this isn't working"
@ability.can :read, :mongoid_projects @ability.can :read, :mongoid_projects

View File

@ -1,4 +1,5 @@
require "spec_helper" require "spec_helper"
require "ostruct" # for OpenStruct below
# Most of Rule functionality is tested in Ability specs # Most of Rule functionality is tested in Ability specs
describe CanCan::Rule do describe CanCan::Rule do
@ -45,4 +46,10 @@ describe CanCan::Rule do
CanCan::Rule.new(false, :read, :integers, :foo => :bar).specificity.should eq(4) CanCan::Rule.new(false, :read, :integers, :foo => :bar).specificity.should eq(4)
CanCan::Rule.new(false, :read, :integers, :foo).specificity.should eq(4) CanCan::Rule.new(false, :read, :integers, :foo).specificity.should eq(4)
end end
it "should not be mergeable if conditions are not simple hashes" do
meta_where = OpenStruct.new(:name => 'metawhere', :column => 'test')
@conditions[meta_where] = :bar
@rule.should be_unmergeable
end
end end