Skip to content
This repository
Browse code

bringing up to date with master branch

  • Loading branch information...
commit 6886aecb9ae45136fe1e080df8665b7d50afa581 2 parents de000fd + 1e89b31
Ryan Bates authored
1  .rbenv-version
... ... @@ -0,0 +1 @@
  1 +1.9.3-p194
90 CHANGELOG.rdoc
Source Rendered
... ... @@ -1,3 +1,48 @@
  1 +1.6.8 (June 25, 2012)
  2 +
  3 +* improved support for namespaced controllers and models
  4 +
  5 +* pass :if and :unless options for load and authorize resource (thanks mauriciozaffari)
  6 +
  7 +* Travis CI badge (thanks plentz)
  8 +
  9 +* adding Ability#merge for combining multiple abilities (thanks rogercampos)
  10 +
  11 +* support for multiple MetaWhere rules (thanks andhapp)
  12 +
  13 +* various fixes for DataMapper, Mongoid, and Inherited Resource integration
  14 +
  15 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.7...1.6.8]
  16 +
  17 +
  18 +1.6.7 (October 4, 2011)
  19 +
  20 +* fixing nested resource problem caused by namespace addition - issue #482
  21 +
  22 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.6...1.6.7]
  23 +
  24 +
  25 +1.6.6 (September 28, 2011)
  26 +
  27 +* correct "return cant jump across threads" error when using check_authorization (thanks codeprimate) - issues #463, #469
  28 +
  29 +* fixing tests in development by specifying with_model version (thanks kirkconnell) - issue #476
  30 +
  31 +* added travis.yml file for TravisCI support (thanks bai) - issue #427
  32 +
  33 +* better support for namespaced models (thanks whilefalse) - issues #424
  34 +
  35 +* adding :id_param option to load_and_authorize_resource (thanks skhisma) - issue #425
  36 +
  37 +* make default unauthorized message translatable text (thanks nhocki) - issue #409
  38 +
  39 +* improving DataMapper behavior (thanks psanford, maxsum-corin) - issue #410, #373
  40 +
  41 +* allow :find_by option to be full find method name - issue #335
  42 +
  43 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.5...1.6.6]
  44 +
  45 +
1 46 1.6.5 (May 18, 2011)
2 47
3 48 * pass action and subject through AccessDenied exception when :through isn't found - issue #366
@@ -14,12 +59,15 @@
14 59
15 60 * improve scope merging - issue #328
16 61
  62 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.4...1.6.5]
17 63
18 64
19 65 1.6.4 (March 29, 2011)
20 66
21 67 * Fixed mongoid 'or' error - see issue #322
22 68
  69 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.3...1.6.4]
  70 +
23 71
24 72 1.6.3 (March 25, 2011)
25 73
@@ -27,11 +75,15 @@
27 75
28 76 * Return subject passed to authorize! - see issue #314
29 77
  78 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.2...1.6.3]
  79 +
30 80
31 81 1.6.2 (March 18, 2011)
32 82
33 83 * Fixed instance loading when :singleton option is used - see issue #310
34 84
  85 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.1...1.6.2]
  86 +
35 87
36 88 1.6.1 (March 15, 2011)
37 89
@@ -41,6 +93,8 @@
41 93
42 94 * Reverted Inherited Resources "collection" override since it doesn't seem to be working - see issue #305
43 95
  96 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.6.0...1.6.1]
  97 +
44 98
45 99 1.6.0 (March 11, 2011)
46 100
@@ -62,6 +116,8 @@
62 116
63 117 * Raise an exception when trying to make a Ability condition with both a hash of conditions and a block - see issue #269
64 118
  119 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.5.1...1.6.0]
  120 +
65 121
66 122 1.5.1 (January 20, 2011)
67 123
@@ -69,6 +125,8 @@
69 125
70 126 * Improving Mongoid support for multiple can and cannot definitions (thanks stellard) - see issue #239
71 127
  128 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.5.0...1.5.1]
  129 +
