rolify 3.3.0.rc3 → 3.3.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.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.travis.yml +12 -11
  4. data/CHANGELOG.rdoc +18 -0
  5. data/Gemfile +20 -3
  6. data/README.md +84 -70
  7. data/Rakefile +21 -8
  8. data/UPGRADE.rdoc +22 -0
  9. data/gemfiles/Gemfile.rails-3.2 +21 -0
  10. data/gemfiles/Gemfile.rails-4.0 +27 -0
  11. data/lib/generators/mongoid/rolify_generator.rb +1 -1
  12. data/lib/generators/rolify/templates/README +1 -1
  13. data/lib/rolify/adapters/active_record/resource_adapter.rb +8 -7
  14. data/lib/rolify/adapters/active_record/role_adapter.rb +13 -9
  15. data/lib/rolify/configure.rb +1 -1
  16. data/lib/rolify/railtie.rb +1 -1
  17. data/lib/rolify/role.rb +3 -5
  18. data/lib/rolify/version.rb +1 -1
  19. data/lib/rolify.rb +13 -10
  20. data/rolify.gemspec +5 -12
  21. data/spec/README.rdoc +24 -0
  22. data/spec/generators/rolify/{rolify_generator_spec.rb → rolify_activerecord_generator_spec.rb} +13 -104
  23. data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
  24. data/spec/generators_helper.rb +8 -1
  25. data/spec/rolify/custom_spec.rb +9 -16
  26. data/spec/rolify/namespace_spec.rb +12 -15
  27. data/spec/rolify/resource_spec.rb +20 -2
  28. data/spec/rolify/resourcifed_and_rolifed_spec.rb +5 -1
  29. data/spec/rolify/role_spec.rb +9 -16
  30. data/spec/rolify/shared_contexts.rb +9 -3
  31. data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +2 -2
  32. data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +12 -4
  33. data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +43 -2
  34. data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +3 -3
  35. data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +3 -3
  36. data/spec/rolify/shared_examples/shared_examples_for_roles.rb +5 -3
  37. data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +10 -10
  38. data/spec/spec_helper.rb +3 -0
  39. data/spec/support/adapters/active_record.rb +9 -2
  40. data/spec/support/adapters/mongoid.rb +8 -4
  41. data/spec/support/data.rb +3 -0
  42. data/spec/support/schema.rb +6 -1
  43. metadata +51 -110
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: adb734bf9468069dbead78c35683ac1ace1525d8
4
+ data.tar.gz: b4a221f3435dda2c84ccccf7086bc4a41eadda8d
5
+ SHA512:
6
+ metadata.gz: 7168a6699067f23cf85cb6b74b599a043e26720be10c9746e7910cc7cbdc296b5d9ffe5922ea2d8484167ccb3c9c6ce6c347efddb20c14c7d843791d0f186e52
7
+ data.tar.gz: 2f701c24d16d9dccbac72a270a93f9cd6e470dd60584c548812a97a09458b75f00829d5b0320ae3ab080d123c713c2c5ad32282b4c615b8ebf11e672dd576de9
data/.gitignore CHANGED
@@ -1,8 +1,10 @@
1
1
  *.gem
2
2
  .bundle
3
3
  Gemfile.lock
4
+ gemfiles/*.lock
4
5
  pkg/*
5
6
  tmp/*
7
+ coverage/*
6
8
  log*/*
