rolify 3.3.0.rc2 → 3.3.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,5 +5,17 @@ task :default => :spec
5
5
 
6
6
  desc "Run all specs"
7
7
  task "spec" do
8
- exec "bundle exec rspec spec"
8
+ Rake::Task['generators'].invoke
9
+ return_code1 = $?.exitstatus
10
+ Rake::Task['rolify'].invoke
11
+ return_code2 = $?.exitstatus
12
+ fail if return_code1 != 0 || return_code2 != 0
13
+ end
14
+
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"
9
21
  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<%= "(\"#{role_cname.camelize.to_s}\")" if role_cname != "Role" %> do |config|
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 orm_adapter == "active_record" %>config.use_mongoid
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,3 +1,3 @@
1
1
  module Rolify
2
- VERSION = "3.3.0.rc2"
2
+ VERSION = "3.3.0.rc3"
3
3
  end
@@ -1,11 +1,11 @@
1
- require 'spec_helper'
1
+ require 'generators_helper'
2
2
 
3
3
  # Generators are not automatically loaded by Rails
4
- require 'generators/rolify/role/role_generator'
4
+ require 'generators/rolify/rolify_generator'
5
5
 
6
- describe Rolify::Generators::RoleGenerator do
6
+ describe Rolify::Generators::RolifyGenerator do
7
7
  # Tell the generator where to put its output (what it thinks of as Rails.root)
8
- destination File.expand_path("../../../../../tmp", __FILE__)
8
+ destination File.expand_path("../../../../tmp", __FILE__)
9
9
  teardown :cleanup_destination_root
10
10
 
11
11
  before {
@@ -16,15 +16,19 @@ describe Rolify::Generators::RoleGenerator do
16
16
  FileUtils.rm_rf destination_root
17
17
  end
18
18
 
19
- describe 'no arguments' do
20
- before(:all) { arguments [] }
19
+ describe 'specifying only Role class name' do
20
+ before(:all) { arguments %w(Role) }
21
21
 
22
22
  before {
23
23
  capture(:stdout) {
24
24
  generator.create_file "app/models/user.rb" do
25
- "class User < ActiveRecord::Base\nend"
25
+ <<-RUBY
26
+ class User < ActiveRecord::Base
27
+ end
28
+ RUBY
26
29
  end
27
30
  }
31
+ require File.join(destination_root, "app/models/user.rb")
28
32
  run_generator
29
33
  }
30
34
 
@@ -58,7 +62,7 @@ describe Rolify::Generators::RoleGenerator do
58
62
  end
59
63
  end
60
64
 
61
- describe 'specifying user and role names' do
65
+ describe 'specifying User and Role class names' do
62
66
  before(:all) { arguments %w(AdminRole AdminUser) }
63
67
 
64
68
  before {
@@ -67,6 +71,7 @@ describe Rolify::Generators::RoleGenerator do
67
71
  "class AdminUser < ActiveRecord::Base\nend"
68
72
  end
69
73
  }
74
+ require File.join(destination_root, "app/models/admin_user.rb")
70
75
  run_generator
71
76
  }
72
77
 
@@ -79,7 +84,7 @@ describe Rolify::Generators::RoleGenerator do
79
84
  it { should contain "# config.use_mongoid" }
80
85
  end
81
86
 
82
- describe 'app/models/rank.rb' do
87
+ describe 'app/models/admin_role.rb' do
83
88
  subject { file('app/models/admin_role.rb') }
84
89
 
85
90
  it { should exist }
@@ -103,21 +108,74 @@ describe Rolify::Generators::RoleGenerator do
103
108
  end
104
109
  end
105
110
 