72 130
73 131 1.5.0 (January 11, 2011)
74 132
@@ -90,6 +148,8 @@
90 148
91 149 * Internal: added .rvmrc to auto-switch to 1.8.7 with gemset - see issue #231
92 150
  151 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.4.1...1.5.0]
  152 +
93 153
94 154 1.4.1 (November 12, 2010)
95 155
@@ -103,6 +163,8 @@
103 163
104 164 * Fix odd behavior when "cache_classes = false" (thanks mphalliday) - see issue #174
105 165
  166 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.4.0...1.4.1]
  167 +
106 168
107 169 1.4.0 (October 5, 2010)
108 170
@@ -142,11 +204,15 @@
142 204
143 205 * No longer calling block in +can+ definition when checking on class - see issue #116
144 206
  207 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.4...1.4.0]
  208 +
145 209
146 210 1.3.4 (August 31, 2010)
147 211
148 212 * Don't stop at +cannot+ with hash conditions when checking class (thanks tamoya) - see issue #131
149 213
  214 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.3...1.3.4]
  215 +
150 216
151 217 1.3.3 (August 20, 2010)
152 218
@@ -154,16 +220,22 @@
154 220
155 221 * Pluralize nested associations for conditions in accessible_by (thanks mlooney) - see issue #123
156 222
  223 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.2...1.3.3]
  224 +
157 225
158 226 1.3.2 (August 7, 2010)
159 227
160 228 * Fixing slice error when passing in custom resource name - see issue #112
161 229
  230 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.1...1.3.2]
  231 +
162 232
163 233 1.3.1 (August 6, 2010)
164 234
165 235 * Fixing protected sanitize_sql error - see issue #111
166 236
  237 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.3.0...1.3.1]
  238 +
167 239
168 240 1.3.0 (August 6, 2010)
169 241
@@ -191,6 +263,8 @@
191 263
192 264 * Supporting deeply nested aliases - see issue #98
193 265
  266 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.2.0...1.3.0]
  267 +
194 268
195 269 1.2.0 (July 16, 2010)
196 270
@@ -206,11 +280,15 @@
206 280
207 281 * Adding joins clause to accessible_by when conditions are across associations
208 282
  283 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.1.1...1.2.0]
  284 +
209 285
210 286 1.1.1 (April 17, 2010)
211 287
212 288 * Fixing behavior in Rails 3 by properly initializing ResourceAuthorization
213 289
  290 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.1...1.1.1]
  291 +
214 292
215 293 1.1.0 (April 17, 2010)
216 294
@@ -234,6 +312,8 @@
234 312
235 313 * Support additional arguments to can? which get passed to the block - see issue #48
236 314
  315 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.0.2...1.1]
  316 +
237 317
238 318 1.0.2 (Dec 30, 2009)
239 319
@@ -243,6 +323,8 @@
243 323
244 324 * Adding custom message argument to unauthorized! method (thanks tjwallace) - see issue #18
245 325
  326 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.0.1...1.0.2]
  327 +
246 328
247 329 1.0.1 (Dec 14, 2009)
248 330
@@ -250,6 +332,8 @@
250 332
251 333 * Don't fetch parent of nested resource if *_id parameter is missing so it works with shallow nested routes - see issue #14
252 334
  335 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/1.0.0...1.0.1]
  336 +
253 337
254 338 1.0.0 (Dec 13, 2009)
255 339
@@ -265,6 +349,8 @@
265 349
266 350 * BACKWARDS INCOMPATIBLE: turning load and authorize resource methods into class methods which set up the before filter so they can accept additional arguments.
267 351
  352 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/0.2.1...1.0.0]
  353 +
268 354
269 355 0.2.1 (Nov 26, 2009)
270 356
@@ -274,6 +360,8 @@
274 360
275 361 * support custom objects (usually symbols) in can definition - see issue #8
276 362
  363 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/0.2.0...0.2.1]
  364 +
