cancancan 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7079b16052c2f98ebe67d6ce08e390188bca856d
4
- data.tar.gz: faa43141ca3124dbe3d40ceb30a76ca78bc56117
3
+ metadata.gz: 59fd5f683924f32658036d91ec731a03dc2620f0
4
+ data.tar.gz: 80ada1d8d82e08dae4f39afa0f4fc6e0bba1a43b
5
5
  SHA512:
6
- metadata.gz: 8168024035574501d8bef61033f458facb1b7173ed98d901b9b51aedc0c659470f6210beb308273de9cd437e4d75027eb39321f104a9e8dc8f933593f1a67aa1
7
- data.tar.gz: e71e7aa4f2a66f0f660aa9ce224dff3c0de2efc3b0fb85d461113175e5349700bf4475f5f9ca7abf16c56457488a33b3b5d954e753b021f162428c5827e28cd5
6
+ metadata.gz: f7854cb0fd225fc65ca36e2ae8b3d257e2509ebc092801a1802686bd2537dcb3c53f0e522f3d9a43b15ded9765a026c9d11c3588f4a4ff28bd752606fa01788c
7
+ data.tar.gz: e98f2b6d8defb75df838a82a5c0a28befdec610db7be82f119e70a922d953963fc7c268d014668ccf28c5fd54ccabbda9ca8d8bba4e175b587a36093f4da76b4
data/.travis.yml CHANGED
@@ -11,6 +11,8 @@ gemfile:
11
11
  - gemfiles/activerecord_3.0.gemfile
12
12
  - gemfiles/activerecord_3.1.gemfile
13
13
  - gemfiles/activerecord_3.2.gemfile
14
+ - gemfiles/activerecord_4.0.gemfile
15
+ - gemfiles/activerecord_4.1.gemfile
14
16
  - gemfiles/datamapper_1.x.gemfile
15
17
  - gemfiles/mongoid_2.x.gemfile
16
18
  - gemfiles/sequel_3.x.gemfile
@@ -19,6 +21,19 @@ services:
19
21
  matrix:
20
22
  allow_failures:
21
23
  - rvm: rbx
24
+ exclude:
25
+ - rvm: 1.8.7
26
+ gemfile: gemfiles/activerecord_4.0.gemfile
27
+ - rvm: 1.8.7
28
+ gemfile: gemfiles/activerecord_4.1.gemfile
29
+ - rvm: 1.9.2
30
+ gemfile: gemfiles/activerecord_4.0.gemfile
31
+ - rvm: 1.9.2
32
+ gemfile: gemfiles/activerecord_4.1.gemfile
33
+ - rvm: ree
34
+ gemfile: gemfiles/activerecord_4.0.gemfile
35
+ - rvm: ree
36
+ gemfile: gemfiles/activerecord_4.1.gemfile
22
37
  notifications:
23
38
  recipients:
24
39
  - bryan@bryanrite.com
data/Appraisals CHANGED
@@ -39,6 +39,34 @@ appraise "activerecord_3.2" do
39
39
  end
40
40
  end
41
41
 
42
+ appraise "activerecord_4.0" do
43
+ gem "activerecord", "~> 4.0.5", :require => "active_record"
44
+ gem 'activesupport', '~> 4.0.5', :require => 'active_support/all'
45
+
46
+ gemfile.platforms :jruby do
47
+ gem "activerecord-jdbcsqlite3-adapter"
48
+ gem "jdbc-sqlite3"
49
+ end
50
+
51
+ gemfile.platforms :ruby, :mswin, :mingw do
52
+ gem "sqlite3"
53
+ end
54
+ end
55
+
56
+ appraise "activerecord_4.1" do
57
+ gem "activerecord", "~> 4.1.1", :require => "active_record"
58
+ gem 'activesupport', '~> 4.1.1', :require => 'active_support/all'
59
+
60
+ gemfile.platforms :jruby do
61
+ gem "activerecord-jdbcsqlite3-adapter"
62
+ gem "jdbc-sqlite3"
63
+ end
64
+
65
+ gemfile.platforms :ruby, :mswin, :mingw do
66
+ gem "sqlite3"
67
+ end
68
+ end
69
+
42
70
  appraise "datamapper_1.x" do
43
71
  gem 'activesupport', '~> 3.0', :require => 'active_support/all'
44
72
  gem "dm-core", "~> 1.0.2"
