Skip to content

Commit

Permalink
Merge branch 'master' into with_any_role_AR
Browse files Browse the repository at this point in the history
  • Loading branch information
EppO authored Nov 20, 2017
2 parents d4276aa + dffb0f6 commit 899c41c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ before_install:

script: bundle exec rake

after_success:
- bundle exec codeclimate-test-reporter

matrix:
include:
- rvm: 2.3.0
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Very simple Roles library without any authorization enforcement supporting scope
Let's see an example:

```ruby
user.has_role?(:moderator, Forum.first)
user.has_role?(:moderator, @forum)
=> false # if user is moderator of another Forum
```

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> ([CanCanCan](https://github.com/CanCanCommunity/cancancan), [authority](https://github.com/nathanl/authority))
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> ([CanCanCan](https://github.com/CanCanCommunity/cancancan), [authority](https://github.com/nathanl/authority), [Pundit](https://github.com/elabs/pundit))

<span style="color: red"><strong>*</strong></span>: authorization gem that doesn't provide a role class

Expand All @@ -22,7 +22,7 @@ This library can be easily integrated with any authentication gem ([devise](http

## Installation

Add this to your Gemfile and run the +bundle+ command.
Add this to your Gemfile and run the `bundle` command.

```ruby
gem "rolify"
Expand All @@ -32,7 +32,9 @@ gem "rolify"

### 1. Generate Role Model

First, create your Role model and migration file using this generator:
First, use the generator to setup Rolify. Role and User class are the default names. However, you can specify any class name you want. For the User class name, you would probably use the one provided by your authentication solution.

If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3.

```
rails g rolify Role User
Expand All @@ -44,10 +46,7 @@ rails g rolify Role User
rails g rolify:role Role User
```

Role and User classes are the default. You can specify any Role class name you want. This is a completely new file so any name will do the job.
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.

If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3
The generator will create your Role model, add a migration file, and update your User class with new class methods.

### 2. Run the migration (only required when using ActiveRecord)

Expand Down Expand Up @@ -201,6 +200,8 @@ forum.applied_roles
```ruby
Forum.with_role(:admin)
# => [ list of Forum instances that have role "admin" bound to them ]
Forum.without_role(:admin)
# => [ list of Forum instances that do NOT have role "admin" bound to them ]
Forum.with_role(:admin, current_user)
# => [ list of Forum instances that have role "admin" bound to them and belong to current_user roles ]
Forum.with_roles([:admin, :user], current_user)
Expand Down
12 changes: 11 additions & 1 deletion lib/generators/active_record/rolify_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def inject_role_class
end

def copy_rolify_migration
migration_template "migration.rb", "db/migrate/rolify_create_#{table_name}.rb"
migration_template "migration.rb", "db/migrate/rolify_create_#{table_name}.rb", migration_version: migration_version
end

private
Expand Down Expand Up @@ -82,6 +82,16 @@ def prompt_missing_user
MSG
end

def rails5?
Rails.version.start_with? '5'
end

def migration_version
if rails5?
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
end
end

end
end
end
5 changes: 2 additions & 3 deletions lib/generators/active_record/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class RolifyCreate<%= table_name.camelize %> < ActiveRecord::Migration
class RolifyCreate<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
def change
create_table(:<%= table_name %>) do |t|
t.string :name
Expand All @@ -11,8 +11,7 @@ def change
t.references :<%= user_reference %>
t.references :<%= role_reference %>
end

add_index(:<%= table_name %>, :name)
<% if ActiveRecord::Base.connection.class.to_s.demodulize != 'PostgreSQLAdapter' %><%= "\n " %>add_index(:<%= table_name %>, :name)<% end %>
add_index(:<%= table_name %>, [ :name, :resource_type, :resource_id ])
add_index(:<%= join_table %>, [ :<%= user_reference %>_id, :<%= role_reference %>_id ])
end
Expand Down
61 changes: 61 additions & 0 deletions spec/generators/rolify/rolify_activerecord_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
destination File.expand_path("../../../../tmp", __FILE__)
teardown :cleanup_destination_root

let(:adapter) { 'SQLite3Adapter' }
before {
prepare_destination
}
Expand All @@ -20,6 +21,8 @@ def cleanup_destination_root
before(:all) { arguments %w(Role) }

before {
allow(ActiveRecord::Base).to receive_message_chain(
'connection.class.to_s.demodulize') { adapter }
capture(:stdout) {
generator.create_file "app/models/user.rb" do
<<-RUBY
Expand Down Expand Up @@ -81,13 +84,33 @@ class User < ActiveRecord::Base
it { should be_a_migration }
it { should contain "create_table(:roles) do" }
it { should contain "create_table(:users_roles, :id => false) do" }

context 'mysql2' do
let(:adapter) { 'Mysql2Adapter' }

it { expect(subject).to contain('add_index(:roles, :name)') }
end

context 'sqlite3' do
let(:adapter) { 'SQLite3Adapter' }

it { expect(subject).to contain('add_index(:roles, :name)') }
end

context 'pg' do
let(:adapter) { 'PostgreSQLAdapter' }

it { expect(subject).not_to contain('add_index(:roles, :name)') }
end
end
end

describe 'specifying User and Role class names' do
before(:all) { arguments %w(AdminRole AdminUser) }

before {
allow(ActiveRecord::Base).to receive_message_chain(
'connection.class.to_s.demodulize') { adapter }
capture(:stdout) {
generator.create_file "app/models/admin_user.rb" do
"class AdminUser < ActiveRecord::Base\nend"
Expand Down Expand Up @@ -136,13 +159,33 @@ class User < ActiveRecord::Base
it { should be_a_migration }
it { should contain "create_table(:admin_roles)" }
it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }

context 'mysql2' do
let(:adapter) { 'Mysql2Adapter' }

it { expect(subject).to contain('add_index(:admin_roles, :name)') }
end

context 'sqlite3' do
let(:adapter) { 'SQLite3Adapter' }

it { expect(subject).to contain('add_index(:admin_roles, :name)') }
end

context 'pg' do
let(:adapter) { 'PostgreSQLAdapter' }

it { expect(subject).not_to contain('add_index(:admin_roles, :name)') }
end
end
end

describe 'specifying namespaced User and Role class names' do
before(:all) { arguments %w(Admin::Role Admin::User) }

before {
allow(ActiveRecord::Base).to receive_message_chain(
'connection.class.to_s.demodulize') { adapter }
capture(:stdout) {
generator.create_file "app/models/admin/user.rb" do
<<-RUBY
Expand Down Expand Up @@ -197,6 +240,24 @@ class User < ActiveRecord::Base
it { should be_a_migration }
it { should contain "create_table(:admin_roles)" }
it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }

context 'mysql2' do
let(:adapter) { 'Mysql2Adapter' }

it { expect(subject).to contain('add_index(:admin_roles, :name)') }
end

context 'sqlite3' do
let(:adapter) { 'SQLite3Adapter' }

it { expect(subject).to contain('add_index(:admin_roles, :name)') }
end

context 'pg' do
let(:adapter) { 'PostgreSQLAdapter' }

it { expect(subject).not_to contain('add_index(:admin_roles, :name)') }
end
end
end
end
5 changes: 2 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "codeclimate-test-reporter"

CodeClimate::TestReporter.start
require "simplecov"
SimpleCov.start

require 'rubygems'
require "bundler/setup"
Expand Down

0 comments on commit 899c41c

Please sign in to comment.