rolify 3.3.0.rc2 → 3.3.0.rc4
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.
- data/.gitignore +1 -0
- data/.travis.yml +18 -11
- data/CHANGELOG.rdoc +18 -0
- data/Gemfile +16 -3
- data/README.md +17 -17
- data/Rakefile +23 -1
- data/UPGRADE.rdoc +22 -0
- data/gemfiles/Gemfile.rails-3.2 +17 -0
- data/gemfiles/Gemfile.rails-4.0 +26 -0
- data/lib/generators/active_record/rolify_generator.rb +50 -0
- data/lib/generators/active_record/templates/migration.rb +19 -0
- data/lib/generators/mongoid/rolify_generator.rb +51 -0
- data/lib/generators/rolify/rolify_generator.rb +35 -0
- data/lib/generators/rolify/{role/templates → templates}/initializer.rb +2 -2
- data/lib/generators/rolify/templates/role-active_record.rb +11 -0
- data/lib/generators/rolify/user_generator.rb +39 -0
- data/lib/rolify/adapters/active_record/resource_adapter.rb +7 -7
- data/lib/rolify/adapters/active_record/role_adapter.rb +8 -8
- data/lib/rolify/configure.rb +1 -1
- data/lib/rolify/version.rb +1 -1
- data/lib/rolify.rb +1 -1
- data/rolify.gemspec +5 -12
- data/spec/README.rdoc +24 -0
- data/spec/generators/rolify/{role/role_generator_spec.rb → rolify_generator_spec.rb} +115 -13
- data/spec/generators_helper.rb +14 -0
- data/spec/rolify/custom_spec.rb +9 -16
- data/spec/rolify/namespace_spec.rb +9 -16
- data/spec/rolify/resource_spec.rb +13 -2
- data/spec/rolify/resourcifed_and_rolifed_spec.rb +5 -1
- data/spec/rolify/role_spec.rb +9 -16
- data/spec/rolify/shared_contexts.rb +9 -3
- data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +2 -2
- data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +1 -1
- data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +3 -3
- data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +3 -3
- data/spec/rolify/shared_examples/shared_examples_for_roles.rb +1 -1
- data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +10 -10
- data/spec/spec_helper.rb +1 -1
- data/spec/support/adapters/active_record.rb +7 -0
- data/spec/support/adapters/mongoid.rb +8 -4
- data/spec/support/data.rb +3 -0
- data/spec/support/schema.rb +5 -0
- metadata +38 -77
- data/lib/generators/rolify/role/role_generator.rb +0 -52
- data/lib/generators/rolify/role/templates/migration.rb +0 -19
- data/lib/generators/rolify/role/templates/role-active_record.rb +0 -6
- /data/lib/generators/{rolify/role/templates/README-active_record → active_record/templates/README} +0 -0
- /data/lib/generators/{rolify/role → mongoid}/templates/README-mongoid +0 -0
- /data/lib/generators/rolify/{role/templates → templates}/README +0 -0
- /data/lib/generators/rolify/{role/templates → templates}/role-mongoid.rb +0 -0
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
rvm:
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
9
|
-
|
|
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
|
|
18
|
+
|
|
19
|
+
matrix:
|
|
20
|
+
allow_failures:
|
|
21
|
+
- rvm: rbx-19mode
|
|
22
|
+
- rvm: jruby-19mode
|
|
23
|
+
- gemfile: gemfiles/Gemfile.rails-4.0
|
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,17 @@
|
|
|
1
|
-
source "
|
|
1
|
+
source "https://rubygems.org"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
case ENV["ADAPTER"]
|
|
4
|
+
when nil, "active_record"
|
|
5
|
+
group :test do
|
|
6
|
+
gem "activerecord-jdbcsqlite3-adapter", :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
|
+
gemspec
|
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# rolify [](http://badge.fury.io/rb/rolify) [](http://badge.fury.io/rb/rolify) [](http://travis-ci.org/EppO/rolify) [](https://gemnasium.com/EppO/rolify) [](https://codeclimate.com/github/EppO/rolify)
|
|
2
2
|
|
|
3
3
|
Very simple Roles library without any authorization enforcement supporting scope on resource object.
|
|
4
4
|
|
|
@@ -15,10 +15,10 @@ This library can be easily integrated with any authentication gem ([devise](http
|
|
|
15
15
|
|
|
16
16
|
## Requirements
|
|
17
17
|
|
|
18
|
-
* Rails >= 3.
|
|
19
|
-
* ActiveRecord >= 3.
|
|
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
|
|
18
|
+
* Rails >= 3.2
|
|
19
|
+
* ActiveRecord >= 3.2 <b>or</b> Mongoid >= 3.1
|
|
20
|
+
* supports ruby 2.0/1.9.3, 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
|
|
22
22
|
|
|
23
23
|
## Installation
|
|
24
24
|
|
|
@@ -28,12 +28,6 @@ In <b>Rails 3</b>, add this to your Gemfile and run the +bundle+ command.
|
|
|
28
28
|
gem "rolify"
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
Alternatively, you can install it as a plugin.
|
|
32
|
-
|
|
33
|
-
```
|
|
34
|
-
rails plugin install git://github.com/EppO/rolify.git
|
|
35
|
-
```
|
|
36
|
-
|
|
37
31
|
## Getting Started
|
|
38
32
|
|
|
39
33
|
### 1. Generate Role Model
|
|
@@ -41,13 +35,13 @@ Alternatively, you can install it as a plugin.
|
|
|
41
35
|
First, create your Role model and migration file using this generator:
|
|
42
36
|
|
|
43
37
|
```
|
|
44
|
-
rails g rolify
|
|
38
|
+
rails g rolify Role User
|
|
45
39
|
```
|
|
46
40
|
|
|
47
41
|
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
42
|
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
43
|
|
|
50
|
-
If you want to use Mongoid instead of ActiveRecord,
|
|
44
|
+
If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3
|
|
51
45
|
|
|
52
46
|
### 2. Run the migration (only required when using ActiveRecord)
|
|
53
47
|
|
|
@@ -59,26 +53,26 @@ Let's migrate !
|
|
|
59
53
|
|
|
60
54
|
### 3.1 Configure your user model
|
|
61
55
|
|
|
62
|
-
This gem adds the `rolify` method to your User class. You can also specify optional callbacks
|
|
56
|
+
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
57
|
|
|
64
58
|
```ruby
|
|
65
59
|
class User < ActiveRecord::Base
|
|
66
60
|
rolify :before_add => :before_add_method
|
|
67
61
|
|
|
68
|
-
def
|
|
62
|
+
def before_add_method(role)
|
|
69
63
|
# do something before it gets added
|
|
70
64
|
end
|
|
71
65
|
end
|
|
72
66
|
```
|
|
73
67
|
|
|
74
|
-
The `rolify` method accepts the following callback
|
|
68
|
+
The `rolify` method accepts the following callback options:
|
|
75
69
|
|
|
76
70
|
- `before_add`
|
|
77
71
|
- `after_add`
|
|
78
72
|
- `before_remove`
|
|
79
73
|
- `after_remove`
|
|
80
74
|
|
|
81
|
-
|
|
75
|
+
Mongoid callbacks are also supported and works the same way.
|
|
82
76
|
|
|
83
77
|
### 3.2 Configure your resource models
|
|
84
78
|
|
|
@@ -201,6 +195,12 @@ Starting from rolify 3.0, you can search roles on instance level or class level
|
|
|
201
195
|
* [Using rolify with Devise and Authority](https://github.com/EppO/rolify/wiki/Using-rolify-with-Devise-and-Authority)
|
|
202
196
|
* [Step-by-step tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/)
|
|
203
197
|
|
|
198
|
+
## Upgrade from previous versions
|
|
199
|
+
|
|
200
|
+
Please read the [upgrade instructions](UPGRADE.rdoc).
|
|
201
|
+
|
|
204
202
|
## Questions or Problems?
|
|
205
203
|
|
|
206
204
|
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.
|
|
205
|
+
|
|
206
|
+
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,31 @@
|
|
|
1
1
|
require 'bundler'
|
|
2
|
+
require 'rspec/core/rake_task'
|
|
3
|
+
|
|
2
4
|
Bundler::GemHelper.install_tasks
|
|
3
5
|
|
|
6
|
+
RSpec::Core::RakeTask.new(:generators) do |task|
|
|
7
|
+
task.pattern = "spec/generators/**/*_spec.rb"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
RSpec::Core::RakeTask.new(:rolify) do |task|
|
|
11
|
+
task.pattern = "spec/rolify/**/*_spec.rb"
|
|
12
|
+
end
|
|
13
|
+
|
|
4
14
|
task :default => :spec
|
|
5
15
|
|
|
6
16
|
desc "Run all specs"
|
|
7
17
|
task "spec" do
|
|
8
|
-
|
|
18
|
+
Rake::Task['generators'].invoke
|
|
19
|
+
return_code1 = $?.exitstatus
|
|
20
|
+
Rake::Task['rolify'].invoke
|
|
21
|
+
return_code2 = $?.exitstatus
|
|
22
|
+
fail if return_code1 != 0 || return_code2 != 0
|
|
9
23
|
end
|
|
24
|
+
|
|
25
|
+
desc "Run specs for all adapters"
|
|
26
|
+
task :spec_all do
|
|
27
|
+
%w[active_record mongoid].each do |model_adapter|
|
|
28
|
+
puts "ADAPTER = #{model_adapter}"
|
|
29
|
+
system "ADAPTER=#{model_adapter} rake"
|
|
30
|
+
end
|
|
31
|
+
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,17 @@
|
|
|
1
|
+
source "https://rubygems.org"
|
|
2
|
+
|
|
3
|
+
case ENV["ADAPTER"]
|
|
4
|
+
when nil, "active_record"
|
|
5
|
+
group :test do
|
|
6
|
+
gem "activerecord-jdbcsqlite3-adapter", :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
|
+
gemspec :path => '../'
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
source "https://rubygems.org"
|
|
2
|
+
|
|
3
|
+
case ENV["ADAPTER"]
|
|
4
|
+
when nil, "active_record"
|
|
5
|
+
gem "activerecord", "~> 4.0.0.beta1", :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.beta1"
|
|
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", :platform => "jruby"
|
|
23
|
+
gem "sqlite3", :platform => "ruby"
|
|
24
|
+
gem "activerecord", "~> 4.0.0.beta1", :require => "active_record"
|
|
25
|
+
gem "mongoid", :git => "git://github.com/mongoid/mongoid.git"
|
|
26
|
+
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,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.table_name }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -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
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Rolify.configure<%= "(\"#{
|
|
1
|
+
Rolify.configure<%= "(\"#{class_name.camelize.to_s}\")" if class_name != "Role" %> do |config|
|
|
2
2
|
# By default ORM adapter is ActiveRecord. uncomment to use mongoid
|
|
3
|
-
<%= "# " if
|
|
3
|
+
<%= "# " if options.orm == :active_record || !options.orm %>config.use_mongoid
|
|
4
4
|
|
|
5
5
|
# Dynamic shortcuts for User class (user.is_admin? like methods). Default is: false
|
|
6
6
|
# Enable this feature _after_ running rake db:migrate as it relies on the roles table
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
class <%= role_cname.camelize %> < ActiveRecord::Base
|
|
2
|
+
<% if need_table_prefix?(role_cname) %>
|
|
3
|
+
def self.table_name_prefix
|
|
4
|
+
<%= table_prefix(role_cname) %>_
|
|
5
|
+
end
|
|
6
|
+
<% end %>
|
|
7
|
+
has_and_belongs_to_many :<%= user_cname.tableize %>, :join_table => :<%= "#{table_name(user_cname, true)}_#{table_name(role_cname, true)}" %>
|
|
8
|
+
belongs_to :resource, :polymorphic => true
|
|
9
|
+
|
|
10
|
+
scopify
|
|
11
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'rails/generators/migration'
|
|
2
|
+
require 'active_support/core_ext'
|
|
3
|
+
|
|
4
|
+
module Rolify
|
|
5
|
+
module Generators
|
|
6
|
+
class UserGenerator < Rails::Generators::NamedBase
|
|
7
|
+
argument :role_cname, :type => :string, :default => "Role"
|
|
8
|
+
class_option :orm, :type => :string, :default => "active_record"
|
|
9
|
+
|
|
10
|
+
desc "Inject rolify method in the User class."
|
|
11
|
+
|
|
12
|
+
def inject_user_content
|
|
13
|
+
inject_into_file(model_path, :after => inject_rolify_method) do
|
|
14
|
+
" rolify#{role_association}\n"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def inject_rolify_method
|
|
19
|
+
if options.orm == :active_record
|
|
20
|
+
/class #{class_name.camelize}\n|class #{class_name.camelize} .*\n|class #{class_name.demodulize.camelize}\n|class #{class_name.demodulize.camelize} .*\n/
|
|
21
|
+
else
|
|
22
|
+
/include Mongoid::Document\n|include Mongoid::Document .*\n/
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def model_path
|
|
27
|
+
File.join("app", "models", "#{file_path}.rb")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def role_association
|
|
31
|
+
if role_cname != "Role"
|
|
32
|
+
" :role_cname => '#{role_cname.camelize}'"
|
|
33
|
+
else
|
|
34
|
+
""
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -1,25 +1,25 @@
|
|
|
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)}.
|
|
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
10
|
resources
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
def in(relation, user, role_names)
|
|
14
14
|
roles = user.roles.where(:name => role_names)
|
|
15
|
-
relation.where("#{quote(role_class.table_name)}.
|
|
15
|
+
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
16
|
end
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
private
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
def quote(column)
|
|
21
21
|
ActiveRecord::Base.connection.quote_column_name column
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
|
-
end
|
|
25
|
+
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.
|
|
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,7 +33,7 @@ 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
38
|
query = relation.scoped
|
|
39
39
|
query = query.joins(:roles)
|
|
@@ -67,10 +67,10 @@ module Rolify
|
|
|
67
67
|
values = [ role ]
|
|
68
68
|
if resource
|
|
69
69
|
query.insert(0, "(")
|
|
70
|
-
query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id IS NULL))"
|
|
70
|
+
query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id IS NULL))"
|
|
71
71
|
values << role << (resource.is_a?(Class) ? resource.to_s : resource.class.name)
|
|
72
72
|
if !resource.is_a? Class
|
|
73
|
-
query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id = ?))"
|
|
73
|
+
query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id = ?))"
|
|
74
74
|
values << role << resource.class.name << resource.id
|
|
75
75
|
end
|
|
76
76
|
query += ")"
|
|
@@ -79,4 +79,4 @@ module Rolify
|
|
|
79
79
|
end
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
|
-
end
|
|
82
|
+
end
|
data/lib/rolify/configure.rb
CHANGED
|
@@ -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
|
data/lib/rolify/version.rb
CHANGED
data/lib/rolify.rb
CHANGED
|
@@ -22,7 +22,7 @@ module Rolify
|
|
|
22
22
|
join_table = "#{self.to_s.tableize.gsub(/\//, "_")}_#{self.role_table_name}"
|
|
23
23
|
rolify_options = { :class_name => options[:role_cname].camelize }
|
|
24
24
|
rolify_options.merge!({ :join_table => join_table }) if Rolify.orm == "active_record"
|
|
25
|
-
rolify_options.merge!(options.reject{ |k,v| ![:before_add, :after_add, :before_remove, :after_remove].include? k.to_sym })
|
|
25
|
+
rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove ].include? k.to_sym })
|
|
26
26
|
|
|
27
27
|
has_and_belongs_to_many :roles, rolify_options
|
|
28
28
|
|
data/rolify.gemspec
CHANGED
|
@@ -15,23 +15,16 @@ Gem::Specification.new do |s|
|
|
|
15
15
|
s.rubyforge_project = s.name
|
|
16
16
|
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
|
18
|
-
s.test_files = `git ls-files --
|
|
18
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
20
20
|
s.require_paths = ["lib"]
|
|
21
21
|
|
|
22
|
-
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
|
|
23
|
-
s.add_development_dependency "activerecord-jdbcsqlite3-adapter"
|
|
24
|
-
else
|
|
25
|
-
s.add_development_dependency "sqlite3"
|
|
26
|
-
s.add_development_dependency "bson_ext" if RUBY_VERSION < "2.0.0"
|
|
27
|
-
end
|
|
28
|
-
s.add_development_dependency "activerecord", ">= 3.1.0"
|
|
29
|
-
s.add_development_dependency "mongoid", ">= 3.0"
|
|
30
22
|
s.add_development_dependency "ammeter"
|
|
31
23
|
s.add_development_dependency "rake"
|
|
32
24
|
s.add_development_dependency "rspec", ">= 2.0"
|
|
33
25
|
s.add_development_dependency "rspec-rails", ">= 2.0"
|
|
34
|
-
s.add_development_dependency "
|
|
35
|
-
s.add_development_dependency "
|
|
36
|
-
s.add_development_dependency "
|
|
26
|
+
s.add_development_dependency "bundler"
|
|
27
|
+
s.add_development_dependency "fuubar"
|
|
28
|
+
s.add_development_dependency "activerecord", ">= 3.2.0"
|
|
29
|
+
s.add_development_dependency "mongoid", ">= 3.1"
|
|
37
30
|
end
|