7
9
  .rbx/*
8
10
  .rspec
data/.travis.yml CHANGED
@@ -1,16 +1,17 @@
1
1
  rvm:
2
- - 1.9.3
3
- - ruby-head
4
- - rbx-19mode
5
- - jruby-19mode
2
+ - 1.9.3
3
+ - 2.0.0
4
+ - rbx-19mode
5
+ - jruby-19mode
6
+
7
+ gemfile:
8
+ - gemfiles/Gemfile.rails-3.2
9
+ - gemfiles/Gemfile.rails-4.0
6
10
 
7
11
  env:
8
- - ADAPTER=active_record
9
- - ADAPTER=mongoid
12
+ - ADAPTER=active_record
13
+ - ADAPTER=mongoid
10
14
 
11
- before_install:
12
- - gem update --system
13
- - gem --version
14
- - ruby -v
15
-
16
15
  services: mongodb
16
+
17
+ before_script: rails --version
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,21 @@
1
+ = 3.3 (not released yet)
2
+ * <b>DEPRECATION NOTICE:</b>Rails 3.1 support dropped: if you use Rails 3.1, please stick to rolify 3.2
3
+ * code cleanup in finders methods
4
+ * generators rewritten entirely. now using ActiveRecord/Mongoid model generator to create Role model
5
+ * added rspec matchers for detailed spec error messages (thanks to @delwyn)
6
+ * clean up specs (thanks to @delwyn), removed subject and let declarations in before(:all) block
7
+ * roles query needs 1 DB hit instead of 1 up to 3 (thanks to @terraplane)
8
+ * remove nil entries from ResourceAdapter#resources_find when using Mongoid adapter (thanks to @daviscabral)
9
+ * fixed a bug regarding redundant index for Mongoid Role model (thanks to @rschultheis)
10
+ * added support for rolify and resourcify methods on the same model class (specs by @amer)
11
+ * added support for namespaced models (thanks to @intrica)
12
+ * fixed compatibility issue with squeel when using symbols as role parameters (hint by @f3ndot)
13
+ * now raises a warning in the initializer if migration has not been run
14
+ * add support for primary key different than 'id' for resource Model (thanks to @rafaeldl)
15
+ * Rails 4 (thanks to @adammathys) and ruby 2.0 compliant
16
+ * configured travis-ci to run the specs on Rails 3.2/4.0 and Rubies 1.9.3/2.0/rbx/jruby
17
+ * added code climate to check for code smell
18
+
1
19
  = 3.2 (Aug 7, 2012)
2
20
  * <b>DEPRECATION NOTICE:</b> Ruby 1.8 support dropped ! Mongoid 3.0 only supports MRI 1.9.3, and HEAD, and JRuby 1.6.0+ in 1.9 mode
3
21
  * removed <tt>dynamic_shortcuts</tt> arguments from the generator
data/Gemfile CHANGED
@@ -1,4 +1,21 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in rolify.gemspec
4
- gemspec
3
+ case ENV["ADAPTER"]
4
+ when nil, "active_record"
5
+ group :test do
6
+ gem "activerecord-jdbcsqlite3-adapter", ">= 1.3.0.rc", :platform => "jruby"
7
+ gem "sqlite3", :platform => "ruby"
8
+ end
9
+ gem "activerecord", ">= 3.2.0", :require => "active_record"
10
+ when "mongoid"
11
+ gem "mongoid", ">= 3.1"
12
+ gem "bson_ext", :platform => "ruby"
13
+ else
14
+ raise "Unknown model adapter: #{ENV["ADAPTER"]}"
15
+ end
16
+
17
+ group :test do
18
+ gem 'coveralls', :require => false
19
+ end
20
+
21
+ gemspec
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- # rolify [![Gem Version](https://badge.fury.io/rb/rolify.png)](http://badge.fury.io/rb/rolify) [![build status](https://secure.travis-ci.org/EppO/rolify.png?branch=master)](http://travis-ci.org/EppO/rolify) [![dependency status](https://gemnasium.com/EppO/rolify.png)](https://gemnasium.com/EppO/rolify) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/EppO/rolify)
1
+ # rolify [![Gem Version](https://badge.fury.io/rb/rolify.png)](http://badge.fury.io/rb/rolify) [![build status](https://secure.travis-ci.org/EppO/rolify.png)](http://travis-ci.org/EppO/rolify) [![dependency status](https://gemnasium.com/EppO/rolify.png)](https://gemnasium.com/EppO/rolify) [![Code Climate](https://codeclimate.com/github/EppO/rolify.png)](https://codeclimate.com/github/EppO/rolify) [![Coverage Status](https://coveralls.io/repos/EppO/rolify/badge.png?branch=master)](https://coveralls.io/r/EppO/rolify)
2
+
2
3
 
3
4
  Very simple Roles library without any authorization enforcement supporting scope on resource object.
4
5
 
@@ -15,23 +16,17 @@ This library can be easily integrated with any authentication gem ([devise](http
15
16
 
16
17
  ## Requirements
17
18
 
18
- * Rails >= 3.1
19
- * ActiveRecord >= 3.1 <b>or</b> Mongoid >= 3.0
20
- * supports ruby 1.9, JRuby 1.6.0+ (in 1.9 mode) and Rubinius 2.0.0dev (in 1.9 mode)
21
- * support of ruby 1.8 has been dropped due to Mongoid 3.0 that only supports 1.9 new hash syntax
19
+ * Rails >= 3.2
20
+ * ActiveRecord >= 3.2 <b>or</b> Mongoid >= 3.1
21
+ * supports ruby 2.0/1.9.3, JRuby 1.6.0+ (in 1.9 mode) and Rubinius 2.0.0dev (in 1.9 mode)
22
+ * support of ruby 1.8 has been dropped due to Mongoid >=3.0 that only supports 1.9 new hash syntax
22
23
 
23
24
  ## Installation
24
25
 
25
26
  In <b>Rails 3</b>, add this to your Gemfile and run the +bundle+ command.
26
27
 
27
28
  ```ruby
28
- gem "rolify"
29
- ```
30
-
31
- Alternatively, you can install it as a plugin.
32
-
33
- ```
34
- rails plugin install git://github.com/EppO/rolify.git
29
+ gem "rolify"
35
30
  ```
36
31
 
37
32
  ## Getting Started
@@ -41,44 +36,50 @@ Alternatively, you can install it as a plugin.
41
36
  First, create your Role model and migration file using this generator:
42
37
 
43
38
  ```
44
- rails g rolify:role Role User
39
+ rails g rolify Role User
40
+ ```
41
+
42
+ **NB** for versions of Rolify prior to 3.3, use:
43
+
44
+ ```
45
+ rails g rolify:role Role User
45
46
  ```
46
47
 
47
48
  Role and User classes are the default. You can specify any Role class name you want. This is completly a new file so any name can do the job.
48
49
  For the User class name, you would probably use the one provided by your authentication solution. rolify just adds some class methods in an existing User class.
49
50
 
50
- If you want to use Mongoid instead of ActiveRecord, follow these [instructions](https://github.com/EppO/rolify/wiki/Configuration), and skip to step #3
51
+ If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3
51
52
 
52
53
  ### 2. Run the migration (only required when using ActiveRecord)
53
54
 
54
55
  Let's migrate !
55
56
 
56
57
  ```
57
- rake db:migrate
58
+ rake db:migrate
58
59
  ```
59
60
 
60
61
  ### 3.1 Configure your user model
61
62
 
62
- This gem adds the `rolify` method to your User class. You can also specify optional callbacks* on the user for when roles are added or removed:
63
+ This gem adds the `rolify` method to your User class. You can also specify optional callbacks on the User class for when roles are added or removed:
63
64
 
64
65
  ```ruby
65
- class User < ActiveRecord::Base
66
- rolify :before_add => :before_add_method
66
+ class User < ActiveRecord::Base
67
+ rolify :before_add => :before_add_method
67
68
 
68
- def :before_add_method(role)
69
- # do something before it gets added
70
- end
69
+ def before_add_method(role)
70
+ # do something before it gets added
71
71
  end
72
+ end
72
73
  ```
73
74
 
74
- The `rolify` method accepts the following callback* options:
75
+ The `rolify` method accepts the following callback options:
75
76
 
76
77
  - `before_add`
77
78
  - `after_add`
78
79
  - `before_remove`
79
80
  - `after_remove`
80
81
 
81
- *PLEASE NOTE: callbacks are currently only supported using ActiveRecord ORM. Mongoid will support association callbacks in its 3.1 release (Mongoid current version is 3.0.x)
82
+ Mongoid callbacks are also supported and works the same way.
82
83
 
83
84
  ### 3.2 Configure your resource models
84
85
 
@@ -96,22 +97,22 @@ end
96
97
  To define a global role:
97
98
 
98
99
  ```ruby
99
- user = User.find(1)
100
- user.add_role :admin
100
+ user = User.find(1)
101
+ user.add_role :admin
101
102
  ```
102
103
 
103
104
  To define a role scoped to a resource instance
104
105
 
105
106
  ```ruby
106
- user = User.find(2)
107
- user.add_role :moderator, Forum.first
107
+ user = User.find(2)
108
+ user.add_role :moderator, Forum.first
108
109
  ```
109
110
 
110
111
  To define a role scoped to a resource class
111
112
 
112
113
  ```ruby
113
- user = User.find(3)
114
- user.add_role :moderator, Forum
114
+ user = User.find(3)
115
+ user.add_role :moderator, Forum
115
116
  ```
116
117
 
117
118
  That's it !
@@ -121,45 +122,45 @@ That's it !
121
122
  To check if a user has a global role:
122
123
 
123
124
  ```ruby
124
- user = User.find(1)
125
- user.add_role :admin # sets a global role
126
- user.has_role? :admin
127
- => true
125
+ user = User.find(1)
126
+ user.add_role :admin # sets a global role
127
+ user.has_role? :admin
128
+ => true
128
129
  ```
129
130
 
130
131
  To check if a user has a role scoped to a resource instance:
131
132
 
132
133
  ```ruby
133
- user = User.find(2)
134
- user.add_role :moderator, Forum.first # sets a role scoped to a resource instance
135
- user.has_role? :moderator, Forum.first
136
- => true
137
- user.has_role? :moderator, Forum.last
138
- => false
134
+ user = User.find(2)
135
+ user.add_role :moderator, Forum.first # sets a role scoped to a resource instance
136
+ user.has_role? :moderator, Forum.first
137
+ => true
138
+ user.has_role? :moderator, Forum.last
139
+ => false
139
140
  ```
140
141
 
141
142
  To check if a user has a role scoped to a resource class:
142
143
 
143
144
  ```ruby
144
- user = User.find(3)
145
- user.add_role :moderator, Forum # sets a role scoped to a resource class
146
- user.has_role? :moderator, Forum
147
- => true
148
- user.has_role? :moderator, Forum.first
149
- => true
150
- user.has_role? :moderator, Forum.last
151
- => true
145
+ user = User.find(3)
146
+ user.add_role :moderator, Forum # sets a role scoped to a resource class
147
+ user.has_role? :moderator, Forum
148
+ => true
149
+ user.has_role? :moderator, Forum.first
150
+ => true
151
+ user.has_role? :moderator, Forum.last
152
+ => true
152
153
  ```
153
154
 
154
155
  A global role overrides resource role request:
155
156
 
156
157
  ```ruby
157
- user = User.find(4)
158
- user.add_role :moderator # sets a global role
159
- user.has_role? :moderator, Forum.first
160
- => true
161
- user.has_role? :moderator, Forum.last
162
- => true
158
+ user = User.find(4)
159
+ user.add_role :moderator # sets a global role
160
+ user.has_role? :moderator, Forum.first
161
+ => true
162
+ user.has_role? :moderator, Forum.last
163
+ => true
163
164
  ```
164
165
 
165
166
  ### 6. Resource roles querying
@@ -169,27 +170,30 @@ Starting from rolify 3.0, you can search roles on instance level or class level
169
170
  #### Instance level
170
171
 
171
172
  ```ruby
172
- forum = Forum.first
173
- forum.roles
174
- # => [ list of roles that are only binded to forum instance ]
175
- forum.applied_roles
176
- # => [ list of roles binded to forum instance and to the Forum class ]
173
+ forum = Forum.first
174
+ forum.roles
175
+ # => [ list of roles that are only binded to forum instance ]
176
+ forum.applied_roles
177
+ # => [ list of roles binded to forum instance and to the Forum class ]
177
178
  ```
178
179
 
179
180
  #### Class level
180
181
 
181
182
  ```ruby
182
- Forum.with_role(:admin)
183
- # => [ list of Forum instances that has role "admin" binded to it ]
184
- Forum.with_role(:admin, current_user)
185
- # => [ list of Forum instances that has role "admin" binded to it and belongs to current_user roles ]
186
-
187
- Forum.find_roles
188
- # => [ list of roles that binded to any Forum instance or to the Forum class ]
189
- Forum.find_roles(:admin)
190
- # => [ list of roles that binded to any Forum instance or to the Forum class with "admin" as a role name ]
191
- Forum.find_roles(:admin, current_user)
192
- # => [ list of roles that binded to any Forum instance or to the Forum class with "admin" as a role name and belongs to current_user roles ]
183
+ Forum.with_role(:admin)
184
+ # => [ list of Forum instances that has role "admin" binded to it ]
185
+ Forum.with_role(:admin, current_user)
186
+ # => [ list of Forum instances that has role "admin" binded to it and belongs to current_user roles ]
187
+
188
+ User.with_any_role(:user, :admin)
189
+ # => [ list of User instances that has role "admin" or "user" binded to it ]
190
+
191
+ Forum.find_roles
192
+ # => [ list of roles that binded to any Forum instance or to the Forum class ]
193
+ Forum.find_roles(:admin)
194
+ # => [ list of roles that binded to any Forum instance or to the Forum class with "admin" as a role name ]
195
+ Forum.find_roles(:admin, current_user)
196
+ # => [ list of roles that binded to any Forum instance or to the Forum class with "admin" as a role name and belongs to current_user roles ]
193
197
  ```
194
198
 
195
199
  ## Resources
@@ -201,6 +205,16 @@ Starting from rolify 3.0, you can search roles on instance level or class level
201
205
  * [Using rolify with Devise and Authority](https://github.com/EppO/rolify/wiki/Using-rolify-with-Devise-and-Authority)
202
206
  * [Step-by-step tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/)
203
207
 
208
+ ## Upgrade from previous versions
209
+
210
+ Please read the [upgrade instructions](UPGRADE.rdoc).
211
+
212
+ ## Known issues
213
+
214
+ If you are using Mongoid and/or less-rails gem, please read [this](https://github.com/EppO/rolify/wiki/FAQ#when-i-start-rails-using-server-console-whatever-i-get-this-error)
215
+
204
216
  ## Questions or Problems?
205
217
 
206
- If you have any issue or feature request with/for rolify, please add an [issue on GitHub](https://github.com/EppO/rolify/issues) or fork the project and send a pull request.
218
+ If you have any issue or feature request with/for rolify, please create an new [issue on GitHub](https://github.com/EppO/rolify/issues) **specifying the ruby runtime, rails and rolify versions you're using and the gems listed in your Gemfile**, or fork the project and send a pull request.
219
+
220
+ To get the specs running you should call `bundle` and then `rake`. See the spec/README for more information.
data/Rakefile CHANGED
@@ -1,7 +1,20 @@
1
1
  require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+ require 'coveralls/rake/task'
4
+
2
5
  Bundler::GemHelper.install_tasks
3
6
 
4
- task :default => :spec
7
+ Coveralls::RakeTask.new
8
+
9
+ RSpec::Core::RakeTask.new(:generators) do |task|
10
+ task.pattern = "spec/generators/**/*_spec.rb"
11
+ end
12
+
13
+ RSpec::Core::RakeTask.new(:rolify) do |task|
14
+ task.pattern = "spec/rolify/**/*_spec.rb"
15
+ end
16
+
17
+ task :default => [ :spec, 'coveralls:push' ]
5
18
 
