cancancan 1.15.0 → 1.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +38 -0
  3. data/.rubocop_todo.yml +48 -0
  4. data/.travis.yml +8 -2
  5. data/Appraisals +1 -0
  6. data/CHANGELOG.rdoc +5 -0
  7. data/Gemfile +1 -1
  8. data/README.md +58 -41
  9. data/Rakefile +7 -3
  10. data/cancancan.gemspec +13 -12
  11. data/gemfiles/activerecord_4.2.gemfile +1 -0
  12. data/lib/cancan.rb +2 -2
  13. data/lib/cancan/ability.rb +26 -24
  14. data/lib/cancan/controller_additions.rb +33 -23
  15. data/lib/cancan/controller_resource.rb +83 -56
  16. data/lib/cancan/exceptions.rb +1 -1
  17. data/lib/cancan/matchers.rb +2 -2
  18. data/lib/cancan/model_adapters/abstract_adapter.rb +8 -8
  19. data/lib/cancan/model_adapters/active_record_4_adapter.rb +48 -35
  20. data/lib/cancan/model_adapters/active_record_adapter.rb +18 -17
  21. data/lib/cancan/model_adapters/mongoid_adapter.rb +26 -21
  22. data/lib/cancan/model_adapters/sequel_adapter.rb +12 -12
  23. data/lib/cancan/model_additions.rb +0 -1
  24. data/lib/cancan/rule.rb +23 -17
  25. data/lib/cancan/version.rb +1 -1
  26. data/lib/generators/cancan/ability/ability_generator.rb +1 -1
  27. data/spec/cancan/ability_spec.rb +189 -180
  28. data/spec/cancan/controller_additions_spec.rb +77 -64
  29. data/spec/cancan/controller_resource_spec.rb +230 -228
  30. data/spec/cancan/exceptions_spec.rb +20 -20
  31. data/spec/cancan/inherited_resource_spec.rb +21 -21
  32. data/spec/cancan/matchers_spec.rb +12 -12
  33. data/spec/cancan/model_adapters/active_record_4_adapter_spec.rb +38 -32
  34. data/spec/cancan/model_adapters/active_record_adapter_spec.rb +155 -145
  35. data/spec/cancan/model_adapters/default_adapter_spec.rb +2 -2
  36. data/spec/cancan/model_adapters/mongoid_adapter_spec.rb +87 -88
  37. data/spec/cancan/model_adapters/sequel_adapter_spec.rb +44 -47
  38. data/spec/cancan/rule_spec.rb +18 -18
  39. data/spec/spec_helper.rb +2 -2
  40. data/spec/support/ability.rb +0 -1
  41. metadata +60 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f603bcd9b02f8b042e5de713df94cb01abec7486
4
- data.tar.gz: 358a5080dcef4525f7993d9c1383daffc147d169
3
+ metadata.gz: 993f2327cf688ab257cf339cabfd621ee1757d86
4
+ data.tar.gz: 8e04df79a1f6619758cfa6faffa8a9d8a91bf647
5
5
  SHA512:
