rolify 5.1.0 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b81efc86ee3d4c660521e8b8cb7d1a5045b2ce7
4
- data.tar.gz: 84f19deb5072b9d46dcade6c8d5db085dd8cfb60
3
+ metadata.gz: fd31510680fda8c2bbb43eb49688f9d169d99780
4
+ data.tar.gz: bc4fd8fd415cc8c45b333c116f75c83a074c77e0
5
5
  SHA512:
6
- metadata.gz: d0209a0003ff41826654b23c1ebd6eeb556aef5d8e6554dd86a63362e82a69cc624abdc903063d65f4ab706a42a4c908cb52662c6db9cec1a8a38725f4274afe
7
- data.tar.gz: 8aee6f2b0fa61dfe9c04aa42c2a496b7b907cb74f44b4c542a6b0587f46686085f5a30457e4168ba1485ed24eb3dd089e17e99a66d86da21e8c724767a2bf5d1
6
+ metadata.gz: ca50a30cd8d60796b6be7d21b26d8bfc6841627623592899f778e56e144c75ff607d7b2263e691452e2613d3fe6f8cf950841fb52b6cf132feb033ad4dce8f71
7
+ data.tar.gz: 2d914c23c63900d12eded794a0b1ced812d0815987b42c31c9d83c784f4f7fcfbcb992efad429f533b2b2dc31e2da5d6bb39ba7d8063fc8e5170fd6f759ca6a8
@@ -8,6 +8,9 @@ before_install:
8
8
 
9
9
  script: bundle exec rake
10
10
 
11
+ after_success:
12
+ - bundle exec codeclimate-test-reporter
13
+
11
14
  matrix:
12
15
  include:
13
16
  - rvm: 2.3.0
data/Appraisals CHANGED
@@ -15,12 +15,12 @@ end
15
15
 
16
16
  appraise 'activerecord-3' do
17
17
  gem "sqlite3", :platform => "ruby"
18
- gem "activerecord", ">= 3.2.0", :require => "active_record"
18
+ gem "activerecord", "~> 3.2.0", :require => "active_record"
19
19
  end
20
20
 
21
21
  appraise 'activerecord-4' do
22
22
  gem "sqlite3", :platform => "ruby"
23
- gem "activerecord", ">= 4.2.5", :require => "active_record"
23
+ gem "activerecord", "~> 4.2.5", :require => "active_record"
24
24
  end
25
25
 
26
26
  appraise 'activerecord-5' do
@@ -1,3 +1,13 @@
1
+ = 5.2.0 (Dec 14, 2017)
2
+ * Fix regression in generator around belongs_to options compatibility
3
+ * Update version of database_cleaner
4
+ * Update initializer.rb to include the remove_role_if_empty option and description
5
+ * Allow inverse_of option on rolify method
6
+ * Fix the code-climate-reporter issue that's causing travis to fail
7
+ * Remove a separate index for the name column
8
+ * Fix migration generator for AR 5.x
9
+ * Fixed ambiguous column error
10
+
1
11
  = 5.1.0 (Mar 19, 2016)
2
12
  * Rails 5 Support (thanks @lorefnon)
3
13
  * Fix user handling in generator (thanks @lorefnon)
@@ -6,3 +6,12 @@ Mauro George (@MauroGeorge)
6
6
  Cheri Allen(@cherimarie)
7
7
  Gaurab Paul (@lorefnon)
8
8
  Dmitry Krakosevich (@DmitryKK)
9
+ Michael Watts (@mikwat)
10
+ Derek Ethier (@ethier)
11
+ Michael (@mibamur)
12
+ Undo1 (@Undo1)
13
+ Hitabis Engineering (@Hitabis)
14
+ Sergey Alekseev (@sergey-alekseev)
15
+ Sankalp Kulshreshtha (@sankalpk)
16
+ Kirill Sevastyanenko (@kirillseva)
17
+ Denis Shevchenko (@SimplySorc)
data/Gemfile CHANGED
@@ -7,7 +7,7 @@ group :test do
7
7
  gem 'byebug'