277 365
278 366 0.2.0 (Nov 17, 2009)
279 367
@@ -285,6 +373,8 @@
285 373
286 374 * BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' to set up abilities - see issue #4
287 375
  376 +* {see the full list of changes}[https://github.com/ryanb/cancan/compare/0.1.0...0.2.0]
  377 +
288 378
289 379 0.1.0 (Nov 16, 2009)
290 380
2  README.rdoc
Source Rendered
... ... @@ -1,4 +1,4 @@
1   -= CanCan
  1 += CanCan {<img src="https://secure.travis-ci.org/ryanb/cancan.png" />}[http://travis-ci.org/ryanb/cancan]
2 2
3 3 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]
4 4
7 lib/cancan/ability.rb
@@ -254,6 +254,13 @@ def fully_authorized!(action, subject)
254 254 @fully_authorized << [action.to_sym, subject.to_sym]
255 255 end
256 256
  257 + def merge(ability)
  258 + ability.send(:rules).each do |rule|
  259 + rules << rule.dup
  260 + end
  261 + self
  262 + end
  263 +
257 264 private
258 265
259 266 def unauthorized_message_keys(action, subject)
7 lib/cancan/controller_additions.rb
@@ -94,7 +94,7 @@ def load_and_authorize_resource(*args)
94 94 # [:+find_by+]
95 95 # Find using a different attribute other than id. For example.
96 96 #
97   - # load_resource :find_by => :permalink # will use find_by_permlink!(params[:id])
  97 + # load_resource :find_by => :permalink # will use find_by_permalink!(params[:id])
98 98 #
99 99 # [:+collection+]
100 100 # Specify which actions are resource collection actions in addition to :+index+. This
@@ -152,6 +152,9 @@ def load_resource(*args)
152 152 # [:+except+]
153 153 # Does not apply before filter to given actions.
154 154 #
  155 + # [:+singleton+]
  156 + # Pass +true+ if this is a singleton resource through a +has_one+ association.
  157 + #
155 158 # [:+parent+]
156 159 # True or false depending on if the resource is considered a parent resource. This defaults to +true+ if a resource
157 160 # name is given which does not match the controller.
@@ -382,7 +385,7 @@ def cannot?(*args)
382 385 end
383 386 end
384 387
385   -if defined? ActionController
  388 +if defined? ActionController::Base
386 389 ActionController::Base.class_eval do
387 390 include CanCan::ControllerAdditions
388 391 end
21 lib/cancan/controller_resource.rb
@@ -6,8 +6,8 @@ def self.add_before_filter(controller_class, behavior, *args)
6 6 options = args.extract_options!.merge(behavior)
7 7 resource_name = args.first
8 8 before_filter_method = options.delete(:prepend) ? :prepend_before_filter : :before_filter
9   - controller_class.send(before_filter_method, options.slice(:only, :except)) do |controller|
10   - controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except)).process
  9 + controller_class.send(before_filter_method, options.slice(:only, :except, :if, :unless)) do |controller|
  10 + controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except, :if, :unless)).process
11 11 end
12 12 end
13 13
@@ -81,6 +81,10 @@ def load_collection
81 81
82 82 def build_resource
83 83 resource = resource_base.new(resource_params || {})
  84 + assign_attributes(resource)
  85 + end
  86 +
  87 + def assign_attributes(resource)
84 88 resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource
85 89 initial_attributes.each do |attr_name, value|
86 90 resource.send("#{attr_name}=", value)
@@ -225,12 +229,19 @@ def name
225 229 end
226 230
227 231 def resource_params
228   - # since Rails includes the namespace in the params sent by the form (issue #349)
229   - @params[namespaced_name.to_s.underscore.gsub("/", "_")]
  232 + if @options[:class]
  233 + @params[@options[:class].to_s.underscore.gsub('/', '_')]
  234 + else
  235 + @params[namespaced_name.to_s.underscore.gsub("/", "_")]
  236 + end
  237 + end
  238 +
  239 + def namespace
  240 + @params[:controller].split("::")[0..-2]
230 241 end
231 242
232 243 def namespaced_name
233   - (@name || @params[:controller].sub("Controller", "")).singularize.camelize.constantize
  244 + [namespace, name.camelize].join('::').singularize.camelize.constantize