106
- describe 'specifying orm adapter' do
107
- before(:all) { arguments [ "Role", "User", "mongoid" ] }
111
+ describe 'specifying namespaced User and Role class names' do
112
+ before(:all) { arguments %w(Admin::Role Admin::User) }
113
+
114
+ before {
115
+ capture(:stdout) {
116
+ generator.create_file "app/models/admin/user.rb" do
117
+ <<-RUBY
118
+ module Admin
119
+ class User < ActiveRecord::Base
120
+ self.table_name_prefix = 'admin_'
121
+ end
122
+ end
123
+ RUBY
124
+ end
125
+ }
126
+ require File.join(destination_root, "app/models/admin/user.rb")
127
+ run_generator
128
+ }
129
+
130
+ describe 'config/initializers/rolify.rb' do
131
+ subject { file('config/initializers/rolify.rb') }
132
+
133
+ it { should exist }
134
+ it { should contain "Rolify.configure(\"Admin::Role\") do |config|"}
135
+ it { should contain "# config.use_dynamic_shortcuts" }
136
+ it { should contain "# config.use_mongoid" }
137
+ end
138
+
139
+ describe 'app/models/admin/role.rb' do
140
+ subject { file('app/models/admin/role.rb') }
141
+
142
+ it { should exist }
143
+ it { should contain "class Admin::Role < ActiveRecord::Base" }
144
+ it { should contain "has_and_belongs_to_many :admin_users, :join_table => :admin_users_admin_roles" }
145
+ it { should contain "belongs_to :resource, :polymorphic => true" }
146
+ end
147
+
148
+ describe 'app/models/admin/user.rb' do
149
+ subject { file('app/models/admin/user.rb') }
150
+
151
+ it { should contain /class User < ActiveRecord::Base\n rolify :role_cname => 'Admin::Role'\n/ }
152
+ end
153
+
154
+ describe 'migration file' do
155
+ subject { migration_file('db/migrate/rolify_create_admin_roles.rb') }
156
+
157
+ it { should be_a_migration }
158
+ it { should contain "create_table(:admin_roles)" }
159
+ it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
160
+ end
161
+ end
162
+
163
+ describe 'specifying ORM adapter' do
164
+ before(:all) { arguments [ "Role", "User", "--orm=mongoid" ] }
108
165
 
109
166
  before {
110
167
  capture(:stdout) {
111
168
  generator.create_file "app/models/user.rb" do
112
- <<-CLASS
169
+ <<-RUBY
113
170
  class User
114
171
  include Mongoid::Document
115
172
 
116
173
  field :login, :type => String
117
174
  end
118
- CLASS
175
+ RUBY
119
176
  end
120
177
  }
178
+ require File.join(destination_root, "app/models/user.rb")
121
179
  run_generator
122
180
  }
123
181
 
@@ -149,4 +207,48 @@ CLASS
149
207
  it { should contain /class User\n include Mongoid::Document\n rolify\n/ }
150
208
  end
151
209
  end
210
+
211
+ describe 'specifying namespaced User and Role class names and ORM adapter' do
212
+ before(:all) { arguments %w(Admin::Role Admin::User --orm=mongoid) }
213
+
214
+ before {
215
+ capture(:stdout) {
216
+ generator.create_file "app/models/admin/user.rb" do
217
+ <<-RUBY
218
+ module Admin
219
+ class User
220
+ include Mongoid::Document
221
+ end
222
+ end
223
+ RUBY
224
+ end
225
+ }
226
+ require File.join(destination_root, "app/models/admin/user.rb")
227
+ run_generator
228
+ }
229
+
230
+ describe 'config/initializers/rolify.rb' do
231
+ subject { file('config/initializers/rolify.rb') }
232
+
233
+ it { should exist }
234
+ it { should contain "Rolify.configure(\"Admin::Role\") do |config|"}
235
+ it { should contain "# config.use_dynamic_shortcuts" }
236
+ it { should_not contain "# config.use_mongoid" }
237
+ end
238
+
239
+ describe 'app/models/admin/role.rb' do
240
+ subject { file('app/models/admin/role.rb') }
241
+
242
+ it { should exist }
243
+ it { should contain "class Admin::Role" }
244
+ it { should contain "has_and_belongs_to_many :admin_users" }
245
+ it { should contain "belongs_to :resource, :polymorphic => true" }
246
+ end
247
+
248
+ describe 'app/models/admin/user.rb' do
249
+ subject { file('app/models/admin/user.rb') }
250
+
251
+ it { should contain /class User\n include Mongoid::Document\n rolify :role_cname => 'Admin::Role'\n/ }
252
+ end
253
+ end
152
254
  end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require "bundler/setup"
