rolify 3.2.0 → 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 (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.travis.yml +13 -10
  4. data/CHANGELOG.rdoc +18 -0
  5. data/Gemfile +20 -3
  6. data/README.md +91 -73
  7. data/Rakefile +27 -2
  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/active_record/rolify_generator.rb +50 -0
  12. data/lib/generators/active_record/templates/README +8 -0
  13. data/lib/generators/active_record/templates/migration.rb +19 -0
  14. data/lib/generators/mongoid/rolify_generator.rb +51 -0
  15. data/lib/generators/mongoid/templates/README-mongoid +4 -0
  16. data/lib/generators/rolify/rolify_generator.rb +35 -0
  17. data/lib/generators/rolify/{role/templates/README-mongoid → templates/README} +1 -5
  18. data/lib/generators/rolify/{role/templates → templates}/initializer.rb +2 -2
  19. data/lib/generators/rolify/templates/role-active_record.rb +11 -0
  20. data/lib/generators/rolify/{role/templates → templates}/role-mongoid.rb +0 -2
  21. data/lib/generators/rolify/user_generator.rb +39 -0
  22. data/lib/rolify/adapters/active_record/resource_adapter.rb +8 -7
  23. data/lib/rolify/adapters/active_record/role_adapter.rb +17 -12
  24. data/lib/rolify/adapters/base.rb +1 -1
  25. data/lib/rolify/adapters/mongoid/resource_adapter.rb +2 -2
  26. data/lib/rolify/adapters/mongoid/role_adapter.rb +24 -14
  27. data/lib/rolify/configure.rb +16 -1
  28. data/lib/rolify/finders.rb +17 -16
  29. data/lib/rolify/matchers.rb +13 -0
  30. data/lib/rolify/railtie.rb +1 -1
  31. data/lib/rolify/resource.rb +7 -2
  32. data/lib/rolify/role.rb +5 -7
  33. data/lib/rolify/version.rb +1 -1
  34. data/lib/rolify.rb +21 -15
  35. data/rolify.gemspec +5 -11
  36. data/spec/README.rdoc +24 -0
  37. data/spec/generators/rolify/{role/role_generator_spec.rb → rolify_activerecord_generator_spec.rb} +49 -39
  38. data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
  39. data/spec/generators_helper.rb +21 -0
  40. data/spec/rolify/custom_spec.rb +10 -17
  41. data/spec/rolify/matchers_spec.rb +24 -0
  42. data/spec/rolify/namespace_spec.rb +24 -0
  43. data/spec/rolify/resource_spec.rb +56 -31
  44. data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
  45. data/spec/rolify/role_spec.rb +9 -16
  46. data/spec/rolify/shared_contexts.rb +9 -3
  47. data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +2 -2
  48. data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +12 -4
  49. data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +45 -4
  50. data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +3 -3
  51. data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +3 -3
  52. data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +59 -59
  53. data/spec/rolify/shared_examples/shared_examples_for_roles.rb +7 -5
  54. data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +10 -10
  55. data/spec/spec_helper.rb +6 -2
  56. data/spec/support/adapters/active_record.rb +47 -9
  57. data/spec/support/adapters/mongoid.rb +85 -24
  58. data/spec/support/data.rb +15 -15
  59. data/spec/support/schema.rb +28 -17
  60. metadata +102 -83
  61. data/lib/generators/rolify/role/role_generator.rb +0 -49
  62. data/lib/generators/rolify/role/templates/README +0 -11
  63. data/lib/generators/rolify/role/templates/README-active_record +0 -21
  64. data/lib/generators/rolify/role/templates/migration.rb +0 -19
  65. data/lib/generators/rolify/role/templates/role-active_record.rb +0 -6
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,14 +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
14
+
15
+ services: mongodb
10
16
 
11
- before_install:
12
- - gem update --system
13
- - gem --version
14
- - ruby -v
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 [![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)
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
 
@@ -9,27 +10,23 @@ user.has_role?(:moderator, Forum.first)
9
10
  => false # if user is moderator of another Forum
10
11
  ```
11
12
 
12
- This library was intended to be used with [CanCan](https://github.com/ryanb/cancan) and [devise](https://github.com/plataformatec/devise) but should be generic enough to be used by any other authentication/authorization solutions.
13
+ This library can be easily integrated with any authentication gem ([devise](https://github.com/plataformatec/devise), [Authlogic](https://github.com/binarylogic/authlogic), [Clearance](https://github.com/thoughtbot/clearance)) and authorization gem<span style="color: red"><strong>*</strong></span> ([CanCan](https://github.com/ryanb/cancan), [authority](https://github.com/nathanl/authority))
14
+
15
+ <span style="color: red"><strong>*</strong></span>: authorization gem that doesn't provide a role class
13
16
 
14
17
  ## Requirements
15
18
 
16
- * Rails >= 3.1
17
- * ActiveRecord >= 3.1 <b>or</b> Mongoid >= 3.0
18
- * supports ruby 1.9, JRuby 1.6.0+ (in 1.9 mode) and Rubinius 2.0.0dev (in 1.9 mode)
19
- * 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
20
23
 
21
24
  ## Installation
22
25
 
23
26
  In <b>Rails 3</b>, add this to your Gemfile and run the +bundle+ command.
24
27
 
25
28
  ```ruby
26
- gem "rolify"
27
- ```
28
-
29
- Alternatively, you can install it as a plugin.
30
-
31
- ```
32
- rails plugin install git://github.com/EppO/rolify.git
29
+ gem "rolify"
33
30
  ```
34
31
 
35
32
  ## Getting Started
@@ -39,44 +36,50 @@ Alternatively, you can install it as a plugin.
39
36
  First, create your Role model and migration file using this generator:
40
37
 
41
38
  ```
42
- 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
43
46
  ```
44
47
 
45
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.
46
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.
47
50
 
48
- 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
49
52
 
50
53
  ### 2. Run the migration (only required when using ActiveRecord)
51
54
 
52
55
  Let's migrate !
53
56
 
54
57
  ```
55
- rake db:migrate
58
+ rake db:migrate
56
59
  ```
57
60
 
58
61
  ### 3.1 Configure your user model
59
62
 
60
- 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:
61
64
 
62
65
  ```ruby
63
- class User < ActiveRecord::Base
64
- rolify :before_add => :before_add_method
66
+ class User < ActiveRecord::Base
67
+ rolify :before_add => :before_add_method
65
68
 
66
- def :before_add_method(role)
67
- # do something before it gets added
68
- end
69
+ def before_add_method(role)
70
+ # do something before it gets added
69
71
  end
72
+ end
70
73
  ```
71
74
 
72
- The `rolify` method accepts the following callback* options:
75
+ The `rolify` method accepts the following callback options:
73
76
 
74
77
  - `before_add`
75
78
  - `after_add`
76
79
  - `before_remove`
77
80
  - `after_remove`
78
81
 
79
- *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.
80
83
 
81
84
  ### 3.2 Configure your resource models
82
85
 
@@ -94,22 +97,22 @@ end
94
97
  To define a global role:
95
98
 
96
99
  ```ruby
97
- user = User.find(1)
98
- user.add_role :admin
100
+ user = User.find(1)
101
+ user.add_role :admin
99
102
  ```
100
103
 
101
104
  To define a role scoped to a resource instance
102
105
 
103
106
  ```ruby
104
- user = User.find(2)
105
- user.add_role :moderator, Forum.first
107
+ user = User.find(2)
108
+ user.add_role :moderator, Forum.first
106
109
  ```
107
110
 
108
111
  To define a role scoped to a resource class
109
112
 
110
113
  ```ruby
111
- user = User.find(3)
112
- user.add_role :moderator, Forum
114
+ user = User.find(3)
115
+ user.add_role :moderator, Forum
113
116
  ```
114
117
 
115
118
  That's it !
@@ -119,45 +122,45 @@ That's it !
119
122
  To check if a user has a global role:
120
123
 
121
124
  ```ruby
122
- user = User.find(1)
123
- user.add_role :admin # sets a global role
124
- user.has_role? :admin
125
- => true
125
+ user = User.find(1)
126
+ user.add_role :admin # sets a global role
127
+ user.has_role? :admin
128
+ => true
126
129
  ```
127
130
 
128
131
  To check if a user has a role scoped to a resource instance:
129
132
 
130
133
  ```ruby
131
- user = User.find(2)
132
- user.add_role :moderator, Forum.first # sets a role scoped to a resource instance
133
- user.has_role? :moderator, Forum.first
134
- => true
135
- user.has_role? :moderator, Forum.last
136
- => 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
137
140
  ```
138
141
 
139
142
  To check if a user has a role scoped to a resource class:
140
143
 
141
144
  ```ruby
142
- user = User.find(3)
143
- user.add_role :moderator, Forum # sets a role scoped to a resource class
144
- user.has_role? :moderator, Forum
145
- => true
146
- user.has_role? :moderator, Forum.first
147
- => true
148
- user.has_role? :moderator, Forum.last
149
- => 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
150
153
  ```
151
154
 
152
155
  A global role overrides resource role request:
153
156
 
154
157
  ```ruby
155
- user = User.find(4)
156
- user.add_role :moderator # sets a global role
157
- user.has_role? :moderator, Forum.first
158
- => true
159
- user.has_role? :moderator, Forum.last
160
- => 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
161
164
  ```
162
165
 
163
166
  ### 6. Resource roles querying
@@ -167,36 +170,51 @@ Starting from rolify 3.0, you can search roles on instance level or class level
167
170
  #### Instance level
168
171
 
169
172
  ```ruby
170
- forum = Forum.first
171
- forum.roles
172
- # => [ list of roles that are only binded to forum instance ]
173
- forum.applied_roles
174
- # => [ 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 ]
175
178
  ```
176
179
 
177
180
  #### Class level
178
181
 
179
182
  ```ruby
180
- Forum.with_role(:admin)
181
- # => [ list of Forum instances that has role "admin" binded to it ]
182
- Forum.with_role(:admin, current_user)
183
- # => [ list of Forum instances that has role "admin" binded to it and belongs to current_user roles ]
184
-
185
- Forum.find_roles
186
- # => [ list of roles that binded to any Forum instance or to the Forum class ]
187
- Forum.find_roles(:admin)
188
- # => [ list of roles that binded to any Forum instance or to the Forum class with "admin" as a role name ]
189
- Forum.find_roles(:admin, current_user)
190
- # => [ 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 ]
191
197
  ```
192
198
 
193
199
  ## Resources
194
200
 
195
201
  * [Wiki](https://github.com/EppO/rolify/wiki)
196
202
  * [Usage](https://github.com/EppO/rolify/wiki/Usage): all the available commands
197
- * [Tutorial](https://github.com/EppO/rolify/wiki/Tutorial): how to use [rolify](http://eppo.github.com/rolify) with [Devise](https://github.com/plataformatec/devise) and [CanCan](https://github.com/ryanb/cancan).
198
- * [Amazing tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/)
203
+ * [Tutorials](https://github.com/EppO/rolify/wiki#wiki-tutorials):
204
+ * [How-To use rolify with Devise and CanCan](https://github.com/EppO/rolify/wiki/Tutorial)
205
+ * [Using rolify with Devise and Authority](https://github.com/EppO/rolify/wiki/Using-rolify-with-Devise-and-Authority)
206
+ * [Step-by-step tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/)
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)
199
215
 
200
216
  ## Questions or Problems?
201
217
 
202
- 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,9 +1,34 @@
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
8
- exec "bundle exec rspec spec"
21
+ Rake::Task['generators'].invoke
22
+ return_code1 = $?.exitstatus
23
+ Rake::Task['rolify'].invoke
24
+ return_code2 = $?.exitstatus
25
+ fail if return_code1 != 0 || return_code2 != 0
9
26
  end
27
+
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
@@ -0,0 +1,50 @@
1
+ require 'rails/generators/active_record'
2
+ require 'active_support/core_ext'
3
+
4
+ module ActiveRecord
5
+ module Generators
6
+ class RolifyGenerator < ActiveRecord::Generators::Base
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ argument :user_cname, :type => :string, :default => "User", :banner => "User"
10
+
11
+ def generate_model
12
+ invoke "active_record:model", [ name ], :migration => false
13
+ end
14
+
15
+ def inject_role_class
16
+ inject_into_class(model_path, class_name, model_content)
17
+ end
18
+
19
+ def copy_rolify_migration
20
+ migration_template "migration.rb", "db/migrate/rolify_create_#{table_name}"
21
+ end
22
+
23
+ def join_table
24
+ user_cname.constantize.table_name + "_" + table_name
25
+ end
26
+
27
+ def user_reference
28
+ user_cname.demodulize.underscore
29
+ end
30
+
31
+ def role_reference
32
+ class_name.demodulize.underscore
33
+ end
34
+
35
+ def model_path
36
+ File.join("app", "models", "#{file_path}.rb")
37
+ end
38
+
39
+ def model_content
40
+ content = <<RUBY
41
+ has_and_belongs_to_many :%{user_cname}, :join_table => :%{join_table}
42
+ belongs_to :resource, :polymorphic => true
43
+
44
+ scopify
45
+ RUBY
46
+ content % { :user_cname => user_cname.constantize.table_name, :join_table => "#{user_cname.constantize.table_name}_#{table_name}"}
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ Now, you just have to run the migration using rake command:
2
+
3
+ rake db:migrate
4
+
5
+ and you will be able to add the resourcify method inside all models you want
6
+ scoped by a role.
7
+
8
+ ===============================================================================
@@ -0,0 +1,19 @@
1
+ class RolifyCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ create_table(:<%= table_name %>) do |t|
4
+ t.string :name
5
+ t.references :resource, :polymorphic => true
6
+
7
+ t.timestamps
8
+ end
9
+
10
+ create_table(:<%= join_table %>, :id => false) do |t|
11
+ t.references :<%= user_reference %>
12
+ t.references :<%= role_reference %>
13
+ end
14
+
15
+ add_index(:<%= table_name %>, :name)
16
+ add_index(:<%= table_name %>, [ :name, :resource_type, :resource_id ])
17
+ add_index(:<%= join_table %>, [ :<%= user_reference %>_id, :<%= role_reference %>_id ])
18
+ end
19
+ end
@@ -0,0 +1,51 @@
1
+ require 'rails/generators/mongoid_generator'
2
+ require 'active_support/core_ext'
3
+
4
+ module Mongoid
5
+ module Generators
6
+ class RolifyGenerator < Rails::Generators::NamedBase
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ argument :user_cname, :type => :string, :default => "User", :banner => "User"
10
+
11
+ def generate_model
12
+ invoke "mongoid:model", [ name ]
13
+ end
14
+
15
+ def inject_role_class
16
+ inject_into_file(model_path, model_contents, :after => "include Mongoid::Document\n")
17
+ end
18
+
19
+ def user_reference
20
+ user_cname.demodulize.underscore
21
+ end
22
+
23
+ def role_reference
24
+ class_name.demodulize.underscore
25
+ end
26
+
27
+ def model_path
28
+ File.join("app", "models", "#{file_path}.rb")
29
+ end
30
+
31
+ def model_contents
32
+ content = <<RUBY
33
+ has_and_belongs_to_many :%{user_cname}
34
+ belongs_to :resource, :polymorphic => true
35
+
36
+ field :name, :type => String
37
+
38
+ index({
39
+ :name => 1,
40
+ :resource_type => 1,
41
+ :resource_id => 1
42
+ },
43
+ { :unique => true})
44
+
45
+ scopify
46
+ RUBY
47
+ content % { :user_cname => user_cname.constantize.collection_name }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,4 @@
1
+ Now, you are able to add the resourcify method inside all models you want
2
+ scoped by a role.
3
+
4
+ ===============================================================================
@@ -0,0 +1,35 @@
1
+ module Rolify
2
+ module Generators
3
+ class RolifyGenerator < Rails::Generators::NamedBase
4
+ Rails::Generators::ResourceHelpers
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ argument :user_cname, :type => :string, :default => "User"
8
+
9
+ namespace :rolify
10
+ hook_for :orm, :required => true
11
+
12
+ desc "Generates a model with the given NAME and a migration file."
13
+
14
+ def self.start(args, config)
15
+ user_cname = args.size > 1 ? args[1] : "User"
16
+ args.insert(1, user_cname) # 0 being the view name
17
+ super
18
+ end
19
+
20
+ def inject_user_class
21
+ invoke "rolify:user", [ user_cname, class_name ], :orm => options.orm
22
+ end
23
+
24
+ def copy_initializer_file
25
+ template "initializer.rb", "config/initializers/rolify.rb"
26
+ end
27
+
28
+ def show_readme
29
+ if behavior == :invoke
30
+ readme "README"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end