8
8
  gem 'pry-byebug'
9
9
  gem 'test-unit' # Implicitly loaded by ammeter
10
- gem 'database_cleaner'
10
+ gem 'database_cleaner', '~> 1.6.2'
11
11
  gem 'codeclimate-test-reporter', :require => nil
12
12
  end
13
13
 
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2016 Florent Monbillard
1
+ Copyright (c) 2017 Florent Monbillard
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -5,11 +5,11 @@ Very simple Roles library without any authorization enforcement supporting scope
5
5
  Let's see an example:
6
6
 
7
7
  ```ruby
8
- user.has_role?(:moderator, Forum.first)
8
+ user.has_role?(:moderator, @forum)
9
9
  => false # if user is moderator of another Forum
10
10
  ```
11
11
 
12
- 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))
12
+ 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))
13
13
 
14
14
  <span style="color: red"><strong>*</strong></span>: authorization gem that doesn't provide a role class
15
15
 
@@ -22,7 +22,7 @@ This library can be easily integrated with any authentication gem ([devise](http
22
22
 
23
23
  ## Installation
24
24
 
25
- Add this to your Gemfile and run the +bundle+ command.
25
+ Add this to your Gemfile and run the `bundle` command.
26
26
 
27
27
  ```ruby
28
28
  gem "rolify"
@@ -32,7 +32,9 @@ gem "rolify"
32
32
 
33
33
  ### 1. Generate Role Model
34
34
 
35
- First, create your Role model and migration file using this generator:
35
+ 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.
36
+
37
+ If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3.
36
38
 
37
39
  ```
38
40
  rails g rolify Role User
@@ -44,10 +46,7 @@ rails g rolify Role User
44
46
  rails g rolify:role Role User
45
47
  ```
46
48
 
47
- 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.
48
- 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
- If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3
49
+ The generator will create your Role model, add a migration file, and update your User class with new class methods.
51
50
 
52
51
  ### 2. Run the migration (only required when using ActiveRecord)
53
52
 
@@ -80,6 +79,8 @@ The `rolify` method accepts the following callback options:
80
79
 
81
80
  Mongoid callbacks are also supported and works the same way.
82
81
 
82
+ The `rolify` method also accepts the `inverse_of` option if you need to disambiguate the relationship.
83
+
83
84
  ### 3.2 Configure your resource models
84
85
 
85
86
  In the resource models you want to apply roles on, just add ``resourcify`` method.
@@ -91,6 +92,18 @@ class Forum < ActiveRecord::Base
91
92
  end
92
93
  ```
93
94
 
95
+ ### 3.3 Assign default role
96
+
97
+ ```ruby
98
+ class User < ActiveRecord::Base
99
+ after_create :assign_default_role
100
+
101
+ def assign_default_role
102
+ self.add_role(:newuser) if self.roles.blank?
103
+ end
104
+ end
105
+ ```
106
+
94
107
  ### 4. Add a role to a user
95
108
 
96
109
  To define a global role:
@@ -114,6 +127,12 @@ user = User.find(3)
114
127
  user.add_role :moderator, Forum
115
128
  ```
116
129
 
130
+ Remove role:
131
+ ```ruby
132
+ user = User.find(3)
133
+ user.remove_role :moderator
134
+ ```
135
+
117
136
  That's it!
118
137
 
119
138
  ### 5. Role queries
@@ -181,6 +200,8 @@ forum.applied_roles
181
200
  ```ruby
182
201
  Forum.with_role(:admin)
183
202
  # => [ list of Forum instances that have role "admin" bound to them ]
203
+ Forum.without_role(:admin)
204
+ # => [ list of Forum instances that do NOT have role "admin" bound to them ]
184
205
  Forum.with_role(:admin, current_user)
185
206
  # => [ list of Forum instances that have role "admin" bound to them and belong to current_user roles ]
186
207
  Forum.with_roles([:admin, :user], current_user)
@@ -2,18 +2,18 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "sqlite3", :platform => "ruby"
6
- gem "activerecord", ">= 3.2.0", :require => "active_record"
5
+ gem "sqlite3", platform: "ruby"
6
+ gem "activerecord", "~> 3.2.0", require: "active_record"
7
7
 
8
8
  group :test do
9
9
  gem "appraisal"
10
- gem "coveralls", :require => false
10
+ gem "coveralls", require: false
11
11
  gem "its"
12
12
  gem "byebug"
13
13
  gem "pry-byebug"
14
14
  gem "test-unit"
15
- gem "database_cleaner"
16
- gem "codeclimate-test-reporter", :require => nil
15
+ gem "database_cleaner", "~> 1.6.2"
16
+ gem "codeclimate-test-reporter", require: nil
17
17
  end
18
18
 
19
- gemspec :path => "../"
19
+ gemspec path: "../"
@@ -2,18 +2,18 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "sqlite3", :platform => "ruby"
6
- gem "activerecord", ">= 4.2.5", :require => "active_record"
5
+ gem "sqlite3", platform: "ruby"
6
+ gem "activerecord", "~> 4.2.5", require: "active_record"
7
7
 
8
8
  group :test do
9
9
  gem "appraisal"
10
- gem "coveralls", :require => false
10
+ gem "coveralls", require: false
11
11
  gem "its"
12
12
  gem "byebug"
13
13
  gem "pry-byebug"
14
14
  gem "test-unit"
15
- gem "database_cleaner"
16
- gem "codeclimate-test-reporter", :require => nil
15
+ gem "database_cleaner", "~> 1.6.2"
16
+ gem "codeclimate-test-reporter", require: nil
17
17
  end
18
18
 
19
- gemspec :path => "../"
19
+ gemspec path: "../"
@@ -2,26 +2,26 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "sqlite3", :platform => "ruby"
6
- gem "activerecord", ">= 5.0.0.beta", :require => "active_record"
5
+ gem "sqlite3", platform: "ruby"
6
+ gem "activerecord", ">= 5.0.0.beta", require: "active_record"
7
7
  gem "actionpack", ">= 5.0.0.beta2"
8
8
  gem "activemodel", ">= 5.0.0.beta2"
9
9
  gem "railties", ">= 5.0.0.beta2"
10
- gem "rspec-rails", :github => "rspec/rspec-rails"
11
- gem "rspec-core", :github => "rspec/rspec-core"
12
- gem "rspec-expectations", :github => "rspec/rspec-expectations"
13
- gem "rspec-mocks", :github => "rspec/rspec-mocks"
14
- gem "rspec-support", :github => "rspec/rspec-support"
10
+ gem "rspec-rails", github: "rspec/rspec-rails"
11
+ gem "rspec-core", github: "rspec/rspec-core"
12
+ gem "rspec-expectations", github: "rspec/rspec-expectations"
13
+ gem "rspec-mocks", github: "rspec/rspec-mocks"
14
+ gem "rspec-support", github: "rspec/rspec-support"
15
15
 
16
16
  group :test do
17
17
  gem "appraisal"
18
- gem "coveralls", :require => false
18
+ gem "coveralls", require: false
19
19
  gem "its"
20
20
  gem "byebug"
21
21
  gem "pry-byebug"
22
22
  gem "test-unit"
23
- gem "database_cleaner"
24
- gem "codeclimate-test-reporter", :require => nil
23
+ gem "database_cleaner", "~> 1.6.2"
24
+ gem "codeclimate-test-reporter", require: nil
25
25
  end
26
26
 
27
- gemspec :path => "../"
27
+ gemspec path: "../"
@@ -3,17 +3,17 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "mongoid", "~> 3"
6
- gem "bson_ext", :platform => "ruby"
6
+ gem "bson_ext", platform: "ruby"
7
7
 
8
8
  group :test do
9
9
  gem "appraisal"
10
- gem "coveralls", :require => false
10
+ gem "coveralls", require: false
11
11
  gem "its"
12
12
  gem "byebug"
13
13
  gem "pry-byebug"
14
14
  gem "test-unit"
15
- gem "database_cleaner"
16
- gem "codeclimate-test-reporter", :require => nil
15
+ gem "database_cleaner", "~> 1.6.2"
16
+ gem "codeclimate-test-reporter", require: nil
17
17
  end
18
18
 
19
- gemspec :path => "../"
19
+ gemspec path: "../"
@@ -3,17 +3,17 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "mongoid", "~> 4"
6
- gem "bson_ext", :platform => "ruby"
6
+ gem "bson_ext", platform: "ruby"
7
7
 
8
8
  group :test do
9
9
  gem "appraisal"
10
- gem "coveralls", :require => false
10
+ gem "coveralls", require: false
11
11
  gem "its"
12
12
  gem "byebug"
13
13
  gem "pry-byebug"
14
14
  gem "test-unit"
15
- gem "database_cleaner"
16
- gem "codeclimate-test-reporter", :require => nil
15
+ gem "database_cleaner", "~> 1.6.2"
16
+ gem "codeclimate-test-reporter", require: nil
17
17
  end
18
18
 
19
- gemspec :path => "../"
19
+ gemspec path: "../"
@@ -3,17 +3,17 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "mongoid", "~> 5"
6
- gem "bson_ext", :platform => "ruby"
6
+ gem "bson_ext", platform: "ruby"
7
7
 
8
8
  group :test do
9
9
  gem "appraisal"
10
- gem "coveralls", :require => false
10
+ gem "coveralls", require: false
11
11
  gem "its"
12
12
  gem "byebug"
13
13
  gem "pry-byebug"
14
14
  gem "test-unit"
15
- gem "database_cleaner"
16
- gem "codeclimate-test-reporter", :require => nil
15
+ gem "database_cleaner", "~> 1.6.2"
16
+ gem "codeclimate-test-reporter", require: nil
17
17
  end
18
18
 
19
- gemspec :path => "../"
19
+ gemspec path: "../"
@@ -1,5 +1,6 @@
1
1
  require 'rails/generators/active_record'
2
2
  require 'active_support/core_ext'
3
+ require 'erb'
3
4
 
4
5
  module ActiveRecord
5
6
  module Generators
@@ -34,7 +35,7 @@ module ActiveRecord
34
35
  end
35
36
 
36
37
  def copy_rolify_migration
37
- migration_template "migration.rb", "db/migrate/rolify_create_#{table_name}.rb"
38
+ migration_template "migration.rb", "db/migrate/rolify_create_#{table_name}.rb", migration_version: migration_version
38
39
  end
39
40
 
40
41
  private
@@ -56,20 +57,7 @@ module ActiveRecord
56
57
  end
57
58
 
58
59
  def model_content
59
- content = <<RUBY
60
- has_and_belongs_to_many :%{user_cname}, :join_table => :%{join_table}
61
-
62
- belongs_to :resource,
63
- :polymorphic => true,
64
- :optional => true
65
-
66
- validates :resource_type,
67
- :inclusion => { :in => Rolify.resource_types },
68
- :allow_nil => true
69
-
70
- scopify
71
- RUBY
72
- content % { :user_cname => user_class.table_name, :join_table => "#{user_cname.constantize.table_name}_#{table_name}"}
60
+ ERB.new(File.read(File.join(__dir__, 'templates/model.rb'))).result(binding)
73
61
  end
74
62
 
75
63
  def user_class
@@ -94,6 +82,16 @@ Please ensure that this model exists and is not mis-spelled and re-run the gener
94
82
  MSG
95
83
  end
96
84
 
85
+ def rails5?
86
+ Rails.version.start_with? '5'
87
+ end
88
+
89
+ def migration_version
90
+ if rails5?
91
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
92
+ end
93
+ end
94
+
97
95
  end
98
96
  end
99
97
  end
@@ -1,4 +1,4 @@
1
- class RolifyCreate<%= table_name.camelize %> < ActiveRecord::Migration
1
+ class RolifyCreate<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
3
  create_table(:<%= table_name %>) do |t|
4
4
  t.string :name
@@ -11,8 +11,7 @@ class RolifyCreate<%= table_name.camelize %> < ActiveRecord::Migration
11
11
  t.references :<%= user_reference %>
12
12
  t.references :<%= role_reference %>
13
13
  end
14
-
15
- add_index(:<%= table_name %>, :name)
14
+ <% if ActiveRecord::Base.connection.class.to_s.demodulize != 'PostgreSQLAdapter' %><%= "\n " %>add_index(:<%= table_name %>, :name)<% end %>
16
15
  add_index(:<%= table_name %>, [ :name, :resource_type, :resource_id ])
17
16
  add_index(:<%= join_table %>, [ :<%= user_reference %>_id, :<%= role_reference %>_id ])
18
17
  end
@@ -0,0 +1,16 @@
1
+ has_and_belongs_to_many :<%= user_class.table_name %>, :join_table => :<%= join_table %>
2
+
3
+ <% if Rails::VERSION::MAJOR < 5 %>
4
+ belongs_to :resource,
5
+ :polymorphic => true
6
+ <% else %>
7
+ belongs_to :resource,
8
+ :polymorphic => true,
9
+ :optional => true
10
+ <% end %>
11
+
12
+ validates :resource_type,
13
+ :inclusion => { :in => Rolify.resource_types },
14
+ :allow_nil => true
15
+
16
+ scopify
@@ -4,4 +4,7 @@ Rolify.configure<%= "(\"#{class_name.camelize.to_s}\")" if class_name != "Role"
4
4
 
5
5
  # Dynamic shortcuts for User class (user.is_admin? like methods). Default is: false
6
6
  # config.use_dynamic_shortcuts
7
- end
7
+
8
+ # Configuration to remove roles from database once the last resource is removed. Default is: true
9
+ # config.remove_role_if_empty = false
10
+ end
@@ -25,7 +25,7 @@ module Rolify
25
25
 
26
26
  rolify_options = { :class_name => options[:role_cname].camelize }
27
27
  rolify_options.merge!({ :join_table => self.role_join_table_name }) if Rolify.orm == "active_record"
28
- rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove ].include? k.to_sym })
28
+ rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove, :inverse_of ].include? k.to_sym })
29
29
 
