roles_active_record 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.document +0 -0
  2. data/.gitignore +0 -0
  3. data/.rspec +0 -0
  4. data/Gemfile +0 -0
  5. data/LICENSE +0 -0
  6. data/README.markdown +1 -1
  7. data/Rakefile +0 -0
  8. data/VERSION +1 -1
  9. data/development.sqlite3 +0 -0
  10. data/lib/generators/active_record/roles/roles_generator.rb +58 -29
  11. data/lib/generators/active_record/roles_migration/roles_migration_generator.rb +7 -6
  12. data/lib/generators/active_record/roles_migration/templates/add_admin_flag_strategy.erb +0 -0
  13. data/lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb +0 -0
  14. data/lib/generators/active_record/roles_migration/templates/add_one_role_strategy.erb +0 -0
  15. data/lib/generators/active_record/roles_migration/templates/add_role_string_strategy.erb +0 -0
  16. data/lib/generators/active_record/roles_migration/templates/{add_roles_mask_to_users_migration.erb → add_roles_mask_strategy.erb} +0 -0
  17. data/lib/generators/active_record/setup/setup_generator.rb +0 -0
  18. data/lib/roles_active_record/base.rb +0 -0
  19. data/lib/roles_active_record/namespaces.rb +0 -0
  20. data/lib/roles_active_record/role.rb +1 -1
  21. data/lib/roles_active_record/strategy/multi/many_roles.rb +0 -0
  22. data/lib/roles_active_record/strategy/multi/roles_mask.rb +0 -0
  23. data/lib/roles_active_record/strategy/single/admin_flag.rb +0 -0
  24. data/lib/roles_active_record/strategy/single/one_role.rb +0 -0
  25. data/lib/roles_active_record/strategy/single/role_string.rb +0 -0
  26. data/lib/roles_active_record/strategy.rb +0 -0
  27. data/lib/{roles_for_active_record.rb → roles_active_record.rb} +1 -1
  28. data/lib/views/_multi_role_selector.erb.html +0 -0
  29. data/lib/views/_single_role_selector.erb.html +0 -0
  30. data/logging.log +26 -0
  31. data/roles_active_record.gemspec +20 -7
  32. data/roles_for_ar.gemspec +0 -0
  33. data/sandbox/Rakefile +0 -0
  34. data/sandbox/add_role_to_users_migration.erb +0 -0
  35. data/sandbox/create_roles_migration.erb +0 -0
  36. data/sandbox/create_user_roles_migration.erb +0 -0
  37. data/sandbox/database.log +0 -0
  38. data/sandbox/database.yml +0 -0
  39. data/sandbox/development.sqlite3 +0 -0
  40. data/sandbox/migrate/201002508_create_roles.rb +0 -0
  41. data/sandbox/migrate/20102507_create_users.rb +0 -0
  42. data/sandbox/model_base.rb +0 -0
  43. data/sandbox/test.rb +0 -0
  44. data/spec/db/database.yml +0 -0
  45. data/spec/fixtures/many_roles_setup.rb +0 -0
  46. data/spec/fixtures/one_role_setup.rb +0 -0
  47. data/spec/generator_spec_helper.rb +13 -0
  48. data/spec/migration_spec_helper.rb +20 -0
  49. data/spec/migrations/admin_flag/004_add_admin_flag_to_users.rb +0 -0
  50. data/spec/migrations/many_roles/002_create_roles.rb +0 -0
  51. data/spec/migrations/many_roles/003_create_user_roles.rb +0 -0
  52. data/spec/migrations/one_role/002_create_roles.rb +0 -0
  53. data/spec/migrations/one_role/003_add_role_to_users.rb +0 -0
  54. data/spec/migrations/role_string/002_add_role_string_to_users.rb +0 -0
  55. data/spec/migrations/roles_mask/005_add_roles_mask_to_users.rb +0 -0
  56. data/spec/migrations/users/001_create_users.rb +0 -0
  57. data/spec/roles_active_record/admin_flag_spec.rb +0 -0
  58. data/spec/roles_active_record/generators/roles_generator_spec.rb +75 -0
  59. data/spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb +38 -36
  60. data/spec/roles_active_record/generators/roles_migration/many_roles_spec.rb +26 -28
  61. data/spec/roles_active_record/generators/roles_migration/one_role_spec.rb +21 -28
  62. data/spec/roles_active_record/generators/roles_migration/role_string_spec.rb +29 -15
  63. data/spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb +12 -14
  64. data/spec/roles_active_record/generators/setup_generator_spec.rb +0 -0
  65. data/spec/roles_active_record/many_roles_spec.rb +0 -0
  66. data/spec/roles_active_record/one_role_spec.rb +0 -0
  67. data/spec/roles_active_record/role_string_spec.rb +0 -0
  68. data/spec/roles_active_record/roles_mask_spec.rb +0 -0
  69. data/spec/spec_helper.rb +0 -0
  70. data/tmp/rails/app/models/user.rb +7 -0
  71. data/tmp/rails/config/routes.rb +2 -0
  72. data/tmp/rails/db/migrations/20100912101428_add_admin_flag_strategy.rb +14 -0
  73. data/tmp/rails/db/migrations/20100912101429_remove_admin_flag_strategy.rb +14 -0
  74. data/tmp/rails/db/migrations/20100912105055_add_many_roles_strategy.rb +40 -0
  75. data/tmp/rails/db/migrations/20100912105323_add_one_role_strategy.rb +40 -0
  76. data/tmp/rails/db/migrations/20100912110044_add_role_string_strategy.rb +13 -0
  77. data/tmp/rails/db/migrations/20100912110249_add_roles_mask_strategy.rb +13 -0
  78. metadata +20 -7
  79. data/spec/roles_active_record/generators/migration_spec_helper.rb +0 -17