6
19
  desc "Run all specs"
7
20
  task "spec" do
@@ -12,10 +25,10 @@ task "spec" do
12
25
  fail if return_code1 != 0 || return_code2 != 0
13
26
  end
14
27
 
15
- task "generators" do
16
- system "bundle exec rspec spec/generators"
17
- end
18
-
19
- task "rolify" do
20
- system "bundle exec rspec spec/rolify"
21
- end
28
+ desc "Run specs for all adapters"
29
+ task :spec_all do
30
+ %w[active_record mongoid].each do |model_adapter|
31
+ puts "ADAPTER = #{model_adapter}"
32
+ system "ADAPTER=#{model_adapter} rake"
33
+ end
34
+ end
data/UPGRADE.rdoc CHANGED
@@ -20,3 +20,25 @@ If you use <i>dynamic methods</i> (<tt>user.is_admin?</tt> like methods), you sh
20
20
  c.use_dynamic_shortcuts
21
21
  end
22
22
  The old fashion way still works though, but won't work anymore if the setter method name is changed in a possible future. You've been warned :-)
23
+
24
+ == From a rolify installation 3.x
25
+
26
+ === Dependencies:
27
+
28
+ Starting from 3.3 release, rolify supports Rails 3.2 and newer.
29
+
30
+ Mongoid callbacks are supported if Mongoid 3.1 and newer is installed.
31
+
32
+ === Rails Generators
33
+
34
+ Role model template when using Mongoid has been changed, you should re-run the generator.
35
+
36
+ Rails generator has been renamed to: <tt>rails g rolify</tt> and arguments have been changed:
37
+ * Role class name is now mandatory, User class name remains optional and its default is still <tt>User</tt>
38
+ * ORM optional argument is now <tt>-o</tt> or <tt>--orm=</tt>
39
+ For instance, here is a new rolify generator command example: <tt>rails g rolify Role</tt>
40
+ You can use <tt>rails g rolify --help</tt> to see all available options.
41
+
42
+ === Testing
43
+
44
+ Starting from rolify 3.3, to run the specs you should run: <tt>rake spec</tt> or simply <tt>rake</tt>.
@@ -0,0 +1,21 @@
1
+ source "https://rubygems.org"
2
+
3
+ case ENV["ADAPTER"]
4
+ when nil, "active_record"
5
+ group :test do
6
+ gem "activerecord-jdbcsqlite3-adapter", ">= 1.3.0.rc", :platform => "jruby"
7
+ gem "sqlite3", :platform => "ruby"
8
+ end
9
+ gem "activerecord", "~> 3.2.0", :require => "active_record"
10
+ when "mongoid"
11
+ gem "mongoid", "~> 3.1"
12
+ gem "bson_ext", :platform => "ruby"
13
+ else
14
+ raise "Unknown model adapter: #{ENV["ADAPTER"]}"
15
+ end
16
+
17
+ group :test do
18
+ gem 'coveralls', :require => false
19
+ end
20
+
21
+ gemspec :path => '../'
@@ -0,0 +1,27 @@
1
+ source "https://rubygems.org"
2
+
3
+ case ENV["ADAPTER"]
4
+ when nil, "active_record"
5
+ gem "activerecord", "~> 4.0.0.rc", :require => "active_record"
6
+ when "mongoid"
7
+ gem "mongoid", :git => "git://github.com/mongoid/mongoid.git"
8
+ gem "bson_ext", :platform => "ruby"
9
+ else
10
+ raise "Unknown model adapter: #{ENV["ADAPTER"]}"
11
+ end
12
+
13
+ group :test do
14
+ gem "rails", "~> 4.0.0.rc"
15
+ gem "ammeter"
16
+ gem "rake"
17
+ gem "rspec"
18
+ gem "rspec-rails"
19
+ gem "fuubar"
20
+ gem "bundler"
21
+
22
+ gem "activerecord-jdbcsqlite3-adapter", "~> 1.3.0.beta", :platform => "jruby"
23
+ gem "sqlite3", :platform => "ruby"
24
+ gem "activerecord", "~> 4.0.0.rc", :require => "active_record"
25
+ gem "mongoid", :git => "git://github.com/mongoid/mongoid.git"
26
+ gem 'coveralls', :require => false
27
+ end
@@ -44,7 +44,7 @@ module Mongoid
44
44
 