234 245 rescue NameError
235 246 name
236 247 end
3  lib/cancan/inherited_resource.rb
@@ -6,7 +6,8 @@ def load_resource_instance
6 6 @controller.send :association_chain
7 7 @controller.instance_variable_get("@#{instance_name}")
8 8 elsif new_actions.include? @params[:action].to_sym
9   - @controller.send :build_resource
  9 + resource = @controller.send :build_resource
  10 + assign_attributes(resource)
10 11 else
11 12 @controller.send :resource
12 13 end
7 lib/cancan/model_adapters/active_record_adapter.rb
@@ -89,7 +89,12 @@ def database_records
89 89 if override_scope
90 90 @model_class.scoped.merge(override_scope)
91 91 elsif @model_class.respond_to?(:where) && @model_class.respond_to?(:joins)
92   - @model_class.where(conditions).joins(joins)
  92 + mergeable_conditions = @rules.select {|rule| rule.unmergeable? }.blank?
  93 + if mergeable_conditions
  94 + @model_class.where(conditions).joins(joins)
  95 + else
  96 + @model_class.where(*(@rules.map(&:conditions))).joins(joins)
  97 + end
93 98 else
94 99 @model_class.scoped(:conditions => conditions, :joins => joins)
95 100 end
3  lib/cancan/model_adapters/mongoid_adapter.rb
@@ -30,8 +30,9 @@ def database_records
30 30 else
31 31 # we only need to process can rules if
32 32 # there are no rules with empty conditions
33   - rules = @rules.reject { |rule| rule.conditions.empty? }
  33 + rules = @rules.reject { |rule| rule.conditions.empty? && rule.base_behavior }
34 34 process_can_rules = @rules.count == rules.count
  35 +
35 36 rules.inject(@model_class.all) do |records, rule|
36 37 if process_can_rules && rule.base_behavior
37 38 records.or rule.conditions
10 lib/cancan/rule.rb
@@ -63,6 +63,10 @@ def instance_conditions?
63 63 @block || conditions?
64 64 end
65 65
  66 + def unmergeable?
  67 + @conditions.respond_to?(:keys) && (! @conditions.keys.first.kind_of? Symbol)
  68 + end
  69 +
66 70 def associations_hash(conditions = @conditions)
67 71 hash = {}
68 72 conditions.map do |name, value|
@@ -139,7 +143,7 @@ def matches_conditions_hash?(subject, conditions = @conditions)
139 143 else
140 144 attribute && matches_conditions_hash?(attribute, value)
141 145 end
142   - elsif value.kind_of?(Array) || value.kind_of?(Range)
  146 + elsif value.kind_of?(Enumerable)
143 147 value.include? attribute
144 148 else
145 149 attribute == value
@@ -151,7 +155,7 @@ def matches_conditions_hash?(subject, conditions = @conditions)
151 155 end
152 156
153 157 def nested_subject_matches_conditions?(subject_hash)
154   - parent, child = subject_hash.shift
  158 + parent, child = subject_hash.first
155 159 matches_conditions_hash?(parent, @conditions[parent.class.name.downcase.to_sym] || {})
156 160 end
157 161
@@ -168,7 +172,7 @@ def subject_name(subject)
168 172 end
169 173
170 174 def model_adapter(subject)
171   - ModelAdapters::AbstractAdapter.adapter_class(subject_object?(subject) ? subject.class : subject)
  175 + CanCan::ModelAdapters::AbstractAdapter.adapter_class(subject_object?(subject) ? subject.class : subject)
172 176 end
173 177 end
174 178 end
31 spec/cancan/ability_spec.rb
@@ -290,6 +290,16 @@
290 290 @ability.should be_fully_authorized(:update, :ranges)
291 291 end
292 292
  293 + it "should accept a set as a condition value" do
  294 + object_with_foo_2 = Object.new
  295 + object_with_foo_2.should_receive(:foo) { 2 }
  296 + object_with_foo_3 = Object.new
  297 + object_with_foo_3.should_receive(:foo) { 3 }
  298 + @ability.can :read, :objects, :foo => [1, 2, 5].to_set
  299 + @ability.can?(:read, object_with_foo_2).should be_true
  300 + @ability.can?(:read, object_with_foo_3).should be_false
  301 + end
  302 +