6
- metadata.gz: c56c8b9e82e5ab6868a5dafcf811d2363b023c4f1eb05149de64996919e6da6e60dff583e7d1b9f1c1eeb277a52a5327e03b52bf753b3ed79ce2cc22fad0b431
7
- data.tar.gz: a1be98535ecbcde3db1b8accc41446bbb139b413b544a2541af00f699e5e5a548d3fdfdb41b09ed951504d4c8a6a31fc24a50aea690ad70ed2fb224a1676d954
6
+ metadata.gz: f761b9d2b370a9105184d09cb05ea11729ca16fae21dd74e979cb07c5885021181cd4f4aa75d8562b49c55663cf70a4d84f8de8ec0b1ece6da3a5ee1b117aa9a
7
+ data.tar.gz: 372c69b7c60be72ccb473137fcd1ae2be1815a8b6996a520d53c13079d425cd0ab879c9867bedfb39827db85c04efb0d499cf72fd1dbfbcc7848db3934cb5e0a
@@ -0,0 +1,38 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Style/NonNilCheck:
5
+ IncludeSemanticChanges: true
6
+
7
+ Style/FrozenStringLiteralComment:
8
+ Enabled: false
9
+
10
+ Style/EmptyMethod:
11
+ Enabled: false
12
+
13
+ Style/VariableNumber:
14
+ Enabled: false
15
+
16
+ Style/ClassAndModuleChildren:
17
+ Enabled: false
18
+
19
+ Metrics/LineLength:
20
+ Max: 120
21
+
22
+ Metrics/ClassLength:
23
+ Exclude:
24
+ - 'lib/cancan/controller_resource.rb'
25
+ - 'lib/cancan/rule.rb'
26
+
27
+ Metrics/ModuleLength:
28
+ Exclude:
29
+ - "**/*_spec.rb"
30
+ - 'lib/cancan/ability.rb'
31
+ - 'lib/cancan/model_adapters/active_record_adapter.rb'
32
+
33
+ AllCops:
34
+ TargetRubyVersion: 2.0
35
+ Exclude:
36
+ - 'gemfiles/vendor/bundle/**/*'
37
+
38
+ inherit_from: .rubocop_todo.yml
@@ -0,0 +1,48 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2016-12-14 08:11:19 +0100 using RuboCop version 0.45.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 14
10
+ Metrics/AbcSize:
11
+ Max: 25
12
+
13
+ # Offense count: 5
14
+ Metrics/CyclomaticComplexity:
15
+ Max: 9
16
+
17
+ # Offense count: 14
18
+ # Configuration parameters: CountComments.
19
+ Metrics/MethodLength:
20
+ Max: 21
21
+
22
+
23
+ Metrics/BlockLength:
24
+ Max: 28
25
+ Exclude:
26
+ - "**/*_spec.rb"
27
+
28
+ # Offense count: 7
29
+ Metrics/PerceivedComplexity:
30
+ Max: 11
31
+
32
+ # TODO: due to mongoid. can't be fixed
33
+ # Offense count: 1
34
+ Performance/FixedSize:
35
+ Exclude:
36
+ - 'spec/cancan/model_adapters/mongoid_adapter_spec.rb'
37
+
38
+
39
+ # TODO: fixing this would change the APIs
40
+ # Offense count: 2
41
+ # Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
42
+ # NamePrefix: is_, has_, have_
43
+ # NamePrefixBlacklist: is_, has_, have_
44
+ # NameWhitelist: is_a?
45
+ Style/PredicateName:
46
+ Exclude:
47
+ - 'spec/**/*'
48
+ - 'lib/cancan/ability.rb'
@@ -29,5 +29,11 @@ matrix:
29
29
  - rvm: jruby-9.0.5.0
30
30
  gemfile: gemfiles/activerecord_5.0.gemfile
31
31
  notifications:
32
- recipients:
33
- - bryan@bryanrite.com
32
+ email:
33
+ recipients:
34
+ - alessandro.rodi@renuo.ch
35
+ - josua.schmid@renuo.ch
36
+ - zora.fuchs@renuo.ch
37
+ on_success: change
38
+ on_failure: change
39
+
data/Appraisals CHANGED
@@ -47,6 +47,7 @@ appraise "activerecord_4.2" do
47
47
  gem "activerecord", "~> 4.2.0", :require => "active_record"
48
48
  gem 'activesupport', '~> 4.2.0', :require => 'active_support/all'
49
49
  gem "actionpack", "~> 4.2.0", :require => "action_pack"
50
+ gem "nokogiri", "~> 1.6.8", :require => "nokogiri" # TODO: fix for ruby 2.0.0
50
51
 
51
52
  gemfile.platforms :jruby do
52
53
  gem "activerecord-jdbcsqlite3-adapter"
@@ -2,6 +2,10 @@ Develop
2
2
 
3
3
  Unreleased
4
4
 
5
+ 1.16.0 (February 2nd, 2017)
6
+
7
+ * Introduce rubocop and fixes most of the issues
8
+
5
9
  1.15.0 (June 13th, 2016)