45
45
  scopify
46
46
  RUBY
47
- content % { :user_cname => user_cname.constantize.table_name }
47
+ content % { :user_cname => user_cname.constantize.collection_name }
48
48
  end
49
49
  end
50
50
  end
@@ -4,7 +4,7 @@ An initializer file has been created here: config/initializers/rolify.rb, you
4
4
  can change rolify settings to match your needs.
5
5
  Defaults values are commented out.
6
6
 
7
- A Role class has been been created in app/models (with the name you gave as
7
+ A Role class has been created in app/models (with the name you gave as
8
8
  argument otherwise the default is role.rb), you can add your own business logic
9
9
  inside.
10
10
 
@@ -1,25 +1,26 @@
1
1
  require 'rolify/adapters/base'
2
2
 
3
3
  module Rolify
4
- module Adapter
4
+ module Adapter
5
5
  class ResourceAdapter < ResourceAdapterBase
6
6
  def resources_find(roles_table, relation, role_name)
7
- resources = relation.joins("INNER JOIN #{quote(roles_table)} ON #{quote(roles_table)}.resource_type = '#{relation.to_s}' AND
8
- (#{quote(roles_table)}.resource_id IS NULL OR #{quote(roles_table)}.resource_id = #{quote(relation.table_name)}.id)")
7
+ resources = relation.joins("INNER JOIN #{quote(roles_table)} ON #{quote(roles_table)}.resource_type = '#{relation.to_s}' AND
8
+ (#{quote(roles_table)}.resource_id IS NULL OR #{quote(roles_table)}.resource_id = #{quote(relation.table_name)}.#{relation.primary_key})")
9
9
  resources = resources.where("#{quote(roles_table)}.name IN (?) AND #{quote(roles_table)}.resource_type = ?", Array(role_name), relation.to_s)
10
+ resources = resources.select("#{quote(relation.table_name)}.*")
10
11
  resources
11
12
  end
12
13
 
13
14
  def in(relation, user, role_names)
14
15
  roles = user.roles.where(:name => role_names)
15
- relation.where("#{quote(role_class.table_name)}.id IN (?) AND ((resource_id = #{quote(relation.table_name)}.id) OR (resource_id IS NULL))", roles)
16
+ relation.where("#{quote(role_class.table_name)}.#{role_class.primary_key} IN (?) AND ((resource_id = #{quote(relation.table_name)}.#{relation.primary_key}) OR (resource_id IS NULL))", roles)
16
17
  end
17
-
18
+
18
19
  private
19
-
20
+
20
21
  def quote(column)
21
22
  ActiveRecord::Base.connection.quote_column_name column
22
23
  end
23
24
  end
24
25
  end
25
- end
26
+ end
@@ -1,7 +1,7 @@
1
1
  require 'rolify/adapters/base'
2
2
 
3
3
  module Rolify
4
- module Adapter
4
+ module Adapter
5
5
  class RoleAdapter < RoleAdapterBase
6
6
  def where(relation, *args)
7
7
  conditions, values = build_conditions(relation, args)
@@ -9,7 +9,7 @@ module Rolify
9
9
  end
10
10
 
11
11
  def find_or_create_by(role_name, resource_type = nil, resource_id = nil)
12
- role_class.find_or_create_by_name_and_resource_type_and_resource_id(role_name, resource_type, resource_id)
12
+ role_class.where(:name => role_name, :resource_type => resource_type, :resource_id => resource_id).first_or_create
13
13
  end
14
14
 
15
15
  def add(relation, role)
@@ -23,8 +23,8 @@ module Rolify
23
23
  roles = relation.roles.where(cond)
24
24
  if roles
25
25
  relation.roles.delete(roles)
26
- roles.each do |role|
27
- role.destroy if role.send(ActiveSupport::Inflector.demodulize(user_class).tableize.to_sym).empty?
26
+ roles.each do |role|
27
+ role.destroy if role.send(ActiveSupport::Inflector.demodulize(user_class).tableize.to_sym).empty?
28
28
  end
29
29
  end
30
30
  roles
@@ -33,9 +33,13 @@ module Rolify
33
33
  def exists?(relation, column)
34
34
  relation.where("#{column} IS NOT NULL")
35
35
  end
36
-
36
+
37
37
  def scope(relation, conditions)
38
- query = relation.scoped
38
+ if Rails.version < "4.0"
39
+ query = relation.scoped
40
+ else
41
+ query = relation.all
42
+ end
39
43
  query = query.joins(:roles)
40
44
  query = where(query, conditions)
41
45
  query
@@ -67,10 +71,10 @@ module Rolify
67
71
  values = [ role ]
68
72
  if resource
69
73
  query.insert(0, "(")
70
- query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id IS NULL))"
74
+ query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id IS NULL))"
71
75
  values << role << (resource.is_a?(Class) ? resource.to_s : resource.class.name)
72
76
  if !resource.is_a? Class
73
- query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id = ?))"
77
+ query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id = ?))"
74
78
  values << role << resource.class.name << resource.id