293 303 it "does not match subjects return nil for methods that must match nested a nested conditions hash" do
294 304 object_with_foo = Object.new
295 305 object_with_foo.should_receive(:foo) { :bar }
@@ -353,7 +363,6 @@
353 363 @ability.can?(:update, :books, :author).should be_false
354 364 end
355 365
356   -
357 366 # Hash Association
358 367
359 368 it "checks permission through association when hash is passed as subject" do
@@ -363,6 +372,15 @@
363 372 @ability.can?(:read, 123 => :books).should be_true
364 373 end
365 374
  375 + it "checks permissions on association hash with multiple rules" do
  376 + @ability.can :read, :books, :range => {:begin => 3}
  377 + @ability.can :read, :books, :range => {:end => 6}
  378 + @ability.can?(:read, (1..4) => :books).should be_false
  379 + @ability.can?(:read, (3..5) => :books).should be_true
  380 + @ability.can?(:read, (1..6) => :books).should be_true
  381 + @ability.can?(:read, 123 => :books).should be_true
  382 + end
  383 +
366 384 it "checks ability on hash subclass" do
367 385 class Container < Hash; end
368 386 @ability.can :read, :containers
@@ -509,4 +527,15 @@ class Container < Hash; end
509 527 # @ability.unauthorized_message(:update, ArgumentError).should == "update argument error"
510 528 end
511 529 end
  530 +
  531 + it "merges the rules from another ability" do
  532 + @ability.can :use, :tools
  533 + another_ability = Object.new
  534 + another_ability.extend(CanCan::Ability)
  535 + another_ability.can :use, :search
  536 +
  537 + @ability.merge(another_ability)
  538 + @ability.can?(:use, :search).should be_true
  539 + @ability.send(:rules).size.should == 2
  540 + end
512 541 end
14 spec/cancan/controller_additions_spec.rb
@@ -32,7 +32,7 @@
32 32 @controller.cannot?(:foo, :bar).should be_true
33 33 end
34 34
35   - it "load_and_authorize_resource should setup a before filter which passes call to ControllerResource" do
  35 + it "load_and_authorize_resource adds a before filter which passes call to ControllerResource" do
36 36 controller_resource = double("controller_resource")
37 37 controller_resource.should_receive(:process)
38 38 CanCan::ControllerResource.stub(:new).with(@controller, nil, :load => true, :authorize => true, :foo => :bar) { controller_resource }
@@ -40,7 +40,7 @@
40 40 @controller_class.load_and_authorize_resource :foo => :bar
41 41 end
42 42
43   - it "load_and_authorize_resource should properly pass first argument as the resource name" do
  43 + it "load_and_authorize_resource passes first argument as the resource name" do
44 44 controller_resource = double("controller_resource")
45 45 controller_resource.should_receive(:process)
46 46 CanCan::ControllerResource.stub(:new).with(@controller, :project, :load => true, :authorize => true, :foo => :bar) { controller_resource }
@@ -48,7 +48,15 @@
48 48 @controller_class.load_and_authorize_resource :project, :foo => :bar
49 49 end
50 50
51   - it "load_and_authorize_resource with :prepend should prepend the before filter" do
  51 + it "load_and_authorize_resource passes :only, :except, :if, :unless options to before filter" do
  52 + controller_resource = double("controller_resource")
  53 + controller_resource.should_receive(:process)
  54 + CanCan::ControllerResource.stub(:new).with(@controller, nil, :load => true, :authorize => true) { controller_resource }
  55 + @controller_class.should_receive(:before_filter).with(:only => 1, :except => 2, :if => 3, :unless => 4).and_yield(@controller)
  56 + @controller_class.load_and_authorize_resource :only => 1, :except => 2, :if => 3, :unless => 4
  57 + end
  58 +
  59 + it "load_and_authorize_resource with :prepend prepends the before filter" do