6
10
 
7
11
  * Add support for Rails 5 (craig1410)
@@ -19,6 +23,7 @@ Unreleased
19
23
 
20
24
  * Significantly improve rule lookup time (amarshall)
21
25
  * Removed deprecation warnings for RSpec 3.2 (NekoNova)
26
+ * Drop support for REE and Ruby 1.x and so Rails 2 (Richard Wilson)
22
27
 
23
28
  1.12.0 (June 28th, 2015)
24
29
 
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
data/README.md CHANGED
@@ -5,33 +5,31 @@
5
5
  [![Code Climate Badge](https://codeclimate.com/github/CanCanCommunity/cancancan.svg)](https://codeclimate.com/github/CanCanCommunity/cancancan)
6
6
  [![Inch CI](http://inch-ci.org/github/CanCanCommunity/cancancan.svg)](http://inch-ci.org/github/CanCanCommunity/cancancan)
7
7
 
8
- [Wiki](https://github.com/CanCanCommunity/cancancan/wiki) | [RDocs](http://rdoc.info/projects/CanCanCommunity/cancancan) | [Screencast](http://railscasts.com/episodes/192-authorization-with-cancan) | [IRC: #cancancan (freenode)](http://webchat.freenode.net/?channels=cancancan)
8
+ [Wiki](https://github.com/CanCanCommunity/cancancan/wiki) |
9
+ [RDocs](http://rdoc.info/projects/CanCanCommunity/cancancan) |
10
+ [Screencast](http://railscasts.com/episodes/192-authorization-with-cancan) |
11
+ [Gitter](https://gitter.im/CanCanCommunity/cancancan)
9
12
 
10
- CanCan is an authorization library for Ruby on Rails which restricts what resources a given user is allowed to access. All permissions are defined in a single location (the `Ability` class) and not duplicated across controllers, views, and database queries.
13
+ CanCanCan is an authorization library for Ruby 2.0+ and Ruby on Rails 3+ which restricts what resources a given user is allowed to access.
11
14
 
12
- ## This is the master branch!
13
- This branch represents work towards version 2.0. Please checkout the 1.x branch for the stable release. Use master at your own risk.
14
-
15
- ## Mission
16
-
17
- This repo is a continuation of the dead [CanCan](https://github.com/ryanb/cancan) project. Our mission is to keep CanCan alive and moving forward, with maintenance fixes and new features. Pull Requests are welcome!
18
-
19
- I am currently focusing on the 1.x branch for the immediate future, making sure it is up to date as well as ensuring compatibility with Rails 4+. I will take a look into the 2.x branch and try to see what improvements, reorganizations and redesigns Ryan was attempting and go forward from there.
20
-
21
- Any help is greatly appreciated, feel free to submit pull-requests or open issues.
15
+ All permissions are defined in a single location (the `Ability` class) and not duplicated across controllers, views, and database queries.
22
16
 
23
17
 
24
18
  ## Installation
25
19
 
26
- In **Rails 3 and 4**, add this to your Gemfile and run the `bundle install` command.
20
+ Add this to your Gemfile:
27
21
 
28
- gem 'cancancan', '~> 1.10'
22
+ gem 'cancancan'
23
+
24
+ and run the `bundle install` command.
29
25
 
30
26
  ## Getting Started
31
27
 
32
- CanCanCan expects a `current_user` method to exist in the controller. First, set up some authentication (such as [Authlogic](https://github.com/binarylogic/authlogic) or [Devise](https://github.com/plataformatec/devise)). See [Changing Defaults](https://github.com/CanCanCommunity/cancancan/wiki/changing-defaults) if you need different behavior.
28
+ CanCanCan expects a `current_user` method to exist in the controller.
29
+ First, set up some authentication (such as [Devise](https://github.com/plataformatec/devise) or [Authlogic](https://github.com/binarylogic/authlogic)).
30
+ See [Changing Defaults](https://github.com/CanCanCommunity/cancancan/wiki/changing-defaults) if you need a different behavior.
33
31
 
34
- When using [rails-api](https://github.com/rails-api/rails-api), you have to manually include the controller methods for CanCan:
32
+ When using [rails-api](https://github.com/rails-api/rails-api), you have to manually include the controller methods for CanCanCan:
35
33
  ```ruby
36
34
  class ApplicationController < ActionController::API
37
35
  include CanCan::ControllerAdditions
@@ -40,27 +38,16 @@ end
40
38
 
41
39
  ### 1. Define Abilities
42
40
 
43
- User permissions are defined in an `Ability` class. CanCan 1.5 includes a Rails 3 and 4 generator for creating this class.
41
+ User permissions are defined in an `Ability` class.
44
42
 
45
43
  rails g cancan:ability
46
44
 
47
- In Rails 2.3, just add a new class in `app/models/ability.rb` with the following contents:
48
-
49
- ```ruby
50
- class Ability
51
- include CanCan::Ability
52
-
53
- def initialize(user)
54
- end
55
- end
56
- ```
57
-
58
45
  See [Defining Abilities](https://github.com/CanCanCommunity/cancancan/wiki/defining-abilities) for details.
59
46
 
60
47
 
61
48
  ### 2. Check Abilities & Authorization
62
49
 
63
- The current user's permissions can then be checked using the `can?` and `cannot?` methods in the view and controller.
50
+ The current user's permissions can then be checked using the `can?` and `cannot?` methods in views and controllers.
64
51
 
65
52
  ```erb
66
53
  <% if can? :update, @article %>
@@ -79,7 +66,9 @@ def show
79
66
  end
80
67
  ```
81
68
 
82
- Setting this for every action can be tedious, therefore the `load_and_authorize_resource` method is provided to automatically authorize all actions in a RESTful style resource controller. It will use a before filter to load the resource into an instance variable and authorize it for every action.
69
+ Setting this for every action can be tedious, therefore the `load_and_authorize_resource` method is provided to
70
+ automatically authorize all actions in a RESTful style resource controller.
71
+ It will use a before action to load the resource into an instance variable and authorize it for every action.
83
72
 
84
73
  ```ruby
85
74
  class ArticlesController < ApplicationController
@@ -98,7 +87,7 @@ See [Authorizing Controller Actions](https://github.com/CanCanCommunity/cancanca
98
87
 
99
88
  When using `strong_parameters` or Rails 4+, you have to sanitize inputs before saving the record, in actions such as `:create` and `:update`.
100
89
 
101
- For the `:update` action, CanCan will load and authorize the resource but *not* change it automatically, so the typical usage would be something like:
90
+ For the `:update` action, CanCanCan will load and authorize the resource but *not* change it automatically, so the typical usage would be something like:
102
91
 
103
92
  ```ruby
104
93
  def update
@@ -115,7 +104,8 @@ def update_params
115
104
  end
116
105
  ```
117
106
 
118
- For the `:create` action, CanCan will try to initialize a new instance with sanitized input by seeing if your controller will respond to the following methods (in order):
107
+ For the `:create` action, CanCan will try to initialize a new instance with sanitized input by seeing if your
108
+ controller will respond to the following methods (in order):
119
109
 
120
110
  1. `create_params`
121
111
  2. `<model_name>_params` such as `article_params` (this is the default convention in rails for naming your param method)
@@ -145,7 +135,7 @@ class ArticlesController < ApplicationController
145
135
  end
146
136
  ```
147
137
 
148
- You can also use a string that will be evaluated in the context of the controller using `instance_eval` and needs to contain valid Ruby code. This does come in handy when using a PermittedParams class as suggested in Railscast 371:
138
+ You can also use a string that will be evaluated in the context of the controller using `instance_eval` and needs to contain valid Ruby code.
149
139
 
150
140
  load_and_authorize_resource param_method: 'permitted_params.article'
151
141
 
@@ -157,13 +147,18 @@ See [Strong Parameters](https://github.com/CanCanCommunity/cancancan/wiki/Strong
157
147
 
158
148
  ### 3. Handle Unauthorized Access
159
149
 
160
- If the user authorization fails, a `CanCan::AccessDenied` exception will be raised. You can catch this and modify its behavior in the `ApplicationController`.
150
+ If the user authorization fails, a `CanCan::AccessDenied` exception will be raised.
151
+ You can catch this and modify its behavior in the `ApplicationController`.
161
152
 
162
153
  ```ruby
163
154
  class ApplicationController < ActionController::Base
164
155
  rescue_from CanCan::AccessDenied do |exception|
165
- redirect_to root_url, :alert => exception.message
166
- end
156
+ respond_to do |format|
157
+ format.json { head :forbidden, content_type: 'text/html' }
158
+ format.html { redirect_to main_app.root_url, notice: exception.message }
159
+ format.js { head :forbidden, content_type: 'text/html' }
160
+ end
161
+ end
167
162
  end
168
163
  ```
169
164
 
@@ -180,7 +175,9 @@ class ApplicationController < ActionController::Base
180
175
  end
181
176
  ```
182
177
 
183
- This will raise an exception if authorization is not performed in an action. If you want to skip this, add `skip_authorization_check` to a controller subclass. See [Ensure Authorization](https://github.com/CanCanCommunity/cancancan/wiki/Ensure-Authorization) for more information.
178
+ This will raise an exception if authorization is not performed in an action.
179
+ If you want to skip this, add `skip_authorization_check` to a controller subclass.
180
+ See [Ensure Authorization](https://github.com/CanCanCommunity/cancancan/wiki/Ensure-Authorization) for more information.
184
181
 
185
182
 
186
183
  ## Wiki Docs
@@ -193,8 +190,22 @@ This will raise an exception if authorization is not performed in an action. If
193
190
  * [Changing Defaults](https://github.com/CanCanCommunity/cancancan/wiki/Changing-Defaults)
194
191
  * [See more](https://github.com/CanCanCommunity/cancancan/wiki)
195
192
 
193
+ ## Mission
194
+
195
+ This repo is a continuation of the dead [CanCan](https://github.com/ryanb/cancan) project.
196
+ Our mission is to keep CanCan alive and moving forward, with maintenance fixes and new features.
197
+ Pull Requests are welcome!
198
+
199
+ Any help is greatly appreciated, feel free to submit pull-requests or open issues.
200
+
201
+
196
202
  ## Questions?
197
- If you have any question or doubt regarding CanCanCan which you cannot find the solution to in the [documentation](https://github.com/CanCanCommunity/cancancan/wiki) or our [mailing list](http://groups.google.com/group/cancancan), please [open a question on Stackoverflow](http://stackoverflow.com/questions/ask?tags=cancancan) with tag [cancancan](http://stackoverflow.com/questions/tagged/cancancan)
203
+
204
+ If you have any question or doubt regarding CanCanCan which you cannot find the solution to in the
205
+ [documentation](https://github.com/CanCanCommunity/cancancan/wiki) or our
206
+ [mailing list](http://groups.google.com/group/cancancan), please
207
+ [open a question on Stackoverflow](http://stackoverflow.com/questions/ask?tags=cancancan) with tag
208
+ [cancancan](http://stackoverflow.com/questions/tagged/cancancan)
198
209
 
199
210
  ## Bugs?
200
211
 
@@ -203,15 +214,21 @@ If you find a bug please add an [issue on GitHub](https://github.com/CanCanCommu
203
214
 
204
215
  ## Development
205
216
 
206
- Cancancan uses [appraisals](https://github.com/thoughtbot/appraisal) to test the code base against multiple versions of Rails, as well as the different model adapters.
217
+ CanCanCan uses [appraisals](https://github.com/thoughtbot/appraisal) to test the code base against multiple versions
218
+ of Rails, as well as the different model adapters.
207
219
 
208
220
  When first developing, you may need to run `bundle install` and then `appraisal install`, to install the different sets.
209
221
 
210
222
  You can then run all appraisal files (like CI does), with `appraisal rake` or just run a specific set `appraisal activerecord_3.0 rake`.
211
223
 
212
- See the [CONTRIBUTING](https://github.com/CanCanCommunity/cancancan/blob/develop/CONTRIBUTING.md) and [spec/README](https://github.com/CanCanCommunity/cancancan/blob/master/spec/README.rdoc) for more information.
224
+ See the [CONTRIBUTING](https://github.com/CanCanCommunity/cancancan/blob/develop/CONTRIBUTING.md) and
225
+ [spec/README](https://github.com/CanCanCommunity/cancancan/blob/master/spec/README.rdoc) for more information.
213
226
 
214
227
 
215
228
  ## Special Thanks
216
229
 
217
- CanCan was inspired by [declarative_authorization](https://github.com/stffn/declarative_authorization/) and [aegis](https://github.com/makandra/aegis). Also many thanks to the [CanCan contributors](https://github.com/CanCanCommunity/cancancan/contributors). See the [CHANGELOG](https://github.com/CanCanCommunity/cancancan/blob/master/CHANGELOG.rdoc) for the full list.
230
+ CanCanCan was inspired by [declarative_authorization](https://github.com/stffn/declarative_authorization/) and
231
+ [aegis](https://github.com/makandra/aegis).
232
+
233
+ Also many thanks to the [CanCanCan contributors](https://github.com/CanCanCommunity/cancancan/contributors).
234
+ See the [CHANGELOG](https://github.com/CanCanCommunity/cancancan/blob/master/CHANGELOG.rdoc) for the full list.
data/Rakefile CHANGED
@@ -1,9 +1,13 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
3
4
 
4
- desc "Run RSpec"
5
+ desc 'Run Rubocop'
6
+ RuboCop::RakeTask.new
7
+
8
+ desc 'Run RSpec'
5
9
  RSpec::Core::RakeTask.new do |t|
6
10
  t.verbose = false
7
11
  end
8
12
 
9
- task :default => :spec
13
+ task default: [:rubocop, :spec]
@@ -4,24 +4,25 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'cancan/version'
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = "cancancan"
7
+ s.name = 'cancancan'
8
8
  s.version = CanCan::VERSION
9
- s.authors = ["Bryan Rite", "Ryan Bates", "Richard Wilson"]
10
- s.email = "r.crawfordwilson@gmail.com"
11
- s.homepage = "https://github.com/CanCanCommunity/cancancan"
12
- s.summary = "Simple authorization solution for Rails."
13
- s.description = "Continuation of the simple authorization solution for Rails which is decoupled from user roles. All permissions are stored in a single location."
9
+ s.authors = ['Alessandro Rodi (Renuo AG)', 'Bryan Rite', 'Ryan Bates', 'Richard Wilson']
10
+ s.email = 'alessandro.rodi@renuo.ch'
11
+ s.homepage = 'https://github.com/CanCanCommunity/cancancan'
12
+ s.summary = 'Simple authorization solution for Rails.'
13
+ s.description = 'Simple authorization solution for Rails. All permissions are stored in a single location.'
14
14
  s.platform = Gem::Platform::RUBY
15
- s.license = "MIT"
15
+ s.license = 'MIT'
16
16
 
17
- s.files = `git ls-files`.split($/)
18
- s.test_files = `git ls-files -- Appraisals {spec,features,gemfiles}/*`.split($/)
19
- s.executables = `git ls-files -- bin/*`.split($/).map{ |f| File.basename(f) }
20
- s.require_paths = ["lib"]
17
+ s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
18
+ s.test_files = `git ls-files -- Appraisals {spec,features,gemfiles}/*`.split($INPUT_RECORD_SEPARATOR)
19
+ s.executables = `git ls-files -- bin/*`.split($INPUT_RECORD_SEPARATOR).map { |f| File.basename(f) }
20
+ s.require_paths = ['lib']
21
21
 
22
- s.required_ruby_version = ">= 2.0.0"
22
+ s.required_ruby_version = '>= 2.0.0'
23
23
 
24
24
  s.add_development_dependency 'bundler', '~> 1.3'
25
+ s.add_development_dependency 'rubocop', '~> 0.46'
25
26
  s.add_development_dependency 'rake', '~> 10.1.1'
26
27
  s.add_development_dependency 'rspec', '~> 3.2.0'
27
28
  s.add_development_dependency 'appraisal', '>= 2.0.0'
@@ -5,6 +5,7 @@ source "https://rubygems.org"
5
5
  gem "activerecord", "~> 4.2.0", :require => "active_record"
6
6
  gem "activesupport", "~> 4.2.0", :require => "active_support/all"
7
7
  gem "actionpack", "~> 4.2.0", :require => "action_pack"
8
+ gem "nokogiri", "~> 1.6.8", :require => "nokogiri"
8
9
 
9
10
  platforms :jruby do
10
11
  gem "activerecord-jdbcsqlite3-adapter"
@@ -1,4 +1,4 @@
1
- require "cancan/version"
1
+ require 'cancan/version'
2
2
  require 'cancan/ability'
3
3
  require 'cancan/rule'
4
4
  require 'cancan/controller_resource'
@@ -13,7 +13,7 @@ require 'cancan/model_adapters/default_adapter'
13
13
  if defined? ActiveRecord
14
14
  require 'cancan/model_adapters/active_record_adapter'
15
15
  if ActiveRecord.respond_to?(:version) &&
16
- ActiveRecord.version >= Gem::Version.new("4")
16
+ ActiveRecord.version >= Gem::Version.new('4')
17
17
  require 'cancan/model_adapters/active_record_4_adapter'
18
18
  else
19
19
  require 'cancan/model_adapters/active_record_3_adapter'
@@ -1,5 +1,4 @@
1
1
  module CanCan
2
-
3
2
  # This module is designed to be included into an Ability class. This will
4
3
  # provide the "can" methods for defining and checking abilities.
5
4
  #
@@ -68,6 +67,7 @@ module CanCan
68
67
  end.reject(&:nil?).first
69
68
  match ? match.base_behavior : false
70
69
  end
70
+
71
71
  # Convenience method which works the same as "can?" but returns the opposite value.
72
72
  #
73
73
  # cannot? :destroy, @project
@@ -187,7 +187,8 @@ module CanCan
187
187
 
188
188
  # User shouldn't specify targets with names of real actions or it will cause Seg fault
189
189
  def validate_target(target)
190
- raise Error, "You can't specify target (#{target}) as alias because it is real action name" if aliased_actions.values.flatten.include? target
190
+ error_message = "You can't specify target (#{target}) as alias because it is real action name"
191
+ raise Error, error_message if aliased_actions.values.flatten.include? target
191
192
  end
192
193
 
193
194
  # Returns a hash of aliased actions. The key is the target and the value is an array of actions aliasing the key.
@@ -208,7 +209,7 @@ module CanCan
208
209
  # See ControllerAdditions#authorize! for documentation.
209
210
  def authorize!(action, subject, *args)
210
211
  message = nil
211
- if args.last.kind_of?(Hash) && args.last.has_key?(:message)
212
+ if args.last.is_a?(Hash) && args.last.key?(:message)
212
213
  message = args.pop[:message]
213
214
  end
214
215
  if cannot?(action, subject, *args)
@@ -220,9 +221,9 @@ module CanCan
220
221
 
221
222
  def unauthorized_message(action, subject)
222
223
  keys = unauthorized_message_keys(action, subject)
223
- variables = {:action => action.to_s}
224
+ variables = { action: action.to_s }
224
225
  variables[:subject] = (subject.class == Class ? subject : subject.class).to_s.underscore.humanize.downcase
225
- message = I18n.translate(nil, variables.merge(:scope => :unauthorized, :default => keys + [""]))
226
+ message = I18n.translate(nil, variables.merge(scope: :unauthorized, default: keys + ['']))
226
227
  message.blank? ? nil : message
227
228
  end
228
229
 
@@ -260,12 +261,12 @@ module CanCan
260
261
  # action: array_of_objects
261
262
  # }
262
263
  def permissions
263
- permissions_list = {:can => {}, :cannot => {}}
264
+ permissions_list = { can: {}, cannot: {} }
264
265
 
265
266
  rules.each do |rule|
266
267
  subjects = rule.subjects
267
268
  expand_actions(rule.actions).each do |action|
268
- if(rule.base_behavior)
269
+ if rule.base_behavior
269
270
  permissions_list[:can][action] ||= []
270
271
  permissions_list[:can][action] += subjects.map(&:to_s)
271
272
  else
@@ -289,7 +290,7 @@ module CanCan
289
290
  private
290
291
 
291
292
  def unauthorized_message_keys(action, subject)
292
- subject = (subject.class == Class ? subject : subject.class).name.underscore unless subject.kind_of? Symbol
293
+ subject = (subject.class == Class ? subject : subject.class).name.underscore unless subject.is_a? Symbol
293
294
  [subject, :all].map do |try_subject|
294
295
  [aliases_for_action(action), :manage].flatten.map do |try_action|
295
296
  :"#{try_action}.#{try_subject}"
@@ -305,7 +306,7 @@ module CanCan
305
306
  expanded = []
306
307
  actions.each do |action|
307
308
  expanded << action
308
- if aliases = aliased_actions[action]
309
+ if (aliases = aliased_actions[action])
309
310
  expanded += expand_actions(aliases)
310
311
  end
311
312
  end
@@ -319,7 +320,7 @@ module CanCan
319
320
 
320
321
  # It translates to an array the subject or the hash with multiple subjects given to can?.
321
322
  def extract_subjects(subject)
322
- if subject.kind_of?(Hash) && subject.key?(:any)
323
+ if subject.is_a?(Hash) && subject.key?(:any)
323
324
  subject[:any]
324
325
  else
325
326
  [subject]
@@ -374,13 +375,12 @@ module CanCan
374
375
  def optimize_order!(rules)
375
376
  first_can_in_group = -1
376
377
  rules.each_with_index do |rule, i|
377
- (first_can_in_group = -1) and next unless rule.base_behavior
378
- (first_can_in_group = i) and next if first_can_in_group == -1
379
- if rule.subjects == [:all]
380
- rules[i] = rules[first_can_in_group]
381
- rules[first_can_in_group] = rule
382
- first_can_in_group += 1
383
- end
378
+ (first_can_in_group = -1) && next unless rule.base_behavior
379
+ (first_can_in_group = i) && next if first_can_in_group == -1
380
+ next unless rule.subjects == [:all]
381
+ rules[i] = rules[first_can_in_group]
382
+ rules[first_can_in_group] = rule
383
+ first_can_in_group += 1
384
384
  end
385
385
  end
386
386
 
@@ -396,25 +396,27 @@ module CanCan
396
396
 
397
397
  def relevant_rules_for_match(action, subject)
398
398
  relevant_rules(action, subject).each do |rule|
399
- if rule.only_raw_sql?
400
- raise Error, "The can? and cannot? call cannot be used with a raw sql 'can' definition. The checking code cannot be determined for #{action.inspect} #{subject.inspect}"
401
- end
399
+ next unless rule.only_raw_sql?
400
+ raise Error,
401
+ "The can? and cannot? call cannot be used with a raw sql 'can' definition."\
402
+ " The checking code cannot be determined for #{action.inspect} #{subject.inspect}"
402
403
  end
403
404
  end
404
405
 
405
406
  def relevant_rules_for_query(action, subject)
406
407
  relevant_rules(action, subject).each do |rule|
407
408
  if rule.only_block?
408
- raise Error, "The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined for #{action.inspect} #{subject.inspect}"
409
+ raise Error, "The accessible_by call cannot be used with a block 'can' definition."\
410
+ " The SQL cannot be determined for #{action.inspect} #{subject.inspect}"
409
411
  end
410
412
  end
411
413
  end
412
414
 
413
415
  def default_alias_actions
414
416
  {
415
- :read => [:index, :show],
416
- :create => [:new],
417
- :update => [:edit],
417
+ read: [:index, :show],
418
+ create: [:new],
419
+ update: [:edit]
418
420
  }
419
421
  end
420
422
  end