data/.document CHANGED
File without changes
data/.gitignore CHANGED
File without changes
data/.rspec CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/LICENSE CHANGED
File without changes
data/README.markdown CHANGED
@@ -19,7 +19,7 @@ It also implements the following Role strategies:
19
19
 
20
20
  ## Rails generator
21
21
 
22
- Needs some more refactoring (see notes below!)
22
+ Will create admin and guest roles by default
23
23
 
24
24
  <code>$ rails g active_record:roles User --strategy admin_flag --roles admin guest</code>
25
25
 
data/Rakefile CHANGED
File without changes
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/development.sqlite3 CHANGED
File without changes
@@ -1,46 +1,75 @@
1
+ require 'rails3_artifactor'
2
+ require 'logging_assist'
3
+
1
4
  module ActiveRecord
2
5
  module Generators
3
- class RolesGenerator < Rails::Generators::NamedBase
4
- desc "Generate roles model for User"
6
+ class RolesGenerator < Rails::Generators::NamedBase
7
+ desc "Add role strategy to a model"
5
8
 
6
- argument :role_strategy, :type => :string, :aliases => "-r", :default => 'inline_role', :desc => "Create roles model for user"
9
+ class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
10
+ :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, one_role, many_roles, roles_mask)"
7
11
 
8
- hook_for :orm
9
-
10
- def self.source_root
11
- @source_root ||= File.expand_path("../../templates", __FILE__)
12
- end
13
12
 
14
- def apply_role_strategy
15
- insert_into_model('user', role_strategy_statement)
13
+ class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
14
+
15
+ class_option :default_roles, :type => :boolean, :default => true, :desc => "Use default roles :admin and :base"
16
+
17
+
18
+ def apply_role_strategy
19
+ log.add_logfile
20
+ log.debug "apply_role_strategy for : #{strategy} in model #{name}"
21
+ begin
22
+ insert_into_model name do
23
+ insertion_text
24
+ end
25
+ rescue
26
+ say "Model #{name} not found"
27
+ end
16
28
  end
17
29
 
18
30
  protected
19
31
 
20
- def match_expr
21
- /< (.+?)\w+\s/
22
- end
32
+ extend Rails3::Assist::UseMacro
33
+ use_orm :active_record
34
+ include Rails::Assist::BasicLogging
23
35
 
36
+ def orm
37
+ :active_record
38
+ end
39
+
40
+ def default_roles
41
+ options[:default_roles] ? [:admin, :guest] : []
42
+ end
43
+
44
+ def roles_to_add
45
+ @roles_to_add ||= default_roles.concat(options[:roles]).to_symbols.uniq
46
+ end
47
+
48
+ def roles
49
+ roles_to_add.map{|r| ":#{r}" }
50
+ end
51
+
24
52
  def role_strategy_statement
25
- "role_strategy #{role_strategy}"
53
+ "strategy :#{strategy}, :default\n#{role_class_stmt}"
26
54
  end
27
55
 