@@ -64,7 +92,6 @@ appraise "sequel_3.x" do
64
92
  gem 'activesupport', '~> 3.0', :require => 'active_support/all'
65
93
 
66
94
  gemfile.platforms :jruby do
67
- gem "activerecord-jdbcsqlite3-adapter"
68
95
  gem "jdbc-sqlite3"
69
96
  end
70
97
 
data/CHANGELOG.rdoc CHANGED
@@ -1,6 +1,35 @@
1
1
  Develop
2
2
 
3
3
 
4
+ 1.9.0 (July 20th, 2014)
5
+
6
+ * Fix cancancan#59 - Parameters are automatically detected and santitized for all actions, not just create and update. (bryanrite)
7
+
8
+ * Fix cancancan#97, 72, 40, 39, 26 - Support Active Record 4 properly with references on nested permissions. (scpike, tdg5, Crystark)
9
+
10
+
11
+ 1.8.4 (June 24th, 2014)
12
+
13
+ * Fix cancancan#86 - Fixes previous RSpec 3 update as there was a bug in the fix for RSpec 2.99. (bryanrite)
14
+
15
+
16
+ 1.8.3 (June 24th, 2014)
17
+
18
+ * Fix cancancan#85 - Remove deprecation notices for RSpec 3 and continue backwards compatibility. (andypike, bryanrite, porteta)
19
+
20
+
21
+ 1.8.2 (June 5th, 2014)
22
+
23
+ * Fix cancancan#75 - More specific hash-like object check. (bryanrite)
24
+
25
+
26
+ 1.8.1 (May 27th, 2014)
27
+
28
+ * Fix cancancan#67 - Sequel tests are run properly for JRuby. (bryanrite)
29
+
30
+ * Fix cancancan#68 - Checks for hash-like objects in subject better. (bryanrite)
31
+
32
+
4
33
  1.8.0 (May 8th, 2014)
5
34
 