52 60 @controller_class.should_receive(:prepend_before_filter).with({})
53 61 @controller_class.load_and_authorize_resource :foo => :bar, :prepend => true
54 62 end
51 spec/cancan/controller_resource_spec.rb
@@ -35,6 +35,26 @@
35 35 @controller.instance_variable_get(:@project).should == project
36 36 end
37 37
  38 + it "attempts to load a resource with the same namespace as the controller when using :: for namespace" do
  39 + module SomeEngine
  40 + class Project < ::Project; end
  41 + end
  42 + project = SomeEngine::Project.create!
  43 + @params.merge!(:controller => "SomeEngine::ProjectsController", :action => "show", :id => project.id)
  44 + CanCan::ControllerResource.new(@controller, :load => true).process
  45 + @controller.instance_variable_get(:@project).should == project
  46 + end
  47 +
  48 + # Rails includes namespace in params, see issue #349
  49 + it "creates through the namespaced params" do
  50 + module SomeEngine
  51 + class Project < ::Project; end
  52 + end
  53 + @params.merge!(:controller => "SomeEngine::ProjectsController", :action => "create", :some_engine_project => {:name => "foobar"})
  54 + CanCan::ControllerResource.new(@controller, :load => true).process
  55 + @controller.instance_variable_get(:@project).name.should == "foobar"
  56 + end
  57 +
38 58 it "loads resource for namespaced controller when using '::' for namespace" do
39 59 project = Project.create!
40 60 @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
@@ -48,6 +68,15 @@
48 68 @controller.instance_variable_get(:@project).name.should == "foobar"
49 69 end
50 70
  71 + it "builds a new resource for namespaced model with hash if params[:id] is not specified" do
  72 + module SomeEngine
  73 + class Project < ::Project; end
  74 + end
  75 + @params.merge!(:action => "create", :some_engine_project => {:name => "foobar"})
  76 + CanCan::ControllerResource.new(@controller, :load => true, :class => SomeEngine::Project).process
  77 + @controller.instance_variable_get(:@project).name.should == "foobar"
  78 + end
  79 +
51 80 it "builds a new resource with attributes from current ability" do
52 81 @params.merge!(:action => "new")
53 82 @ability.can(:create, :projects, :name => "from conditions")
@@ -169,6 +198,11 @@ class CustomModel
169 198 resource.should_not be_parent
170 199 end
171 200
  201 + it "has the specified resource_class if name is passed to load_resource" do
  202 + resource = CanCan::ControllerResource.new(@controller, :category)
  203 + resource.send(:resource_class).should == Category
  204 + end
  205 +
172 206 it "loads parent resource through proper id parameter" do
173 207 project = Project.create!
174 208 @params.merge!(:action => "index", :project_id => project.id)
@@ -226,23 +260,18 @@ class CustomModel
226 260 it "named resources should be loaded independently of the controller name" do
227 261 category = Category.create!
228 262 @params.merge!(:action => "new", :category_id => category.id)
229   -
230 263 CanCan::ControllerResource.new(@controller, :category, :load => true).process
231 264 CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process
232   -
233 265 @controller.instance_variable_get(:@category).should eq(category)
234   -
235 266 project = @controller.instance_variable_get(:@project)
236 267 project.category.should eq(category)
237 268 end
238   -
  269 +
239 270 it "parent resources shouldn't be altered" do
240 271 category = Category.create!
241 272 @params.merge!(:action => "create", :category_id => category.id, :project => { :name => 'foo' })
242   -
243 273 CanCan::ControllerResource.new(@controller, :category, :load => true).process
244 274 CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process
245   -
246 275 project = @controller.instance_variable_get(:@project)
247 276 project.new_record?.should eq(true)
248 277 project.name.should eq('foo')
@@ -329,6 +358,16 @@ class CustomModel
329 358 @controller.instance_variable_get(:@project).should == project
330 359 end
331 360
  361 + it "loads the model using a custom namespaced class" do
  362 + module SomeEngine
  363 + class Project < ::Project; end
  364 + end
  365 + project = SomeEngine::Project.create!
  366 + @params.merge!(:action => "show", :id => project.id)
  367 + CanCan::ControllerResource.new(@controller, :load => true, :class => SomeEngine::Project).process
  368 + @controller.instance_variable_get(:@project).should == project
  369 + end
  370 +