28
- def role_strategy
29
- options[:role_strategy]
56
+ def role_class_stmt
57
+ " role_class :role" if [:one_role, :many_roles].include? (strategy.to_sym)
58
+ end
59
+
60
+ def roles_statement
61
+ roles ? "valid_roles_are #{roles.join(', ')}" : ''
62
+ end
63
+
64
+ def insertion_text
65
+ %Q{include Roles::#{orm.to_s.camelize}
66
+ #{role_strategy_statement}
67
+ #{roles_statement}}
68
+ end
69
+
70
+ def strategy
71
+ options[:strategy]
30
72
  end
31
-
32
- def model_file(name)
33
- File.join(Rails.root, "app/models/#{name}.rb")
34
- end
35
-
36
- def insert_into_model(model_name, insert_text)
37
- model_name = model_name.to_s
38
- file = File.new(model_file(model_name))
39
- return if (file.read =~ /#{insert_text}/)
40
- gsub_file model_file(model_name), match_expr do |match|
41
- match << insert_text
42
- end
43
- end
44
73
  end
45
74
  end
46
75
  end
@@ -1,12 +1,9 @@
1
- # require 'generators/migration_helper'
2
- # require 'generators/role_migrations'
3
- # require 'auth_assistant/model/user_config'
1
+ require 'migration_assist'
4
2
 
5
3
  module ActiveRecord
6
4
  module Generators
7
5
  class RolesMigrationGenerator < Rails::Generators::NamedBase
8
- include Rails::Generators::MigrationHelper
9
- include RSpec::Rails::App
6
+ include Rails::Migration::Assist
10
7
 
11
8
  desc "Generates user role migrations"
12
9
 
@@ -22,7 +19,7 @@ module ActiveRecord
22
19
  end
23
20
 
24
21
  def valid_strategy?
25
- if ![:admin_flag, :role_string, :one_role, :many_roles, :roles_mask].include?(strategy.to_sym)
22
+ if !strategies.include?(strategy.to_sym)
26
23
  info "Unknown role strategy #{strategy}"
27
24
  raise ArgumentError, "Unknown role strategy #{strategy}"
28
25
  end
@@ -37,6 +34,10 @@ module ActiveRecord
37
34
  end
38
35
 
39
36
  protected
37
+
38
+ def strategies
39
+ [:admin_flag, :role_string, :one_role, :many_roles, :roles_mask]
40
+ end
40
41
 
41
42
  def reverse?
42
43
  options[:reverse]
File without changes
File without changes
@@ -3,7 +3,7 @@ module Roles::Base
3
3
  strategy_class.valid_roles = role_list.to_symbols
4
4
  if role_class_name
5
5
  role_list.each do |name|
6
- res = role_class_name.create(:name => name.to_s)
6
+ role_class_name.create(:name => name.to_s)
7
7
  end
8
8
  end
9
9
  end
File without changes
File without changes
File without changes
File without changes
@@ -2,7 +2,7 @@ require 'roles_generic'
2
2
  require 'require_all'
3
3
  require 'set'
4
4
  require 'sugar-high/array'
5
-
5
+ require 'rails'
6
6
  require 'arel'
7
7
  require 'meta_where'
8
8
  require 'yaml'
File without changes
File without changes
data/logging.log ADDED
@@ -0,0 +1,26 @@
1
+ [Rails::Assist::Logging]
2
+ apply_role_strategy for : admin_flag in model User
3
+ [Rails::Assist::Logging]
4
+ apply_role_strategy for : one_role in model User
5
+ [Rails::Assist::Logging]
6
+ apply_role_strategy for : admin_flag in model User
7
+ [Rails::Assist::Logging]
8
+ apply_role_strategy for : admin_flag in model User
9
+ [Rails::Assist::Logging]
10
+ apply_role_strategy for : one_role in model User
11
+ [Rails::Assist::Logging]
12
+ apply_role_strategy for : admin_flag in model User
13
+ [Rails::Assist::Logging]
14
+ apply_role_strategy for : admin_flag in model User
15
+ [Rails::Assist::Logging]
16
+ apply_role_strategy for : admin_flag in model User
17
+ [Rails::Assist::Logging]
18
+ apply_role_strategy for : admin_flag in model User
19
+ [Rails::Assist::Logging]
20
+ apply_role_strategy for : admin_flag in model User
21
+ [Rails::Assist::Logging]
22
+ apply_role_strategy for : admin_flag in model User
23
+ [Rails::Assist::Logging]
24
+ apply_role_strategy for : admin_flag in model User
25
+ [Rails::Assist::Logging]
26
+ apply_role_strategy for : one_role in model User
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_active_record}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2010-09-06}
12
+ s.date = %q{2010-09-12}
13
13
  s.description = %q{Makes it easy to set a role strategy on your User model in Active Record}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -32,8 +32,9 @@ Gem::Specification.new do |s|
32
32
  "lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb",
33
33
  "lib/generators/active_record/roles_migration/templates/add_one_role_strategy.erb",
34
34
  "lib/generators/active_record/roles_migration/templates/add_role_string_strategy.erb",
35
- "lib/generators/active_record/roles_migration/templates/add_roles_mask_to_users_migration.erb",
35
+ "lib/generators/active_record/roles_migration/templates/add_roles_mask_strategy.erb",
36
36
  "lib/generators/active_record/setup/setup_generator.rb",
37
+ "lib/roles_active_record.rb",
37
38
  "lib/roles_active_record/base.rb",
38
39
  "lib/roles_active_record/namespaces.rb",
39
40
  "lib/roles_active_record/role.rb",
@@ -43,9 +44,9 @@ Gem::Specification.new do |s|
43
44
  "lib/roles_active_record/strategy/single/admin_flag.rb",
44
45
  "lib/roles_active_record/strategy/single/one_role.rb",
45
46
  "lib/roles_active_record/strategy/single/role_string.rb",
46
- "lib/roles_for_active_record.rb",
47
47
  "lib/views/_multi_role_selector.erb.html",
48
48
  "lib/views/_single_role_selector.erb.html",
49
+ "logging.log",
49
50
  "roles_active_record.gemspec",
50
51
  "roles_for_ar.gemspec",
51
52
  "sandbox/Rakefile",
@@ -62,6 +63,8 @@ Gem::Specification.new do |s|
62
63
  "spec/db/database.yml",
63
64
  "spec/fixtures/many_roles_setup.rb",
64
65
  "spec/fixtures/one_role_setup.rb",
66
+ "spec/generator_spec_helper.rb",
67
+ "spec/migration_spec_helper.rb",
65
68
  "spec/migrations/admin_flag/004_add_admin_flag_to_users.rb",
66
69
  "spec/migrations/many_roles/002_create_roles.rb",
67
70
  "spec/migrations/many_roles/003_create_user_roles.rb",
@@ -71,7 +74,7 @@ Gem::Specification.new do |s|
71
74
  "spec/migrations/roles_mask/005_add_roles_mask_to_users.rb",
72
75
  "spec/migrations/users/001_create_users.rb",
73
76
  "spec/roles_active_record/admin_flag_spec.rb",
74
- "spec/roles_active_record/generators/migration_spec_helper.rb",
77
+ "spec/roles_active_record/generators/roles_generator_spec.rb",
75
78
  "spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb",
76
79
  "spec/roles_active_record/generators/roles_migration/many_roles_spec.rb",
77
80
  "spec/roles_active_record/generators/roles_migration/one_role_spec.rb",
@@ -82,7 +85,15 @@ Gem::Specification.new do |s|
82
85
  "spec/roles_active_record/one_role_spec.rb",
83
86
  "spec/roles_active_record/role_string_spec.rb",
84
87
  "spec/roles_active_record/roles_mask_spec.rb",
