roles_active_record 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +13 -0
  5. data/LICENSE +20 -0
  6. data/README.markdown +178 -0
  7. data/Rakefile +28 -0
  8. data/VERSION +1 -0
  9. data/development.sqlite3 +0 -0
  10. data/lib/generators/active_record/roles/roles_generator.rb +46 -0
  11. data/lib/generators/active_record/roles_migration/roles_migration_generator.rb +54 -0
  12. data/lib/generators/active_record/roles_migration/templates/add_admin_flag_strategy.erb +14 -0
  13. data/lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb +40 -0
  14. data/lib/generators/active_record/roles_migration/templates/add_one_role_strategy.erb +40 -0
  15. data/lib/generators/active_record/roles_migration/templates/add_role_string_strategy.erb +13 -0
  16. data/lib/generators/active_record/roles_migration/templates/add_roles_mask_to_users_migration.erb +13 -0
  17. data/lib/generators/active_record/setup/setup_generator.rb +40 -0
  18. data/lib/roles_active_record/base.rb +19 -0
  19. data/lib/roles_active_record/namespaces.rb +12 -0
  20. data/lib/roles_active_record/role.rb +24 -0
  21. data/lib/roles_active_record/strategy/multi/many_roles.rb +70 -0
  22. data/lib/roles_active_record/strategy/multi/roles_mask.rb +84 -0
  23. data/lib/roles_active_record/strategy/single/admin_flag.rb +53 -0
  24. data/lib/roles_active_record/strategy/single/one_role.rb +49 -0
  25. data/lib/roles_active_record/strategy/single/role_string.rb +51 -0
  26. data/lib/roles_active_record/strategy.rb +15 -0
  27. data/lib/roles_for_active_record.rb +13 -0
  28. data/lib/views/_multi_role_selector.erb.html +5 -0
  29. data/lib/views/_single_role_selector.erb.html +1 -0
  30. data/roles_active_record.gemspec +161 -0
  31. data/roles_for_ar.gemspec +165 -0
  32. data/sandbox/Rakefile +16 -0
  33. data/sandbox/add_role_to_users_migration.erb +13 -0
  34. data/sandbox/create_roles_migration.erb +12 -0
  35. data/sandbox/create_user_roles_migration.erb +13 -0
  36. data/sandbox/database.log +47 -0
  37. data/sandbox/database.yml +4 -0
  38. data/sandbox/development.sqlite3 +0 -0
  39. data/sandbox/migrate/201002508_create_roles.rb +11 -0
  40. data/sandbox/migrate/20102507_create_users.rb +11 -0
  41. data/sandbox/model_base.rb +46 -0
  42. data/sandbox/test.rb +13 -0
  43. data/spec/db/database.yml +4 -0
  44. data/spec/fixtures/many_roles_setup.rb +8 -0
  45. data/spec/fixtures/one_role_setup.rb +8 -0
  46. data/spec/migrations/admin_flag/004_add_admin_flag_to_users.rb +15 -0
  47. data/spec/migrations/many_roles/002_create_roles.rb +13 -0
  48. data/spec/migrations/many_roles/003_create_user_roles.rb +13 -0
  49. data/spec/migrations/one_role/002_create_roles.rb +12 -0
  50. data/spec/migrations/one_role/003_add_role_to_users.rb +13 -0
  51. data/spec/migrations/role_string/002_add_role_string_to_users.rb +13 -0
  52. data/spec/migrations/roles_mask/005_add_roles_mask_to_users.rb +13 -0
  53. data/spec/migrations/users/001_create_users.rb +11 -0
  54. data/spec/roles_active_record/admin_flag_spec.rb +71 -0
  55. data/spec/roles_active_record/generators/migration_spec_helper.rb +17 -0
  56. data/spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb +67 -0
  57. data/spec/roles_active_record/generators/roles_migration/many_roles_spec.rb +57 -0
  58. data/spec/roles_active_record/generators/roles_migration/one_role_spec.rb +56 -0
  59. data/spec/roles_active_record/generators/roles_migration/role_string_spec.rb +40 -0
  60. data/spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb +36 -0
  61. data/spec/roles_active_record/generators/setup_generator_spec.rb +40 -0
  62. data/spec/roles_active_record/many_roles_spec.rb +69 -0
  63. data/spec/roles_active_record/one_role_spec.rb +68 -0
  64. data/spec/roles_active_record/role_string_spec.rb +68 -0
  65. data/spec/roles_active_record/roles_mask_spec.rb +68 -0
  66. data/spec/spec_helper.rb +57 -0
  67. metadata +318 -0