30
30
  has_and_belongs_to_many :roles, rolify_options
31
31
 
@@ -27,7 +27,7 @@ module Rolify
27
27
 
28
28
  def in(relation, user, role_names)
29
29
  roles = user.roles.where(:name => role_names).select("#{quote_table(role_class.table_name)}.#{quote_column(role_class.primary_key)}")
30
- relation.where("#{quote_table(role_class.table_name)}.#{quote_column(role_class.primary_key)} IN (?) AND ((resource_id = #{quote_table(relation.table_name)}.#{quote_column(relation.primary_key)}) OR (resource_id IS NULL))", roles)
30
+ relation.where("#{quote_table(role_class.table_name)}.#{quote_column(role_class.primary_key)} IN (?) AND ((#{quote_table(role_class.table_name)}.resource_id = #{quote_table(relation.table_name)}.#{quote_column(relation.primary_key)}) OR (#{quote_table(role_class.table_name)}.resource_id IS NULL))", roles)
31
31
  end
32
32
 
33
33
  def applied_roles(relation, children)
@@ -1,3 +1,3 @@
1
1
  module Rolify
2
- VERSION = "5.1.0"
2
+ VERSION = "5.2.0"
3
3
  end
@@ -8,6 +8,7 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
8
8
  destination File.expand_path("../../../../tmp", __FILE__)