85
- "spec/spec_helper.rb"
88
+ "spec/spec_helper.rb",
89
+ "tmp/rails/app/models/user.rb",
90
+ "tmp/rails/config/routes.rb",
91
+ "tmp/rails/db/migrations/20100912101428_add_admin_flag_strategy.rb",
92
+ "tmp/rails/db/migrations/20100912101429_remove_admin_flag_strategy.rb",
93
+ "tmp/rails/db/migrations/20100912105055_add_many_roles_strategy.rb",
94
+ "tmp/rails/db/migrations/20100912105323_add_one_role_strategy.rb",
95
+ "tmp/rails/db/migrations/20100912110044_add_role_string_strategy.rb",
96
+ "tmp/rails/db/migrations/20100912110249_add_roles_mask_strategy.rb"
86
97
  ]
87
98
  s.homepage = %q{http://github.com/kristianmandrup/roles_for_dm}
88
99
  s.rdoc_options = ["--charset=UTF-8"]
@@ -92,6 +103,8 @@ Gem::Specification.new do |s|
92
103
  s.test_files = [
93
104
  "spec/fixtures/many_roles_setup.rb",
94
105
  "spec/fixtures/one_role_setup.rb",
106
+ "spec/generator_spec_helper.rb",
107
+ "spec/migration_spec_helper.rb",
95
108
  "spec/migrations/admin_flag/004_add_admin_flag_to_users.rb",
96
109
  "spec/migrations/many_roles/002_create_roles.rb",
97
110
  "spec/migrations/many_roles/003_create_user_roles.rb",
@@ -101,7 +114,7 @@ Gem::Specification.new do |s|
101
114
  "spec/migrations/roles_mask/005_add_roles_mask_to_users.rb",
102
115
  "spec/migrations/users/001_create_users.rb",
103
116
  "spec/roles_active_record/admin_flag_spec.rb",
104
- "spec/roles_active_record/generators/migration_spec_helper.rb",
117
+ "spec/roles_active_record/generators/roles_generator_spec.rb",
105
118
  "spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb",
106
119
  "spec/roles_active_record/generators/roles_migration/many_roles_spec.rb",
107
120
  "spec/roles_active_record/generators/roles_migration/one_role_spec.rb",
data/roles_for_ar.gemspec CHANGED
File without changes
data/sandbox/Rakefile CHANGED
File without changes
File without changes
File without changes
File without changes
data/sandbox/database.log CHANGED
File without changes
data/sandbox/database.yml CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
data/sandbox/test.rb CHANGED
File without changes
data/spec/db/database.yml CHANGED
File without changes
File without changes
File without changes
@@ -0,0 +1,13 @@
1
+ require 'rspec'
2
+ require 'rspec/autorun'
3
+ require 'rails3_artifactor'
4
+ require 'generator-spec'
5
+ require 'roles_generic'
6
+
7
+ RSpec::Generator.configure do |config|
8
+ config.debug = false
9
+ config.remove_temp_dir = false # true
10
+ config.default_rails_root(__FILE__)
11
+ config.lib = File.dirname(__FILE__) + '/../lib'
12
+ config.logger = :stdout
13
+ end
@@ -0,0 +1,20 @@
1
+ require 'rspec'
2
+ require 'rspec/autorun'
3
+ require 'active_record'
4
+ require 'yaml'
5
+ require 'logger'
6
+ require 'database_cleaner'
7
+ require 'roles_active_record'
8
+ require 'migration-spec'
9
+ require 'generator-spec'
10
+
11
+ Rails::Migration::Assist.rails_root_dir = File.expand_path(File.dirname(__FILE__) + '/../tmp/rails')
12
+
13
+ RSpec::Generator.configure do |config|
14
+ config.debug = true
15
+ config.remove_temp_dir = false # true
16
+ config.default_rails_root(__FILE__)
17
+ config.lib = File.dirname(__FILE__) + '/../lib'
18
+ config.logger = :stdout
19
+ end
20
+
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,75 @@
1
+ require 'generator_spec_helper'
2
+ require_generator :active_record => :roles
3
+
4
+ # root_dir = Rails3::Assist::Directory.rails_root
5
+ root_dir = File.join(Rails.application.config.root_dir, 'rails')
6
+
7
+ describe 'role strategy generator: admin_flag' do
8
+ describe 'ORM: active_record' do
9
+ use_orm :active_record
10
+
11
+ before do
12
+ setup_generator 'datamapper_roles_generator' do
13
+ tests ActiveRecord::Generators::RolesGenerator
14
+ end
15
+ end
16
+
17
+ before :each do
18
+ remove_model :user
19
+ end
20
+
21
+ after :each do
22
+ # remove_model :user
23
+ end
24
+
25
+ it "should configure 'admin_flag' strategy without default roles" do
26
+ create_model :user do
27
+ '# content'
28
+ end
29
+ with_generator do |g|
30
+ arguments = "User --strategy admin_flag --roles special --no-default-roles"
31
+ puts "arguments: #{arguments}"
32
+ g.run_generator arguments.args
33
+ root_dir.should have_model :user do |clazz|
34
+ clazz.should include_module 'Roles::ActiveRecord'
35
+ clazz.should have_call :valid_roles_are, :args => ':special'
36
+ clazz.should have_call :strategy, :args => ":admin_flag"
37
+ end
38
+ end
39
+ end
40
+
41
+ it "should configure 'admin_flag' strategy" do
42
+ create_model :user do
43
+ '# content'
44
+ end
45
+ with_generator do |g|
46
+ arguments = "User --strategy admin_flag --roles admin user"
47
+ puts "arguments: #{arguments}"
48
+ g.run_generator arguments.args
49
+ root_dir.should have_model :user do |clazz|
50
+ clazz.should include_module 'Roles::ActiveRecord'
51
+ clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
52
+ clazz.should have_call :strategy, :args => ":admin_flag"
53
+ end
54
+ end
55
+ end
56
+
57
+ it "should configure 'one_role' strategy" do
58
+ create_model :user do
59
+ '# content'
60
+ end
61
+ with_generator do |g|
62
+ arguments = "User --strategy one_role --roles admin user"
63
+ puts "arguments: #{arguments}"
64
+ g.run_generator arguments.args
65
+ root_dir.should have_model :user do |clazz|
66
+ clazz.should include_module 'Roles::ActiveRecord'
67
+ clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
68
+ clazz.should have_call :role_class, :args => ":role"
69
+ clazz.should have_call :strategy, :args => ":one_role"
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+
@@ -1,12 +1,18 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
1
+ require 'migration_spec_helper'
2
2
 
3
3
  require_generator :active_record => :roles_migration
4
4
 
5
+ root_dir = Rails::Migration::Assist.rails_root_dir
6
+ Rails3::Assist::Directory.rails_root = root_dir
7
+ Rails::Migration::Assist.orm = :active_record
8
+
9
+ puts "root_dir: #{root_dir}"
10
+
5
11
  describe 'roles_migration_generator' do
6
- use_orm :active_record
7
- helpers :migration
12
+ use_orm :active_record
13
+ use_helper :migration
8
14
 
9
- before :each do
15
+ before do
10
16
  setup_generator 'roles_migration_generator' do
11
17
  tests ActiveRecord::Generators::RolesMigrationGenerator
12
18
  end
@@ -15,28 +21,26 @@ describe 'roles_migration_generator' do
15
21
  after :each do
16
22
  end
17
23
 
18
- # it "should generate migration 'add_admin_flag_strategy' for role strategy 'admin_flag'" do
19
- # with_generator do |g|
20
- # remove_migration :add_admin_flag_strategy
21
- # g.run_generator "User --strategy admin_flag".args
22
- #
23
- # g.should generate_migration :add_admin_flag_strategy do |content|
24
- # content.should have_migration :add_admin_flag_strategy do |klass|
25
- # klass.should have_up do |up|
26
- # up.should have_change_table :users do |tbl_content|
27
- # tbl_content.should have_add_column :admin_flag, :boolean
28
- # end
29
- # end
30
- #
31
- # klass.should have_down do |down|
32
- # down.should have_change_table :users do |tbl_content|
33
- # tbl_content.should have_remove_column :admin_flag
34
- # end
35
- # end
36
- # end
37
- # end
38
- # end # with
39
- # end
24
+ it "should generate migration 'add_admin_flag_strategy' for role strategy 'admin_flag'" do
25
+ with_generator do |g|
26
+ remove_migration :add_admin_flag_strategy
27
+ g.run_generator "User --strategy admin_flag".args
28
+
29
+ g.should generate_migration :add_admin_flag_strategy do |content|
30
+ content.should have_up do |up|
31
+ up.should have_change_table :users do |tbl_content|
32
+ tbl_content.should have_add_column :admin_flag, :boolean
33
+ end
34
+ end
35
+
36
+ content.should have_down do |down|
37
+ down.should have_change_table :users do |tbl_content|
38
+ tbl_content.should have_remove_column :admin_flag
39
+ end
40
+ end
41
+ end
42
+ end # with
43
+ end
40
44
 
41
45
  it "should generate reverse migration 'remove_admin_flag_strategy' for role strategy 'admin_flag'" do
42
46
  with_generator do |g|
@@ -44,17 +48,15 @@ describe 'roles_migration_generator' do
44
48
  g.run_generator "User --strategy admin_flag --reverse".args
45
49
 
46
50
  g.should generate_migration :remove_admin_flag_strategy do |content|
47
- content.should have_migration :remove_admin_flag_strategy do |klass|
48
- klass.should have_down do |down|
49
- down.should have_change_table :users do |tbl_content|
50
- tbl_content.should have_add_column :admin_flag, :boolean
51
- end
51
+ content.should have_down do |down|
52
+ down.should have_change_table :users do |tbl_content|
53
+ tbl_content.should have_add_column :admin_flag, :boolean
52
54
  end
53
-
54
- klass.should have_up do |up|
55
- up.should have_change_table :users do |tbl_content|
56
- tbl_content.should have_remove_column :admin_flag
57
- end
55
+ end
56
+
57
+ content.should have_up do |up|
58
+ up.should have_change_table :users do |tbl_content|
59
+ tbl_content.should have_remove_column :admin_flag
58
60
  end
59
61
  end
60
62
  end
@@ -1,9 +1,9 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
1
+ require 'migration_spec_helper'
2
2
  require_generator :active_record => :roles_migration
3
3
 
4
4
  describe 'roles_migration_generator' do
5
5
  use_orm :active_record
6
- helpers :migration
6
+ use_helper :migration
7
7
 
8
8
  before :each do
9
9
  setup_generator 'roles_migration_generator' do
@@ -20,36 +20,34 @@ describe 'roles_migration_generator' do
20
20
  g.run_generator "User --strategy many_roles".args
21
21
 
22
22
  g.should generate_migration :add_many_roles_strategy do |content|
23
- content.should have_migration :add_many_roles_strategy do |klass|
24
- klass.should have_class_self do |class_self|
25
- class_self.should have_method :up do |up|
26
- up.should have_call :create_user_roles
27
- up.should have_call :create_roles
28
- end
29
-
30
- class_self.should have_method :create_user_roles do |method|
31
- method.should have_create_table :user_roles do |tbl_content|
32
- tbl_content.should have_add_column :role_id, :integer
33
- tbl_content.should have_add_column :user_id, :integer
34
- tbl_content.should have_timestamps
35
- end
23
+ content.should have_class_self do |class_self|
24
+ class_self.should have_method :up do |up|
25
+ up.should have_call :create_user_roles
26
+ up.should have_call :create_roles
27
+ end
28
+
29
+ class_self.should have_method :create_user_roles do |method|
30
+ method.should have_create_table :user_roles do |tbl_content|
31
+ tbl_content.should have_add_column :role_id, :integer
32
+ tbl_content.should have_add_column :user_id, :integer
33
+ tbl_content.should have_timestamps
36
34
  end
35
+ end
37
36
 
38
- class_self.should have_method :create_roles do |method|
39
- method.should have_create_table :roles do |tbl_content|
40
- tbl_content.should have_add_column :name, :string
41
- tbl_content.should have_timestamps
42
- end
37
+ class_self.should have_method :create_roles do |method|
38
+ method.should have_create_table :roles do |tbl_content|
39
+ tbl_content.should have_add_column :name, :string
40
+ tbl_content.should have_timestamps
43
41
  end
44
-
45
- class_self.should have_method :down do |down|
46
- down.should have_call :drop_user_roles
47
- down.should have_call :drop_roles
48
- end
49
-
50
- class_self.should have_method :drop_user_roles
51
- class_self.should have_method :drop_roles
52
42
  end
43
+
44
+ class_self.should have_method :down do |down|
45
+ down.should have_call :drop_user_roles
46
+ down.should have_call :drop_roles
47
+ end
48
+
49
+ class_self.should have_method :drop_user_roles
50
+ class_self.should have_method :drop_roles
53
51
  end
54
52
  end
55
53
  end # with
@@ -1,9 +1,9 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
1
+ require 'migration_spec_helper'
2
2
  require_generator :active_record => :roles_migration
3
3
 
4
4
  describe 'roles_migration_generator' do
5
5
  use_orm :active_record
6
- helpers :migration
6
+ use_helper :migration
7
7
 
8
8
  before :each do
9
9
  setup_generator 'roles_migration_generator' do
@@ -21,36 +21,29 @@ describe 'roles_migration_generator' do
21
21
 
22
22
  g.run_generator [:user, %w{--strategy one_role}].args
23
23
 
24
- g.should generate_migration :add_role_to_users do |content|
25
- content.should have_migration :add_role_to_users do |klass|
26
- klass.should have_up do |up|
27
- up.should have_change_table :users do |tbl_content|
28
- tbl_content.should have_add_column :role_id, :integer
29
- end
24
+ # def up
25
+ # create_roles
26
+ # add_user_role
27
+ # end
28
+ #
29
+ # def down
30
+ # drop_roles
31
+ # remove_user_role
32
+ # end
33
+
34
+ g.should generate_migration :add_one_role_strategy do |content|
35
+ content.should have_class_self do |class_self|
36
+ class_self.should have_method :up do |up|
37
+ up.should have_call :create_roles
38
+ up.should have_call :add_user_role
30
39
  end
31
40
 
32
- klass.should have_down do |down|
33
- down.should have_change_table :users do |tbl_content|
34
- tbl_content.should have_remove_column :role_id
35
- end
41
+ class_self.should have_method :down do |down|
42
+ down.should have_call :drop_roles
43
+ down.should have_call :remove_user_role
36
44
  end
37
45
  end
38
- end
39
-
40
- g.should generate_migration :create_roles do |content|
41
- content.should have_migration :create_roles do |klass|
42
- klass.should have_up do |up|
43
- up.should have_create_table :roles do |tbl_content|
44
- tbl_content.should have_add_column :name, :string
45
- tbl_content.should have_timestamps
46
- end
47
- end
48
-
49
- klass.should have_down do |down|
50
- down.should have_drop_table :roles
51
- end
52
- end
53
- end
46
+ end
54
47
  end # with
55
48
  end
56
49
  end
@@ -1,9 +1,9 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
1
+ require 'migration_spec_helper'
2
2
  require_generator :active_record => :roles_migration
3
3
 
4
4
  describe 'roles_migration_generator' do
5
5
  use_orm :active_record
6
- helpers :migration
6
+ use_helper :migration
7
7
 
8
8
  before :each do
9
9
  setup_generator 'roles_migration_generator' do
@@ -15,26 +15,40 @@ describe 'roles_migration_generator' do
15
15
  end
16
16
 
17
17
 
18
- it "should generate migration 'add_inline_role_to_user' for role strategy 'role_string'" do
18
+ it "should generate migration 'add_role_string_strategy' for role strategy 'role_string'" do
19
+
20
+ # class AddRoleStringStrategy < ActiveRecord::Migration
21
+ # def self.up
22
+ # change_table :users do |t|
23
+ # t.string :role, :default => 'guest'
24
+ # end
25
+ # end
26
+ #
27
+ # def self.down
28
+ # change_table :users do |t|
29
+ # t.remove :role
30
+ # end
31
+ # end
32
+ # end
33
+
34
+
19
35
  with_generator do |g|
20
- remove_migration :add_inline_role_to_users
36
+ remove_migration :add_role_string_strategy
21
37
  g.run_generator [:user, %w{--strategy role_string}].args
22
38
 
23
- g.should generate_migration :add_inline_role_to_users do |content|
24
- content.should have_migration :add_inline_role_to_users do |klass|
25
- klass.should have_up do |up|
26
- up.should have_change_table :users do |tbl_content|
27
- tbl_content.should have_add_column :role, :string
28
- end
39
+ g.should generate_migration :add_role_string_strategy do |content|
40
+ content.should have_up do |up|
41
+ up.should have_change_table :users do |tbl_content|
42
+ tbl_content.should have_add_column :role, :string
29
43
  end
44
+ end
30
45
 
31
- klass.should have_down do |down|
32
- down.should have_change_table :users do |tbl_content|
33
- tbl_content.should have_remove_column :role
34
- end
46
+ content.should have_down do |down|
47
+ down.should have_change_table :users do |tbl_content|
48
+ tbl_content.should have_remove_column :role
35
49
  end
36
50
  end
37
- end
51
+ end
38
52
  end # with
39
53
  end
40
54
  end
@@ -1,9 +1,9 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
1
+ require 'migration_spec_helper'
2
2
  require_generator :active_record => :roles_migration
3
3
 
4
4
  describe 'roles_migration_generator' do
5
5
  use_orm :active_record
6
- helpers :migration
6
+ use_helper :migration
7
7
 
8
8
  before :each do
9
9
  setup_generator 'roles_migration_generator' do
@@ -11,23 +11,21 @@ describe 'roles_migration_generator' do
11
11
  end
12
12
  end
13
13
 
14
- it "should generate migration 'add_roles_mask_to_users' for role strategy 'roles_mask'" do
14
+ it "should generate migration 'add_roles_mask_strategy' for role strategy 'roles_mask'" do
15
15
  with_generator do |g|
16
- remove_migration :add_roles_mask_to_users
16
+ remove_migration :add_roles_mask_strategy
17
17
  g.run_generator [:user, %w{--strategy roles_mask}].args
18
18
 
19
- g.should generate_migration :add_roles_mask_to_users do |content|
20
- content.should have_migration :add_roles_mask_to_users do |klass|
21
- klass.should have_up_method do |up|
22
- up.should have_change_table :users do |tbl_content|
23
- tbl_content.should have_add_column :roles_mask, :integer
24
- end
19
+ g.should generate_migration :add_roles_mask_strategy do |content|
20
+ content.should have_up_method do |up|
21
+ up.should have_change_table :users do |tbl_content|
22
+ tbl_content.should have_add_column :roles_mask, :integer
25
23
  end
24
+ end
26
25
 
27
- klass.should have_down_method do |down|
28
- down.should have_change_table :users do |tbl_content|
29
- tbl_content.should have_remove_column :roles_mask
30
- end
26
+ content.should have_down_method do |down|
27
+ down.should have_change_table :users do |tbl_content|
28
+ tbl_content.should have_remove_column :roles_mask
31
29
  end
32
30
  end
33
31
  end
File without changes
File without changes
File without changes
File without changes
data/spec/spec_helper.rb CHANGED
File without changes
@@ -0,0 +1,7 @@
1
+ class User < ActiveRecord::Base
2
+ include Roles::ActiveRecord
3
+ strategy :one_role, :default
4
+ role_class :role
5
+ valid_roles_are :admin, :guest, :user
6
+
7
+ end
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,14 @@
1
+ class AddAdminFlagStrategy < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ change_table :users do |t|
5
+ t.boolean :admin_flag, :default => false
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ change_table :users do |t|
11
+ t.remove :admin_flag
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ class RemoveAdminFlagStrategy < ActiveRecord::Migration
2
+
3
+ def self.down
4
+ change_table :users do |t|
5
+ t.boolean :admin_flag, :default => false
6
+ end
7
+ end
8
+
9
+ def self.up
10
+ change_table :users do |t|
11
+ t.remove :admin_flag
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,40 @@
1
+ class AddManyRolesStrategy < ActiveRecord::Migration
2
+ class << self
3
+
4
+ def up
5
+ create_roles
6
+ create_user_roles
7
+ end
8
+
9
+ def down
10
+ drop_roles
11
+ drop_user_roles
12
+ end
13
+
14
+ protected
15
+
16
+ def create_user_roles
17
+ create_table :user_roles do |t|
18
+ t.integer :user_id
19
+ t.integer :role_id
20
+ t.timestamps
21
+ end
22
+ end
23
+
24
+ def drop_user_roles
25
+ drop_table :user_roles
26
+ end
27
+
28
+
29
+ def create_roles
30
+ create_table :roles do |t|
31
+ t.string :name
32
+ t.timestamps
33
+ end
34
+ end
35
+
36
+ def drop_roles
37
+ drop_table :roles
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ class AddOneRoleStrategy < ActiveRecord::Migration
2
+ class << self
3
+
4
+ def up
5
+ create_roles
6
+ add_user_role
7
+ end
8
+
9
+ def down
10
+ drop_roles
11
+ remove_user_role
12
+ end
13
+
14
+ protected
15
+
16
+ def add_user_role
17
+ change_table :users do |t|
18
+ t.integer :role_id
19
+ end
20
+ end
21
+
22
+ def remove_user_role
23
+ change_table :users do |t|
24
+ t.remove :role_id
25
+ end
26
+ end
27
+
28
+
29
+ def create_roles
30
+ create_table :roles do |t|
31
+ t.string :name
32
+ t.timestamps
33
+ end
34
+ end
35
+
36
+ def drop_roles
37
+ drop_table :roles
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,13 @@
1
+ class AddRoleStringStrategy < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :users do |t|
4
+ t.string :role, :default => 'guest'
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ change_table :users do |t|
10
+ t.remove :role
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class AddRolesMaskStrategy < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :users do |t|
4
+ t.integer :roles_mask, :default => 1
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ change_table :users do |t|
10
+ t.remove :roles_mask
11
+ end
12
+ end
13
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-06 00:00:00 +02:00
17
+ date: 2010-09-12 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -209,8 +209,9 @@ files:
209
209
  - lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
210
210
  - lib/generators/active_record/roles_migration/templates/add_one_role_strategy.erb
211
211
  - lib/generators/active_record/roles_migration/templates/add_role_string_strategy.erb
212
- - lib/generators/active_record/roles_migration/templates/add_roles_mask_to_users_migration.erb
212
+ - lib/generators/active_record/roles_migration/templates/add_roles_mask_strategy.erb
213
213
  - lib/generators/active_record/setup/setup_generator.rb
214
+ - lib/roles_active_record.rb
214
215
  - lib/roles_active_record/base.rb
215
216
  - lib/roles_active_record/namespaces.rb
216
217
  - lib/roles_active_record/role.rb
@@ -220,9 +221,9 @@ files:
220
221
  - lib/roles_active_record/strategy/single/admin_flag.rb
221
222
  - lib/roles_active_record/strategy/single/one_role.rb
222
223
  - lib/roles_active_record/strategy/single/role_string.rb
223
- - lib/roles_for_active_record.rb
224
224
  - lib/views/_multi_role_selector.erb.html
225
225
  - lib/views/_single_role_selector.erb.html
226
+ - logging.log
226
227
  - roles_active_record.gemspec
227
228
  - roles_for_ar.gemspec
228
229
  - sandbox/Rakefile
@@ -239,6 +240,8 @@ files:
239
240
  - spec/db/database.yml
240
241
  - spec/fixtures/many_roles_setup.rb
241
242
  - spec/fixtures/one_role_setup.rb
243
+ - spec/generator_spec_helper.rb
244
+ - spec/migration_spec_helper.rb
242
245
  - spec/migrations/admin_flag/004_add_admin_flag_to_users.rb
243
246
  - spec/migrations/many_roles/002_create_roles.rb
244
247
  - spec/migrations/many_roles/003_create_user_roles.rb
@@ -248,7 +251,7 @@ files:
248
251
  - spec/migrations/roles_mask/005_add_roles_mask_to_users.rb
249
252
  - spec/migrations/users/001_create_users.rb
250
253
  - spec/roles_active_record/admin_flag_spec.rb
251
- - spec/roles_active_record/generators/migration_spec_helper.rb
254
+ - spec/roles_active_record/generators/roles_generator_spec.rb
252
255
  - spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb
253
256
  - spec/roles_active_record/generators/roles_migration/many_roles_spec.rb
254
257
  - spec/roles_active_record/generators/roles_migration/one_role_spec.rb
@@ -260,6 +263,14 @@ files:
260
263
  - spec/roles_active_record/role_string_spec.rb
261
264
  - spec/roles_active_record/roles_mask_spec.rb
262
265
  - spec/spec_helper.rb
266
+ - tmp/rails/app/models/user.rb
267
+ - tmp/rails/config/routes.rb
268
+ - tmp/rails/db/migrations/20100912101428_add_admin_flag_strategy.rb
269
+ - tmp/rails/db/migrations/20100912101429_remove_admin_flag_strategy.rb
270
+ - tmp/rails/db/migrations/20100912105055_add_many_roles_strategy.rb
271
+ - tmp/rails/db/migrations/20100912105323_add_one_role_strategy.rb
272
+ - tmp/rails/db/migrations/20100912110044_add_role_string_strategy.rb
273
+ - tmp/rails/db/migrations/20100912110249_add_roles_mask_strategy.rb
263
274
  has_rdoc: true
264
275
  homepage: http://github.com/kristianmandrup/roles_for_dm
265
276
  licenses: []
@@ -295,6 +306,8 @@ summary: Implementation of Roles generic API for Active Record
295
306
  test_files:
296
307
  - spec/fixtures/many_roles_setup.rb
297
308
  - spec/fixtures/one_role_setup.rb
309
+ - spec/generator_spec_helper.rb
310
+ - spec/migration_spec_helper.rb
298
311
  - spec/migrations/admin_flag/004_add_admin_flag_to_users.rb
299
312
  - spec/migrations/many_roles/002_create_roles.rb
300
313
  - spec/migrations/many_roles/003_create_user_roles.rb
@@ -304,7 +317,7 @@ test_files:
304
317
  - spec/migrations/roles_mask/005_add_roles_mask_to_users.rb
305
318
  - spec/migrations/users/001_create_users.rb
306
319
  - spec/roles_active_record/admin_flag_spec.rb
307
- - spec/roles_active_record/generators/migration_spec_helper.rb
320
+ - spec/roles_active_record/generators/roles_generator_spec.rb
308
321
  - spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb
309
322
  - spec/roles_active_record/generators/roles_migration/many_roles_spec.rb
310
323
  - spec/roles_active_record/generators/roles_migration/one_role_spec.rb
@@ -1,17 +0,0 @@
1
- require 'rspec'
2
- require 'rspec/autorun'
3
- require 'active_record'
4
- require 'yaml'
5
- require 'logger'
6
- require 'database_cleaner'
7
- require 'roles_for_ar'
8
- require 'generator_spec'
9
-
10
- RSpec::Generator.configure do |config|
11
- config.debug = true
12
- config.remove_temp_dir = true
13
- config.default_rails_root(__FILE__)
14
- config.lib = File.dirname(__FILE__) + '/../../../lib'
15
- config.logger = :stdout
16
- end
17
-