3
+
4
+ require 'rolify'
5
+ require 'rolify/matchers'
6
+ require 'rails/all'
7
+
8
+ module TestApp
9
+ class Application < ::Rails::Application
10
+ config.root = File.dirname(__FILE__)
11
+ end
12
+ end
13
+
14
+ require 'ammeter/init'
@@ -1,7 +1,7 @@
1
1
  require "rolify/shared_contexts"
2
2
 
3
3
  shared_examples_for "Role.scopes" do |param_name, param_method|
4
- before(:all) do
4
+ before(:each) do
5
5
  role_class.destroy_all
6
6
  end
7
7
 
@@ -28,7 +28,7 @@ shared_examples_for "Role.scopes" do |param_name, param_method|
28
28
  let!(:zombie_role) { subject.add_role :visitor, Forum.last }
29
29
  let!(:anonymous_role) { subject.add_role :anonymous, Group.last }
30
30
 
31
- it { subject.roles.instance_scoped.should == [ visitor_role, zombie_role, anonymous_role ] }
31
+ it { subject.roles.instance_scoped.all.entries.should == [ visitor_role, zombie_role, anonymous_role ] }
32
32
  it { subject.roles.instance_scoped(Forum).should == [ visitor_role, zombie_role ] }
33
33
  it { subject.roles.instance_scoped(Forum.first).should == [ visitor_role ] }
34
34
  it { subject.roles.instance_scoped(Forum.last).should == [ zombie_role ] }
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ require "bundler/setup"
3
3
 
4
4
  require 'rolify'
5
5
  require 'rolify/matchers'
6
- require 'ammeter/init'
6
+ require 'rails/all'
7
7
 
8
8
  ENV['ADAPTER'] ||= 'active_record'
9
9
 
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: 3.3.0.rc2
4
+ version: 3.3.0.rc3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-08 00:00:00.000000000 Z
12
+ date: 2013-02-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3
@@ -204,14 +204,17 @@ files:
204
204
  - README.md
205
205
  - Rakefile
206
206
  - UPGRADE.rdoc
207
- - lib/generators/rolify/role/role_generator.rb
208
- - lib/generators/rolify/role/templates/README
209
- - lib/generators/rolify/role/templates/README-active_record
210
- - lib/generators/rolify/role/templates/README-mongoid
211
- - lib/generators/rolify/role/templates/initializer.rb
212
- - lib/generators/rolify/role/templates/migration.rb
213
- - lib/generators/rolify/role/templates/role-active_record.rb
214
- - lib/generators/rolify/role/templates/role-mongoid.rb
207
+ - lib/generators/active_record/rolify_generator.rb
208
+ - lib/generators/active_record/templates/README
209
+ - lib/generators/active_record/templates/migration.rb
210
+ - lib/generators/mongoid/rolify_generator.rb
211
+ - lib/generators/mongoid/templates/README-mongoid
212
+ - lib/generators/rolify/rolify_generator.rb
213
+ - lib/generators/rolify/templates/README
214
+ - lib/generators/rolify/templates/initializer.rb
215
+ - lib/generators/rolify/templates/role-active_record.rb
216
+ - lib/generators/rolify/templates/role-mongoid.rb
217
+ - lib/generators/rolify/user_generator.rb
215
218
  - lib/rolify.rb
216
219
  - lib/rolify/adapters/active_record/resource_adapter.rb
217
220
  - lib/rolify/adapters/active_record/role_adapter.rb
@@ -230,7 +233,8 @@ files:
230
233
  - lib/rolify/utils.rb
231
234
  - lib/rolify/version.rb
232
235
  - rolify.gemspec
233
- - spec/generators/rolify/role/role_generator_spec.rb
236
+ - spec/generators/rolify/rolify_generator_spec.rb
237
+ - spec/generators_helper.rb
234
238
  - spec/rolify/config_spec.rb
235
239
  - spec/rolify/custom_spec.rb
236
240
  - spec/rolify/matchers_spec.rb