332 371 it "does not authorize based on resource name if class is false because we don't do class level authorization anymore" do
333 372 @params.merge!(:action => "show", :id => 123)
334 373 @controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized }
16 spec/cancan/inherited_resource_spec.rb
@@ -39,4 +39,20 @@
39 39 CanCan::InheritedResource.new(@controller, :load => true).process
40 40 @controller.instance_variable_get(:@projects).should == :projects
41 41 end
  42 +
  43 + it "should build a new resource with attributes from current ability" do
  44 + @params[:action] = "new"
  45 + @ability.can(:create, :projects, :name => "from conditions")
  46 + @controller.stub(:build_resource) { Struct.new(:name).new }
  47 + CanCan::InheritedResource.new(@controller, :load => true).process
  48 + @controller.instance_variable_get(:@project).name.should == "from conditions"
  49 + end
  50 +
  51 + it "should override initial attributes with params" do
  52 + @params.merge!(:action => "new", :project => {:name => "from params"})
  53 + @ability.can(:create, :projects, :name => "from conditions")
  54 + @controller.stub(:build_resource) { Struct.new(:name).new }
  55 + CanCan::ControllerResource.new(@controller, :load => true).process
  56 + @controller.instance_variable_get(:@project).name.should == "from params"
  57 + end
42 58 end
11 spec/cancan/model_adapters/active_record_adapter_spec.rb
@@ -228,6 +228,17 @@ class Comment < ActiveRecord::Base
228 228 @ability.should_not be_able_to(:read, article2)
229 229 end
230 230
  231 + it "should merge MetaWhere and non-MetaWhere conditions" do
  232 + pending
  233 + @ability.can :read, Article, :priority.lt => 2
  234 + @ability.can :read, Article, :priority => 1
  235 + article1 = Article.create!(:priority => 1)
  236 + article2 = Article.create!(:priority => 3)
  237 + Article.accessible_by(@ability).should == [article1]
  238 + @ability.should be_able_to(:read, article1)
  239 + @ability.should_not be_able_to(:read, article2)
  240 + end
  241 +
231 242 it "matches any MetaWhere condition" do
232 243 pending
233 244 adapter = CanCan::ModelAdapters::ActiveRecordAdapter
12 spec/cancan/model_adapters/mongoid_adapter_spec.rb
@@ -71,6 +71,18 @@ class MongoidProject
71 71 MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
72 72 end
73 73
  74 + it "returns the correct records when a mix of can and cannot rules in defined ability" do
  75 + pending "TODO figure out why this isn't working"
  76 + @ability.can :manage, :mongoid_projects, :title => 'Sir'
  77 + @ability.cannot :destroy, :mongoid_projects
  78 +
  79 + sir = MongoidProject.create(:title => 'Sir')
  80 + lord = MongoidProject.create(:title => 'Lord')
  81 + dude = MongoidProject.create(:title => 'Dude')
  82 +
  83 + MongoidProject.accessible_by(@ability, :destroy).entries.should == [sir]
  84 + end
  85 +
74 86 it "is able to mix empty conditions and hashes" do
75 87 pending "TODO figure out why this isn't working"
76 88 @ability.can :read, :mongoid_projects
7 spec/cancan/rule_spec.rb
... ... @@ -1,4 +1,5 @@
1 1 require "spec_helper"
  2 +require "ostruct" # for OpenStruct below
2 3
3 4 # Most of Rule functionality is tested in Ability specs
4 5 describe CanCan::Rule do
@@ -45,4 +46,10 @@
45 46 CanCan::Rule.new(false, :read, :integers, :foo => :bar).specificity.should eq(4)
46 47 CanCan::Rule.new(false, :read, :integers, :foo).specificity.should eq(4)
47 48 end
  49 +
  50 + it "should not be mergeable if conditions are not simple hashes" do
  51 + meta_where = OpenStruct.new(:name => 'metawhere', :column => 'test')
  52 + @conditions[meta_where] = :bar
  53 + @rule.should be_unmergeable
  54 + end
48 55 end

0 comments on commit 6886aec

Please sign in to comment.
Something went wrong with that request. Please try again.