6
35
  * Feature cancan#884 - Add a Sequel model adapter (szetobo)
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
  {<img src="https://badge.fury.io/rb/cancancan.png" alt="Gem Version" />}[http://badge.fury.io/rb/cancancan]
3
3
  {<img src="https://travis-ci.org/CanCanCommunity/cancancan.png?branch=master" alt="Build Status" />}[https://travis-ci.org/CanCanCommunity/cancancan]
4
4
  {<img src="https://codeclimate.com/github/CanCanCommunity/cancancan.png" />}[https://codeclimate.com/github/CanCanCommunity/cancancan]
5
- {<img src="http://inch-pages.github.io/github/CanCanCommunity/cancancan.png" alt="Inline docs" />}[http://inch-pages.github.io/github/CanCanCommunity/cancancan]
5
+ {<img src="http://inch-ci.org/github/CanCanCommunity/cancancan.png" alt="Inline docs" />}[http://inch-ci.org/github/CanCanCommunity/cancancan]
6
6
 
7
7
  Wiki[https://github.com/bryanrite/cancancan/wiki] | RDocs[http://rdoc.info/projects/ryanb/cancan] | Screencast[http://railscasts.com/episodes/192-authorization-with-cancan]
8
8
 
@@ -22,7 +22,7 @@ Any help is greatly appreciated, feel free to submit pull-requests or open issue
22
22
 
23
23
  In <b>Rails 3 and 4</b>, add this to your Gemfile and run the +bundle+ command.
24
24
 
25
- gem 'cancancan', '~> 1.8'
25
+ gem 'cancancan', '~> 1.9'
26
26
 
27
27
  In <b>Rails 2</b>, add this to your environment.rb file.
28
28
 
@@ -173,7 +173,7 @@ Cancancan uses {appraisals}[https://github.com/thoughtbot/appraisal] to test the
173
173
 
174
174
  When first developing, you may need to run <tt>bundle install</tt> and then <tt>appraisal install</tt>, to install the different sets.
175
175
 
176
- You can then run all appraisal files (like CI does), with <tt>appraisal rake</tt> or just run a specific set <tt>appraisal rails_3.0 rake</tt>.
176
+ You can then run all appraisal files (like CI does), with <tt>appraisal rake</tt> or just run a specific set <tt>appraisal activerecord_3.0 rake</tt>.
177
177
 
178
178
  See the {CONTRIBUTING}[https://github.com/CanCanCommunity/cancancan/blob/develop/CONTRIBUTING.md] and {spec/README}[https://github.com/bryanrite/cancancan/blob/master/spec/README.rdoc] for more information.
179
179
 
data/cancancan.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
24
24
 
25
25
  s.add_development_dependency 'bundler', '~> 1.3'
26
26
  s.add_development_dependency 'rake', '~> 10.1.1'
27
- s.add_development_dependency 'rspec', '~> 2.14'
27
+ s.add_development_dependency 'rspec', '~> 3.0.0'
28
28
  s.add_development_dependency 'appraisal', '>= 1.0.0'
29
29
 
30
30
  s.rubyforge_project = s.name
@@ -0,0 +1,17 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.0.5", :require => "active_record"
6
+ gem "activesupport", "~> 4.0.5", :require => "active_support/all"
7
+
8
+ platforms :jruby do
9
+ gem "activerecord-jdbcsqlite3-adapter"
10
+ gem "jdbc-sqlite3"
11
+ end
12
+
13
+ platforms :ruby, :mswin, :mingw do
14
+ gem "sqlite3"
15
+ end
16
+
17
+ gemspec :path => "../"
@@ -0,0 +1,17 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.1.1", :require => "active_record"
6
+ gem "activesupport", "~> 4.1.1", :require => "active_support/all"
7
+
8
+ platforms :jruby do
9
+ gem "activerecord-jdbcsqlite3-adapter"
10
+ gem "jdbc-sqlite3"
11
+ end
12
+
13
+ platforms :ruby, :mswin, :mingw do
14
+ gem "sqlite3"
15
+ end
16
+
17
+ gemspec :path => "../"
@@ -6,7 +6,6 @@ gem "sequel", "~> 3.47.0"
6
6
  gem "activesupport", "~> 3.0", :require => "active_support/all"
7
7
 
8
8
  platforms :jruby do
9
- gem "activerecord-jdbcsqlite3-adapter"
10
9
  gem "jdbc-sqlite3"
11
10
  end
12
11
 
@@ -285,7 +285,7 @@ module CanCan
285
285
 
286
286
  # It translates to an array the subject or the hash with multiple subjects given to can?.
287
287
  def extract_subjects(subject)
288
- subject = if subject.respond_to?(:keys) && subject.key?(:any)
288
+ subject = if subject.kind_of?(Hash) && subject.key?(:any)
289
289
  subject[:any]
290
290
  else
291
291
  [subject]
@@ -294,7 +294,7 @@ module CanCan
294
294
 
295
295
  def self.included(base)
296
296
  base.extend ClassMethods
297
- base.helper_method :can?, :cannot?, :current_ability
297
+ base.helper_method :can?, :cannot?, :current_ability if base.respond_to? :helper_method
298
298
  end
299
299
 
300
300
  # Raises a CanCan::AccessDenied exception if the current_ability cannot
@@ -220,22 +220,29 @@ module CanCan
220
220
  end
221
221
 
222
222
  def resource_params
223
- if param_actions.include?(@params[:action].to_sym) && params_method.present?
223
+ if parameters_require_sanitizing? && params_method.present?
224
224
  return case params_method
225
225
  when Symbol then @controller.send(params_method)
226
226
  when String then @controller.instance_eval(params_method)
227
227
  when Proc then params_method.call(@controller)
228
228
  end
229
- elsif @options[:class]
230
- params_key = extract_key(@options[:class])
231
- return @params[params_key] if @params[params_key]
229
+ else
230
+ resource_params_by_namespaced_name
232
231
  end
232
+ end
233
233
 
234
- resource_params_by_namespaced_name
234
+ def parameters_require_sanitizing?
235
+ save_actions.include?(@params[:action].to_sym) || resource_params_by_namespaced_name.present?
235
236
  end
236
237
 
237
238
  def resource_params_by_namespaced_name
238
- @params[extract_key(namespaced_name)]
239
+ if @options[:instance_name] && @params.has_key?(extract_key(@options[:instance_name]))
240
+ @params[extract_key(@options[:instance_name])]
241
+ elsif @options[:class] && @params.has_key?(extract_key(@options[:class]))
242
+ @params[extract_key(@options[:class])]
243
+ else
244
+ @params[extract_key(namespaced_name)]
245
+ end
239
246
  end
240
247
 
241
248
  def params_method
@@ -277,7 +284,7 @@ module CanCan
277
284
  [:new, :create] + Array(@options[:new])
278
285
  end
279
286
 
280
- def param_actions
287
+ def save_actions
281
288
  [:create, :update]
282
289
  end
283
290
 
@@ -4,7 +4,7 @@ if rspec_module == 'RSpec'
4
4
  require 'rspec/core'
5
5
  require 'rspec/expectations'
6
6
  else
7
- ActiveSupport::Deprecation.warn("RSpec v1 will not be supported in the CanCanCan >= 2.0.0")
7
+ ActiveSupport::Deprecation.warn("RSpec < 3 will not be supported in the CanCanCan >= 2.0.0")
8
8
  end
9
9
 
10
10
  Kernel.const_get(rspec_module)::Matchers.define :be_able_to do |*args|
@@ -12,11 +12,17 @@ Kernel.const_get(rspec_module)::Matchers.define :be_able_to do |*args|
12
12
  ability.can?(*args)
13
13
  end
14
14
 
15
- failure_message_for_should do |ability|
15
+ # Check that RSpec is < 2.99
16
+ if !respond_to?(:failure_message) && respond_to?(:failure_message_for_should)
17
+ alias :failure_message :failure_message_for_should
18
+ alias :failure_message_when_negated :failure_message_for_should_not
19
+ end
20
+
21
+ failure_message do |ability|
16
22
  "expected to be able to #{args.map(&:inspect).join(" ")}"
17
23
  end
18
24
 
19
- failure_message_for_should_not do |ability|
25
+ failure_message_when_negated do |ability|
20
26
  "expected not to be able to #{args.map(&:inspect).join(" ")}"
21
27
  end
22
28
  end
@@ -0,0 +1,47 @@
1
+ module CanCan
2
+ module ModelAdapters
3
+ class ActiveRecord3Adapter < AbstractAdapter
4
+ include ActiveRecordAdapter
5
+ def self.for_class?(model_class)
6
+ model_class <= ActiveRecord::Base
7
+ end
8
+
9
+ def self.override_condition_matching?(subject, name, value)
10
+ name.kind_of?(MetaWhere::Column) if defined? MetaWhere
11
+ end
12
+
13
+ def self.matches_condition?(subject, name, value)
14
+ subject_value = subject.send(name.column)
15
+ if name.method.to_s.ends_with? "_any"
16
+ value.any? { |v| meta_where_match? subject_value, name.method.to_s.sub("_any", ""), v }
17
+ elsif name.method.to_s.ends_with? "_all"
18
+ value.all? { |v| meta_where_match? subject_value, name.method.to_s.sub("_all", ""), v }
19
+ else
20
+ meta_where_match? subject_value, name.method, value
21
+ end
22
+ end
23
+
24
+ def self.meta_where_match?(subject_value, method, value)
25
+ case method.to_sym
26
+ when :eq then subject_value == value
27
+ when :not_eq then subject_value != value
28
+ when :in then value.include?(subject_value)
29
+ when :not_in then !value.include?(subject_value)
30
+ when :lt then subject_value < value
31
+ when :lteq then subject_value <= value
32
+ when :gt then subject_value > value
33
+ when :gteq then subject_value >= value
34
+ when :matches then subject_value =~ Regexp.new("^" + Regexp.escape(value).gsub("%", ".*") + "$", true)
35
+ when :does_not_match then !meta_where_match?(subject_value, :matches, value)
36
+ else raise NotImplemented, "The #{method} MetaWhere condition is not supported."
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def build_relation(*where_conditions)
43
+ @model_class.where(*where_conditions).includes(joins)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,21 @@
1
+ module CanCan
2
+ module ModelAdapters
3
+ class ActiveRecord4Adapter < AbstractAdapter
4
+ include ActiveRecordAdapter
5
+ def self.for_class?(model_class)
6
+ model_class <= ActiveRecord::Base
7
+ end
8
+
9
+ private
10
+
11
+ # As of rails 4, `includes()` no longer causes active record to
12
+ # look inside the where clause to decide to outer join tables
13
+ # you're using in the where. Instead, `references()` is required
14
+ # in addition to `includes()` to force the outer join.
15
+ #
16
+ def build_relation(*where_conditions)
17
+ @model_class.where(*where_conditions).includes(joins).references(joins)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,41 +1,6 @@
1
1
  module CanCan
2
2
  module ModelAdapters
3
- class ActiveRecordAdapter < AbstractAdapter
4
- def self.for_class?(model_class)
5
- model_class <= ActiveRecord::Base
6
- end
7
-
8
- def self.override_condition_matching?(subject, name, value)
9
- name.kind_of?(MetaWhere::Column) if defined? MetaWhere
10
- end
11
-
12
- def self.matches_condition?(subject, name, value)
13
- subject_value = subject.send(name.column)
14
- if name.method.to_s.ends_with? "_any"
15
- value.any? { |v| meta_where_match? subject_value, name.method.to_s.sub("_any", ""), v }
16
- elsif name.method.to_s.ends_with? "_all"
17
- value.all? { |v| meta_where_match? subject_value, name.method.to_s.sub("_all", ""), v }
18
- else
19
- meta_where_match? subject_value, name.method, value
20
- end
21
- end
22
-
23
- def self.meta_where_match?(subject_value, method, value)
24
- case method.to_sym
25
- when :eq then subject_value == value
26
- when :not_eq then subject_value != value
27
- when :in then value.include?(subject_value)
28
- when :not_in then !value.include?(subject_value)
29
- when :lt then subject_value < value
30
- when :lteq then subject_value <= value
31
- when :gt then subject_value > value
32
- when :gteq then subject_value >= value
33
- when :matches then subject_value =~ Regexp.new("^" + Regexp.escape(value).gsub("%", ".*") + "$", true)
34
- when :does_not_match then !meta_where_match?(subject_value, :matches, value)
35
- else raise NotImplemented, "The #{method} MetaWhere condition is not supported."
36
- end
37
- end
38
-
3
+ module ActiveRecordAdapter
39
4
  # Returns conditions intended to be used inside a database query. Normally you will not call this
40
5
  # method directly, but instead go through ModelAdditions#accessible_by.
41
6
  #
@@ -99,11 +64,10 @@ module CanCan
99
64
  if override_scope
100
65
  @model_class.where(nil).merge(override_scope)
101
66
  elsif @model_class.respond_to?(:where) && @model_class.respond_to?(:joins)
102
- mergeable_conditions = @rules.select {|rule| rule.unmergeable? }.blank?
103
- if mergeable_conditions
104
- @model_class.where(conditions).includes(joins)
67
+ if mergeable_conditions?
68
+ build_relation(conditions)
105
69
  else
106
- @model_class.where(*(@rules.map(&:conditions))).includes(joins)
70
+ build_relation(*(@rules.map(&:conditions)))
107
71
  end
108
72
  else
109
73
  @model_class.all(:conditions => conditions, :joins => joins)
@@ -112,6 +76,10 @@ module CanCan
112
76
 
113
77
  private
114
78
 
79
+ def mergeable_conditions?
80
+ @rules.find {|rule| rule.unmergeable? }.blank?
81
+ end
82
+
115
83
  def override_scope
116
84
  conditions = @rules.map(&:conditions).compact
117
85
  if defined?(ActiveRecord::Relation) && conditions.any? { |c| c.kind_of?(ActiveRecord::Relation) }
@@ -1,3 +1,3 @@
1
1
  module CanCan
2
- VERSION = "1.8.0"
2
+ VERSION = "1.9.0"
3
3
  end
data/lib/cancan.rb CHANGED
@@ -9,7 +9,17 @@ require 'cancan/inherited_resource'
9
9
 
10
10
  require 'cancan/model_adapters/abstract_adapter'
11
11
  require 'cancan/model_adapters/default_adapter'
12
- require 'cancan/model_adapters/active_record_adapter' if defined? ActiveRecord
12
+
13
+ if defined? ActiveRecord
14
+ require 'cancan/model_adapters/active_record_adapter'
15
+ if ActiveRecord.respond_to?(:version) &&
16
+ ActiveRecord.version >= Gem::Version.new("4")
17
+ require 'cancan/model_adapters/active_record_4_adapter'
18
+ else
19
+ require 'cancan/model_adapters/active_record_3_adapter'
20
+ end
21
+ end
22
+
13
23
  require 'cancan/model_adapters/data_mapper_adapter' if defined? DataMapper
14
24
  require 'cancan/model_adapters/mongoid_adapter' if defined?(Mongoid) && defined?(Mongoid::Document)
15
25
  require 'cancan/model_adapters/sequel_adapter' if defined? Sequel
@@ -27,6 +27,6 @@ class Ability
27
27
  # can :update, Article, :published => true
28
28
  #
29
29
  # See the wiki for details:
30
- # https://github.com/bryanrite/cancancan/wiki/Defining-Abilities
30
+ # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
31
31
  end
32
32
  end