9
9
  teardown :cleanup_destination_root
10
10
 
11
+ let(:adapter) { 'SQLite3Adapter' }
11
12
  before {
12
13
  prepare_destination
13
14
  }
@@ -20,6 +21,8 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
20
21
  before(:all) { arguments %w(Role) }
21
22
 
22
23
  before {
24
+ allow(ActiveRecord::Base).to receive_message_chain(
25
+ 'connection.class.to_s.demodulize') { adapter }
23
26
  capture(:stdout) {
24
27
  generator.create_file "app/models/user.rb" do
25
28
  <<-RUBY
@@ -43,8 +46,24 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
43
46
  describe 'app/models/role.rb' do
44
47
  subject { file('app/models/role.rb') }
45
48
  it { should exist }
46
- it { should contain "class Role < ActiveRecord::Base" }
49
+ it do
50
+ if Rails::VERSION::MAJOR < 5
51
+ should contain "class Role < ActiveRecord::Base"
52
+ else
53
+ should contain "class Role < ApplicationRecord"
54
+ end
55
+ end
47
56
  it { should contain "has_and_belongs_to_many :users, :join_table => :users_roles" }
57
+ it do
58
+ if Rails::VERSION::MAJOR < 5
59
+ should contain "belongs_to :resource,\n"
60
+ " :polymorphic => true"
61
+ else
62
+ should contain "belongs_to :resource,\n"
63
+ " :polymorphic => true,\n"
64
+ " :optional => true"
65
+ end
66
+ end
48
67
  it { should contain "belongs_to :resource,\n"
49
68
  " :polymorphic => true,\n"
50
69
  " :optional => true"
@@ -65,6 +84,24 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
65
84
  it { should be_a_migration }
66
85
  it { should contain "create_table(:roles) do" }
67
86
  it { should contain "create_table(:users_roles, :id => false) do" }
87
+
88
+ context 'mysql2' do
89
+ let(:adapter) { 'Mysql2Adapter' }
90
+
91
+ it { expect(subject).to contain('add_index(:roles, :name)') }
92
+ end
93
+
94
+ context 'sqlite3' do
95
+ let(:adapter) { 'SQLite3Adapter' }
96
+
97
+ it { expect(subject).to contain('add_index(:roles, :name)') }
98
+ end
99
+
100
+ context 'pg' do
101
+ let(:adapter) { 'PostgreSQLAdapter' }
102
+
103
+ it { expect(subject).not_to contain('add_index(:roles, :name)') }
104
+ end
68
105
  end
69
106
  end
70
107
 
@@ -72,6 +109,8 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
72
109
  before(:all) { arguments %w(AdminRole AdminUser) }
73
110
 
74
111
  before {
112
+ allow(ActiveRecord::Base).to receive_message_chain(
113
+ 'connection.class.to_s.demodulize') { adapter }
75
114
  capture(:stdout) {
76
115
  generator.create_file "app/models/admin_user.rb" do
77
116
  "class AdminUser < ActiveRecord::Base\nend"
@@ -94,7 +133,13 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
94
133
  subject { file('app/models/admin_role.rb') }
95
134
 
96
135
  it { should exist }
97
- it { should contain "class AdminRole < ActiveRecord::Base" }
136
+ it do
137
+ if Rails::VERSION::MAJOR < 5
138
+ should contain "class AdminRole < ActiveRecord::Base"
139
+ else
140
+ should contain "class AdminRole < ApplicationRecord"
141
+ end
142
+ end
98
143
  it { should contain "has_and_belongs_to_many :admin_users, :join_table => :admin_users_admin_roles" }
99
144
  it { should contain "belongs_to :resource,\n"
100
145
  " :polymorphic => true,\n"
@@ -114,6 +159,24 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
114
159
  it { should be_a_migration }
115
160
  it { should contain "create_table(:admin_roles)" }
116
161
  it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
162
+
163
+ context 'mysql2' do
164
+ let(:adapter) { 'Mysql2Adapter' }
165
+
166
+ it { expect(subject).to contain('add_index(:admin_roles, :name)') }
167
+ end
168
+
169
+ context 'sqlite3' do
170
+ let(:adapter) { 'SQLite3Adapter' }
171
+
172
+ it { expect(subject).to contain('add_index(:admin_roles, :name)') }
173
+ end
174
+
175
+ context 'pg' do
176
+ let(:adapter) { 'PostgreSQLAdapter' }
177
+
178
+ it { expect(subject).not_to contain('add_index(:admin_roles, :name)') }
179
+ end
117
180
  end
118
181
  end
119
182
 
@@ -121,6 +184,8 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
121
184
  before(:all) { arguments %w(Admin::Role Admin::User) }
122
185
 
123
186
  before {
187
+ allow(ActiveRecord::Base).to receive_message_chain(
188
+ 'connection.class.to_s.demodulize') { adapter }
124
189
  capture(:stdout) {
125
190
  generator.create_file "app/models/admin/user.rb" do
126
191
  <<-RUBY
@@ -149,7 +214,13 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
149
214
  subject { file('app/models/admin/role.rb') }
150
215
 
151
216
  it { should exist }
152
- it { should contain "class Admin::Role < ActiveRecord::Base" }
217
+ it do
218
+ if Rails::VERSION::MAJOR < 5
219
+ should contain "class Admin::Role < ActiveRecord::Base"
220
+ else
221
+ should contain "class Admin::Role < ApplicationRecord"
222
+ end
223
+ end
153
224
  it { should contain "has_and_belongs_to_many :admin_users, :join_table => :admin_users_admin_roles" }
154
225
  it { should contain "belongs_to :resource,\n"
155
226
  " :polymorphic => true,\n"
@@ -169,6 +240,24 @@ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_r
169
240
  it { should be_a_migration }
170
241
  it { should contain "create_table(:admin_roles)" }
171
242
  it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
243
+
244
+ context 'mysql2' do
245
+ let(:adapter) { 'Mysql2Adapter' }
246
+
247
+ it { expect(subject).to contain('add_index(:admin_roles, :name)') }
248
+ end
249
+
250
+ context 'sqlite3' do
251
+ let(:adapter) { 'SQLite3Adapter' }
252
+
253
+ it { expect(subject).to contain('add_index(:admin_roles, :name)') }
254
+ end
255
+
256
+ context 'pg' do
257
+ let(:adapter) { 'PostgreSQLAdapter' }
258
+
259
+ it { expect(subject).not_to contain('add_index(:admin_roles, :name)') }
260
+ end
172
261
  end
173
262
  end
174
263
  end
@@ -1,6 +1,5 @@
1
- require "codeclimate-test-reporter"
2
-
3
- CodeClimate::TestReporter.start
1
+ require "simplecov"
2
+ SimpleCov.start
4
3
 
5
4
  require 'rubygems'
6
5
  require "bundler/setup"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rolify
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.0
4
+ version: 5.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florent Monbillard
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-19 00:00:00.000000000 Z
12
+ date: 2017-12-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ammeter
@@ -99,6 +99,7 @@ files:
99
99
  - lib/generators/active_record/rolify_generator.rb
100
100
  - lib/generators/active_record/templates/README
101
101
  - lib/generators/active_record/templates/migration.rb
102
+ - lib/generators/active_record/templates/model.rb
102
103
  - lib/generators/mongoid/rolify_generator.rb
103
104
  - lib/generators/mongoid/templates/README-mongoid
104
105
  - lib/generators/rolify/rolify_generator.rb
@@ -183,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
184
  version: '0'
184
185
  requirements: []
185
186
  rubyforge_project: rolify
186
- rubygems_version: 2.4.5.1
187
+ rubygems_version: 2.6.14
187
188
  signing_key:
188
189
  specification_version: 4
189
190
  summary: Roles library with resource scoping