@@ -0,0 +1,47 @@
1
+ D, [2010-08-03T11:17:25.097372 #5468] DEBUG -- : SQL (0.3ms)  SELECT name
2
+ FROM sqlite_master
3
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
4
+ 
5
+ D, [2010-08-03T11:17:25.097614 #5468] DEBUG -- : SQL (0.1ms) select sqlite_version(*)
6
+ D, [2010-08-03T11:17:25.099557 #5468] DEBUG -- : SQL (1.7ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
7
+ D, [2010-08-03T11:17:25.099704 #5468] DEBUG -- : SQL (0.0ms) PRAGMA index_list("schema_migrations")
8
+ D, [2010-08-03T11:17:25.101503 #5468] DEBUG -- : SQL (1.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
9
+ D, [2010-08-03T11:17:25.101691 #5468] DEBUG -- : SQL (0.1ms) SELECT name
10
+ FROM sqlite_master
11
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
12
+
13
+ D, [2010-08-03T11:18:24.658917 #5733] DEBUG -- : SQL (0.8ms)  SELECT name
14
+ FROM sqlite_master
15
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
16
+ 
17
+ D, [2010-08-03T11:19:32.203267 #5997] DEBUG -- : SQL (0.4ms)  SELECT name
18
+ FROM sqlite_master
19
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
20
+ 
21
+ D, [2010-08-03T11:21:50.864071 #6613] DEBUG -- : SQL (0.4ms)  SELECT name
22
+ FROM sqlite_master
23
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
24
+ 
25
+ D, [2010-08-03T11:21:50.866101 #6613] DEBUG -- : SQL (0.1ms) SELECT name
26
+ FROM sqlite_master
27
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
28
+
29
+ D, [2010-08-03T11:21:50.867280 #6613] DEBUG -- : SQL (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
30
+ I, [2010-08-03T11:21:50.867334 #6613] INFO -- : Migrating to CreateUsers (20102507)
31
+ D, [2010-08-03T11:21:50.867474 #6613] DEBUG -- : SQL (0.0ms) select sqlite_version(*)
32
+ D, [2010-08-03T11:21:50.870727 #6613] DEBUG -- : SQL (0.5ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) NOT NULL) 
33
+ D, [2010-08-03T11:21:50.900844 #6613] DEBUG -- : SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ('20102507')
34
+ D, [2010-08-03T14:16:49.368569 #54191] DEBUG -- : SQL (0.4ms)  SELECT name
35
+ FROM sqlite_master
36
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
37
+ 
38
+ D, [2010-08-03T14:16:49.387225 #54191] DEBUG -- : SQL (0.2ms) SELECT name
39
+ FROM sqlite_master
40
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
41
+
42
+ D, [2010-08-03T14:16:49.388472 #54191] DEBUG -- : SQL (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
43
+ I, [2010-08-03T14:16:49.388536 #54191] INFO -- : Migrating to CreateUsers (20102507)
44
+ I, [2010-08-03T14:16:49.388570 #54191] INFO -- : Migrating to CreateRoles (201002508)
45
+ D, [2010-08-03T14:16:49.388708 #54191] DEBUG -- : SQL (0.1ms) select sqlite_version(*)
46
+ D, [2010-08-03T14:16:49.406721 #54191] DEBUG -- : SQL (0.4ms) CREATE TABLE "roles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) NOT NULL) 
47
+ D, [2010-08-03T14:16:49.427264 #54191] DEBUG -- : SQL (13.7ms) INSERT INTO "schema_migrations" ("version") VALUES ('201002508')
@@ -0,0 +1,4 @@
1
+ adapter: sqlite3
2
+ database: development.sqlite3
3
+ pool: 5
4
+ timeout: 5000
Binary file
@@ -0,0 +1,11 @@
1
+ class CreateRoles < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :roles do |t|
4
+ t.column :name, :string, :null => false
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :roles
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.column :name, :string, :null => false
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :users
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ require 'require_all'
2
+ require 'active_support/inflector'
3
+ require_all File.dirname(__FILE__) + '/strategies'
4
+
5
+
6
+
7
+ module RoleModels::ActiveRecord
8
+ include RoleModels::Base
9
+ orm_name :active_record
10
+ end
11
+
12
+
13
+ module ActiveRecord
14
+ class Base
15
+ include RoleModels::ActiveRecord
16
+ end
17
+ end
18
+
19
+
20
+ class User
21
+ include RoleModels::Generic
22
+
23
+ attr_accessor :roles_mask
24
+ role_strategy :roles_mask
25
+ end
26
+
27
+ class User
28
+ include RoleModels::Generic::RolesMask
29
+ attr_accessor :roles_mask
30
+ end
31
+
32
+ class User < ActiveRecord::Base
33
+ role_strategy :roles_mask
34
+ end
35
+
36
+ class User
37
+ include MongoMapper::Document
38
+
39
+ role_strategy :roles_mask
40
+ end
41
+
42
+ class User
43
+ include Mongoid::Document
44
+
45
+ role_strategy :roles_mask
46
+ end
data/sandbox/test.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'active_record'
2
+ require 'yaml'
3
+ require 'logger'
4
+ path = File.dirname(__FILE__) + '/database.yml'
5
+ dbfile = File.open(path)
6
+ dbconfig = YAML::load(dbfile)
7
+ ActiveRecord::Base.establish_connection(dbconfig)
8
+ ActiveRecord::Base.logger = Logger.new(STDERR)
9
+
10
+ class User < ActiveRecord::Base
11
+ end
12
+
13
+ puts User.count
@@ -0,0 +1,4 @@
1
+ adapter: sqlite3
2
+ database: development.sqlite3
3
+ pool: 5
4
+ timeout: 5000
@@ -0,0 +1,8 @@
1
+ class User < ActiveRecord::Base
2
+ include Roles::ActiveRecord
3
+
4
+ strategy :many_roles, :default
5
+ role_class :role
6
+
7
+ valid_roles_are :admin, :guest
8
+ end
@@ -0,0 +1,8 @@
1
+ class User < ActiveRecord::Base
2
+ include Roles::ActiveRecord
3
+
4
+ strategy :one_role, :default
5
+ role_class :role
6
+
7
+ valid_roles_are :admin, :guest
8
+ end
@@ -0,0 +1,15 @@
1
+ class AddAdminFlagToUsers < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :users do |t|
4
+ t.boolean :admin_flag, :default => false
5
+ end
6
+ # add_column :users, :admin_flag, :boolean, :default => false
7
+ end
8
+
9
+ def self.down
10
+ change_table :users do |t|
11
+ t.remove :admin_flag
12
+ end
13
+ # remove_column :users, :admin_flag
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ class CreateRoles < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :roles do |t|
4
+ t.integer :role_id
5
+ t.string :name
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :roles
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class CreateUserRoles < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :user_roles do |t|
4
+ t.integer :user_id
5
+ t.integer :role_id
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :user_roles
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ class CreateRoles < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :roles do |t|
4
+ t.string :name
5
+ t.timestamps
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :roles
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ class AddRoleToUsers < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :users do |t|
4
+ t.integer :role_id
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ change_table :users do |t|
10
+ t.remove :role_id
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class AddRoleStringToUsers < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :users do |t|
4
+ t.string :role_string, :default => 'guest'
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ change_table :users do |t|
10
+ t.remove :role_string
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class AddRolesMaskToUsers < 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
@@ -0,0 +1,11 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.column :name, :string, :null => false
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :users
10
+ end
11
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :admin_flag
3
+
4
+ class User < ActiveRecord::Base
5
+ include Roles::ActiveRecord
6
+
7
+ strategy :admin_flag, :default
8
+ valid_roles_are :admin, :guest
9
+ end
10
+
11
+ describe "Roles for Active Record" do
12
+ before do
13
+ migrate('admin_flag')
14
+ end
15
+
16
+ before :each do
17
+ @user = User.create(:name => 'Kristian')
18
+ @user.roles = :guest
19
+ @user.save
20
+
21
+ @admin_user = User.create(:name => 'Admin user')
22
+ @admin_user.roles = :admin
23
+ @admin_user.save
24
+ end
25
+
26
+ describe "Admin flag strategy " do
27
+
28
+ describe '#in_role' do
29
+ it "should return first user maching role" do
30
+ User.in_role(:guest).first.name.should == 'Kristian'
31
+ User.in_role(:admin).first.name.should == 'Admin user'
32
+ end
33
+ end
34
+
35
+ describe 'roles API' do
36
+ it "should have admin user role to :admin" do
37
+ @admin_user.role.should == :admin
38
+ @admin_user.roles.should == [:admin]
39
+ @admin_user.admin?.should be_true
40
+
41
+ @admin_user.class.valid_roles.should == [:admin, :guest]
42
+
43
+ @admin_user.has_role?(:guest).should be_false
44
+
45
+ @admin_user.has_role?(:admin).should be_true
46
+ @admin_user.is?(:admin).should be_true
47
+ @admin_user.has_roles?(:admin).should be_true
48
+ @admin_user.has?(:admin).should be_true
49
+ end
50
+
51
+ it "should have user role to :guest" do
52
+ @user.roles.should == [:guest]
53
+ @user.admin?.should be_false
54
+
55
+ @user.has_role?(:guest).should be_true
56
+ @user.has_role?(:admin).should be_false
57
+ @user.is?(:admin).should be_false
58
+
59
+ @user.has_roles?(:guest).should be_true
60
+ @user.has?(:admin).should be_false
61
+ end
62
+
63
+ it "should set user role to :admin using =" do
64
+ @user.roles = :admin
65
+ @user.role.should == :admin
66
+ @user.has_role?(:admin).should be_true
67
+ end
68
+ end
69
+ end
70
+ end
71
+
@@ -0,0 +1,17 @@
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
+
@@ -0,0 +1,67 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
2
+
3
+ require_generator :active_record => :roles_migration
4
+
5
+ describe 'roles_migration_generator' do
6
+ use_orm :active_record
7
+ helpers :migration
8
+
9
+ before :each do
10
+ setup_generator 'roles_migration_generator' do
11
+ tests ActiveRecord::Generators::RolesMigrationGenerator
12
+ end
13
+ end
14
+
15
+ after :each do
16
+ end
17
+
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
40
+
41
+ it "should generate reverse migration 'remove_admin_flag_strategy' for role strategy 'admin_flag'" do
42
+ with_generator do |g|
43
+ remove_migration :remove_admin_flag_strategy
44
+ g.run_generator "User --strategy admin_flag --reverse".args
45
+
46
+ 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
52
+ 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
58
+ end
59
+ end
60
+ end
61
+ end # with
62
+ end
63
+
64
+ end
65
+
66
+
67
+
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
2
+ require_generator :active_record => :roles_migration
3
+
4
+ describe 'roles_migration_generator' do
5
+ use_orm :active_record
6
+ helpers :migration
7
+
8
+ before :each do
9
+ setup_generator 'roles_migration_generator' do
10
+ tests ActiveRecord::Generators::RolesMigrationGenerator
11
+ end
12
+ end
13
+
14
+ after :each do
15
+ end
16
+
17
+ it "should generate migrations 'create_roles' and 'create_user_roles' for role strategy 'many_roles'" do
18
+ with_generator do |g|
19
+ remove_migration :add_many_roles_strategy
20
+ g.run_generator "User --strategy many_roles".args
21
+
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
36
+ end
37
+
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
43
+ 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
+ end
53
+ end
54
+ end
55
+ end # with
56
+ end
57
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
2
+ require_generator :active_record => :roles_migration
3
+
4
+ describe 'roles_migration_generator' do
5
+ use_orm :active_record
6
+ helpers :migration
7
+
8
+ before :each do
9
+ setup_generator 'roles_migration_generator' do
10
+ tests ActiveRecord::Generators::RolesMigrationGenerator
11
+ end
12
+ end
13
+
14
+ after :each do
15
+ end
16
+
17
+
18
+ it "should generate migration 'add_role_to_user' and 'create_roles' for role strategy 'one_role'" do
19
+ with_generator do |g|
20
+ remove_migrations :add_role_to_users, :create_roles
21
+
22
+ g.run_generator [:user, %w{--strategy one_role}].args
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
30
+ end
31
+
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
36
+ end
37
+ 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
54
+ end # with
55
+ end
56
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
2
+ require_generator :active_record => :roles_migration
3
+
4
+ describe 'roles_migration_generator' do
5
+ use_orm :active_record
6
+ helpers :migration
7
+
8
+ before :each do
9
+ setup_generator 'roles_migration_generator' do
10
+ tests ActiveRecord::Generators::RolesMigrationGenerator
11
+ end
12
+ end
13
+
14
+ after :each do
15
+ end
16
+
17
+
18
+ it "should generate migration 'add_inline_role_to_user' for role strategy 'role_string'" do
19
+ with_generator do |g|
20
+ remove_migration :add_inline_role_to_users
21
+ g.run_generator [:user, %w{--strategy role_string}].args
22
+
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
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 :role
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end # with
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../migration_spec_helper')
2
+ require_generator :active_record => :roles_migration
3
+
4
+ describe 'roles_migration_generator' do
5
+ use_orm :active_record
6
+ helpers :migration
7
+
8
+ before :each do
9
+ setup_generator 'roles_migration_generator' do
10
+ tests ActiveRecord::Generators::RolesMigrationGenerator
11
+ end
12
+ end
13
+
14
+ it "should generate migration 'add_roles_mask_to_users' for role strategy 'roles_mask'" do
15
+ with_generator do |g|
16
+ remove_migration :add_roles_mask_to_users
17
+ g.run_generator [:user, %w{--strategy roles_mask}].args
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
25
+ end
26
+
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
31
+ end
32
+ end
33
+ end
34
+ end # with
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require_generator :setup
3
+
4
+ describe 'setup_generator' do
5
+ include RSpec::Rails::Orm::ActiveRecord
6
+ include RSpec::Rails::Migration
7
+
8
+ before :each do
9
+ setup_generator 'setup_generator' do
10
+ tests MigrationGenerator
11
+ end
12
+ end
13
+
14
+ after :each do
15
+ end
16
+
17
+ it "should generate role migrations" do
18
+ with_generator do |g|
19
+ name = 'auth_assistant::setup'
20
+ remove_migration 'create_users'
21
+ g.run_generator [name]
22
+ g.should generate_migration name do |content|
23
+ content.should have_migration name do |klass|
24
+ klass.should have_up do |up|
25
+ up.should have_create_table :users do |user_tbl|
26
+ user_tbl.should have_columns :name => :string, :age => :string
27
+ end
28
+ end
29
+
30
+ klass.should have_down do |up|
31
+ up.should have_drop_table :users
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+