75
79
  end
76
80
  query += ")"
@@ -79,4 +83,4 @@ module Rolify
79
83
  end
80
84
  end
81
85
  end
82
- end
86
+ end
@@ -45,7 +45,7 @@ module Rolify
45
45
  role_cnames = [ "Role" ] if role_cnames.empty?
46
46
  role_cnames.each do |role_cname|
47
47
  role_class = role_cname.constantize
48
- if role_class.superclass == ActiveRecord && !role_class.table_exists?
48
+ if role_class.superclass.to_s == "ActiveRecord" && !role_class.table_exists?
49
49
  warn "[WARN] table '#{role_cname}' doesn't exist. Did you run the migration ? Ignoring rolify config."
50
50
  return false
51
51
  end
@@ -3,7 +3,7 @@ require 'rails'
3
3
 
4
4
  module Rolify
5
5
  class Railtie < Rails::Railtie
6
- initializer 'rolify.initialize' do
6
+ initializer 'rolify.initialize', :after do
7
7
  ActiveSupport.on_load(:active_record) do
8
8
  ActiveRecord::Base.send :extend, Rolify
9
9
  end
data/lib/rolify/role.rb CHANGED
@@ -64,11 +64,9 @@ module Rolify
64
64
 
65
65
  def method_missing(method, *args, &block)
66
66
  if method.to_s.match(/^is_(\w+)_of[?]$/) || method.to_s.match(/^is_(\w+)[?]$/)
67
- if self.class.role_class.where(:name => $1).count > 0
68
- resource = args.first
69
- self.class.define_dynamic_method $1, resource
70
- return has_role?("#{$1}", resource)
71
- end
67
+ resource = args.first
68
+ self.class.define_dynamic_method $1, resource
69
+ return has_role?("#{$1}", resource)
72
70
  end unless !Rolify.dynamic_shortcuts
73
71
  super
74
72
  end
@@ -1,3 +1,3 @@
1
1
  module Rolify
2
- VERSION = "3.3.0.rc3"
2
+ VERSION = "3.3.0"
3
3
  end