@@ -276,12 +280,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
276
280
  version: 1.3.1
277
281
  requirements: []
278
282
  rubyforge_project: rolify
279
- rubygems_version: 1.8.23
283
+ rubygems_version: 1.8.25
280
284
  signing_key:
281
285
  specification_version: 3
282
286
  summary: Roles library with resource scoping
283
287
  test_files:
284
- - spec/generators/rolify/role/role_generator_spec.rb
288
+ - spec/generators/rolify/rolify_generator_spec.rb
289
+ - spec/generators_helper.rb
285
290
  - spec/rolify/config_spec.rb
286
291
  - spec/rolify/custom_spec.rb
287
292
  - spec/rolify/matchers_spec.rb
@@ -1,52 +0,0 @@
1
- require 'rails/generators/migration'
2
-
3
- module Rolify
4
- module Generators
5
- class RoleGenerator < Rails::Generators::Base
6
- include Rails::Generators::Migration
7
-
8
- source_root File.expand_path('../templates', __FILE__)
9
- argument :role_cname, :type => :string, :default => "Role"
10
- argument :user_cname, :type => :string, :default => "User"
11
- argument :orm_adapter, :type => :string, :default => "active_record"
12
- #class_option :dynamic_shortcuts, :type => :boolean, :default => false
13
-
14
- desc "Generates a model with the given NAME and a migration file."
15
-
16
- def generate_role
17
- template "role-#{orm_adapter}.rb", "app/models/#{role_cname.underscore}.rb"
18
- inject_into_file(model_path, :after => inject_rolify_method) do
19
- " rolify" + (role_cname == "Role" ? "" : " :role_cname => '#{role_cname.camelize}'") + "\n"
20
- end
21
- end
22
-
23
- def copy_role_file
24
- template "initializer.rb", "config/initializers/rolify.rb"
25
- migration_template "migration.rb", "db/migrate/rolify_create_#{role_cname.tableize}" if orm_adapter == "active_record"
26
- end
27
-
28
- def model_path
29
- File.join("app", "models", "#{user_cname.underscore}.rb")
30
- end
31
-
32
- def self.next_migration_number(path)
33
- Time.now.utc.strftime("%Y%m%d%H%M%S")
34
- end
35
-
36
- def show_readme
37
- if behavior == :invoke
38
- readme "README"
39
- readme "README-#{orm_adapter}"
40
- end
41
- end
42
-
43
- def inject_rolify_method
44
- if orm_adapter == "active_record"
45
- /class #{user_cname.camelize}\n|class #{user_cname.camelize} .*\n/
46
- else
47
- /include Mongoid::Document\n|include Mongoid::Document .*\n/
48
- end
49
- end
50
- end
51
- end
52
- end
@@ -1,19 +0,0 @@
1
- class RolifyCreate<%= role_cname.pluralize.camelize %> < ActiveRecord::Migration
2
- def change
3
- create_table(:<%= role_cname.tableize %>) do |t|
4
- t.string :name
5
- t.references :resource, :polymorphic => true
6
-
7
- t.timestamps
8
- end
9
-
10
- create_table(:<%= (user_cname.tableize + "_" + role_cname.tableize) %>, :id => false) do |t|
11
- t.references :<%= user_cname.underscore.singularize %>
12
- t.references :<%= role_cname.underscore.singularize %>
13
- end
14
-
15
- add_index(:<%= role_cname.tableize %>, :name)
16
- add_index(:<%= role_cname.tableize %>, [ :name, :resource_type, :resource_id ])
17
- add_index(:<%= "#{user_cname.tableize}_#{role_cname.tableize}" %>, [ :<%= user_cname.underscore.singularize %>_id, :<%= role_cname.underscore.singularize %>_id ])
18
- end
19
- end
@@ -1,6 +0,0 @@
1
- class <%= role_cname.camelize %> < ActiveRecord::Base
2
- has_and_belongs_to_many :<%= user_cname.tableize %>, :join_table => :<%= "#{user_cname.tableize}_#{role_cname.tableize}" %>
3
- belongs_to :resource, :polymorphic => true
4
-
5
- scopify
6
- end