roles_active_record 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.3.5
data/development.sqlite3 CHANGED
Binary file
@@ -4,11 +4,16 @@ class AddManyRolesStrategy < ActiveRecord::Migration
4
4
  def up
5
5
  create_roles
6
6
  create_user_roles
7
+ add_index :roles, :name, :unique => true
8
+
9
+ # insert_roles
7
10
  end
8
11
 
9
12
  def down
10
13
  drop_roles
11
14
  drop_user_roles
15
+
16
+ remove_index :roles, :name
12
17
  end
13
18
 
14
19
  protected
@@ -36,5 +41,14 @@ class AddManyRolesStrategy < ActiveRecord::Migration
36
41
  def drop_roles
37
42
  drop_table :roles
38
43
  end
44
+
45
+ def insert_roles
46
+ <%- roles_to_add.each do |role| -%>
47
+ begin
48
+ Role.create(:name => '<%= role %>')
49
+ rescue
50
+ end
51
+ <%- end -%>
52
+ end
39
53
  end
40
54
  end
@@ -3,13 +3,16 @@ class AddOneRoleStrategy < ActiveRecord::Migration
3
3
 
4
4
  def up
5
5
  create_roles
6
- add_user_role
7
- insert_roles
6
+ add_user_role
7
+ add_index :roles, :name, :unique => true
8
+ # insert_roles
8
9
  end
9
10
 
10
11
  def down
11
12
  drop_roles
12
13
  remove_user_role
14
+
15
+ remove_index :roles, :name
13
16
  end
14
17
 
15
18
  protected
@@ -29,7 +32,7 @@ class AddOneRoleStrategy < ActiveRecord::Migration
29
32
 
30
33
  def create_roles
31
34
  create_table :roles do |t|
32
- t.string :name
35
+ t.string :name, :null => false
33
36
  t.timestamps
34
37
  end
35
38
  end
@@ -40,7 +43,10 @@ class AddOneRoleStrategy < ActiveRecord::Migration
40
43
 
41
44
  def insert_roles
42
45
  <%- roles_to_add.each do |role| -%>
43
- Role.create!(:name => '<%= role %>')
46
+ begin
47
+ Role.create(:name => '<%= role %>')
48
+ rescue
49
+ end
44
50
  <%- end -%>
45
51
  end
46
52
  end
@@ -1,13 +1,13 @@
1
1
  class AddRoleStringStrategy < ActiveRecord::Migration
2
2
  def self.up
3
3
  change_table :<%= table_name %> do |t|
4
- t.string :role, :default => 'guest'
4
+ t.string :role_string, :default => 'guest', :null => false
5
5
  end
6
6
  end
7
7
 
8
8
  def self.down
9
9
  change_table :<%= table_name %> do |t|
10
- t.remove :role
10
+ t.remove :role_string
11
11
  end
12
12
  end
13
13
  end
@@ -2,12 +2,13 @@ module Roles::Base
2
2
  def valid_roles_are(*role_list)
3
3
  strategy_class.valid_roles = role_list.to_symbols
4
4
  if role_class_name
5
- "Create Roles: #{role_list}"
6
5
  role_list.each do |name|
7
- role_class_name.create(:name => name.to_s)
6
+ begin
7
+ role_class_name.create(:name => name.to_s) # if !role_class_name.where(:name => name.to_s).first
8
+ rescue
9
+ puts "Role name: #{name} is a duplicate"
10
+ end
8
11
  end
9
- else
10
- "Using Inline roles"
11
12
  end
12
13
  end
13
14
  end
@@ -4,6 +4,7 @@ class Role < ActiveRecord::Base
4
4
  scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
5
5
  has_many :users, :through => :user_roles
6
6
  has_many :user_roles
7
+ validates :name, :uniqueness => true
7
8
  end
8
9
 
9
10
  class UserRole < ActiveRecord::Base
@@ -3,6 +3,7 @@ require 'roles_active_record/strategy/single'
3
3
  class Role < ActiveRecord::Base
4
4
  scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
5
5
  has_many :users
6
+ validates :name, :uniqueness => true
6
7
  end
7
8
 
8
9
  module RoleStrategy::ActiveRecord
@@ -20,7 +20,7 @@ module RoleStrategy::ActiveRecord
20
20
  end
21
21
 
22
22
  def in_any_role(*role_names)
23
- where(:role_attribute.in => role_names)
23
+ where(role_attribute.in => role_names)
24
24
  end
25
25
  end
26
26
 
@@ -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.3.4"
8
+ s.version = "0.3.5"
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-12-17}
12
+ s.date = %q{2010-12-19}
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 = [
@@ -63,14 +63,15 @@ Gem::Specification.new do |s|
63
63
  "sandbox/test.rb",
64
64
  "spec/db/database.yml",
65
65
  "spec/fixtures/many_roles_setup.rb",
66
+ "spec/fixtures/many_roles_setup_unique_check.rb",
66
67
  "spec/fixtures/one_role_setup.rb",
68
+ "spec/fixtures/one_role_setup_unique_check.rb",
67
69
  "spec/generator_spec_helper.rb",
68
70
  "spec/migration_spec_helper.rb",
69
71
  "spec/migrations/admin_flag/004_add_admin_flag_to_users.rb",
70
72
  "spec/migrations/many_roles/002_create_roles.rb",
71
73
  "spec/migrations/many_roles/003_create_user_roles.rb",
72
- "spec/migrations/one_role/002_create_roles.rb",
73
- "spec/migrations/one_role/003_add_role_to_users.rb",
74
+ "spec/migrations/one_role/002_add_one_role_strategy.rb",
74
75
  "spec/migrations/role_string/002_add_role_string_to_users.rb",
75
76
  "spec/migrations/roles_mask/005_add_roles_mask_to_users.rb",
76
77
  "spec/migrations/users/001_create_users.rb",
@@ -84,9 +85,11 @@ Gem::Specification.new do |s|
84
85
  "spec/roles_active_record/strategy/api.rb",
85
86
  "spec/roles_active_record/strategy/api_examples.rb",
86
87
  "spec/roles_active_record/strategy/multi/many_roles_spec.rb",
88
+ "spec/roles_active_record/strategy/multi/many_roles_unique_spec.rb",
87
89
  "spec/roles_active_record/strategy/multi/roles_mask_spec.rb",
88
90
  "spec/roles_active_record/strategy/single/admin_flag_spec.rb",
89
91
  "spec/roles_active_record/strategy/single/one_role_spec.rb",
92
+ "spec/roles_active_record/strategy/single/one_role_unique_spec.rb",
90
93
  "spec/roles_active_record/strategy/single/role_string_spec.rb",
91
94
  "spec/roles_active_record/strategy/user_setup.rb",
92
95
  "spec/spec_helper.rb",
@@ -105,14 +108,15 @@ Gem::Specification.new do |s|
105
108
  s.summary = %q{Implementation of Roles generic API for Active Record}
106
109
  s.test_files = [
107
110
  "spec/fixtures/many_roles_setup.rb",
111
+ "spec/fixtures/many_roles_setup_unique_check.rb",
108
112
  "spec/fixtures/one_role_setup.rb",
113
+ "spec/fixtures/one_role_setup_unique_check.rb",
109
114
  "spec/generator_spec_helper.rb",
110
115
  "spec/migration_spec_helper.rb",
111
116
  "spec/migrations/admin_flag/004_add_admin_flag_to_users.rb",
112
117
  "spec/migrations/many_roles/002_create_roles.rb",
113
118
  "spec/migrations/many_roles/003_create_user_roles.rb",
114
- "spec/migrations/one_role/002_create_roles.rb",
115
- "spec/migrations/one_role/003_add_role_to_users.rb",
119
+ "spec/migrations/one_role/002_add_one_role_strategy.rb",
116
120
  "spec/migrations/role_string/002_add_role_string_to_users.rb",
117
121
  "spec/migrations/roles_mask/005_add_roles_mask_to_users.rb",
118
122
  "spec/migrations/users/001_create_users.rb",
@@ -126,9 +130,11 @@ Gem::Specification.new do |s|
126
130
  "spec/roles_active_record/strategy/api.rb",
127
131
  "spec/roles_active_record/strategy/api_examples.rb",
128
132
  "spec/roles_active_record/strategy/multi/many_roles_spec.rb",
133
+ "spec/roles_active_record/strategy/multi/many_roles_unique_spec.rb",
129
134
  "spec/roles_active_record/strategy/multi/roles_mask_spec.rb",
130
135
  "spec/roles_active_record/strategy/single/admin_flag_spec.rb",
131
136
  "spec/roles_active_record/strategy/single/one_role_spec.rb",
137
+ "spec/roles_active_record/strategy/single/one_role_unique_spec.rb",
132
138
  "spec/roles_active_record/strategy/single/role_string_spec.rb",
133
139
  "spec/roles_active_record/strategy/user_setup.rb",
134
140
  "spec/spec_helper.rb"
@@ -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, :user, :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, :user, :guest
8
+ end
@@ -0,0 +1,52 @@
1
+ class AddOneRoleStrategy < ActiveRecord::Migration
2
+ class << self
3
+
4
+ def up
5
+ create_roles
6
+ add_user_role
7
+ add_index :roles, :name, :unique => true
8
+ # insert_roles
9
+ end
10
+
11
+ def down
12
+ drop_roles
13
+ remove_user_role
14
+
15
+ remove_index :roles, :name
16
+ end
17
+
18
+ protected
19
+
20
+ def add_user_role
21
+ change_table :users do |t|
22
+ t.integer :role_id
23
+ end
24
+ end
25
+
26
+ def remove_user_role
27
+ change_table :users do |t|
28
+ t.remove :role_id
29
+ end
30
+ end
31
+
32
+ def create_roles
33
+ create_table :roles do |t|
34
+ t.string :name, :null => false
35
+ t.timestamps
36
+ end
37
+ end
38
+
39
+ def drop_roles
40
+ drop_table :roles
41
+ end
42
+
43
+ def insert_roles
44
+ roles_to_add.each do |role|
45
+ begin
46
+ Role.create(:name => "#{role}") # if !Role.where(:name => name.to_s).first
47
+ rescue
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -209,5 +209,5 @@ describe "Roles for Active Record: #{api_name}" do
209
209
  @guest_user.remove_role :admin
210
210
  @guest_user.has_role?(:admin).should_not be_true
211
211
  end
212
- end
212
+ end
213
213
  end
@@ -1,13 +1,6 @@
1
1
  require 'spec_helper'
2
2
  use_roles_strategy :many_roles
3
3
 
4
- class User < ActiveRecord::Base
5
- include Roles::ActiveRecord
6
-
7
- strategy :many_roles, :default
8
- valid_roles_are :admin, :guest, :user
9
- end
10
-
11
4
  def api_migrate
12
5
  migrate('many_roles')
13
6
  end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :many_roles
3
+
4
+ def api_migrate
5
+ migrate('many_roles')
6
+ end
7
+
8
+ def api_fixture
9
+ load 'fixtures/many_roles_setup_unique_check.rb'
10
+ end
11
+
12
+ def api_name
13
+ :many_roles
14
+ end
15
+
16
+ describe "Roles for Active Record: #{api_name}" do
17
+ require "roles_active_record/strategy/user_setup.rb"
18
+
19
+ before do
20
+ api_fixture
21
+ default_user_setup
22
+ end
23
+
24
+ describe '#valid_roles' do
25
+ context ':guest role twice in list of valid roles' do
26
+ it 'roles table should not have duplicate role :guest' do
27
+ Role.all.map(&:name).select{|n| n == 'guest'}.size.should == 1
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+
35
+
@@ -14,6 +14,3 @@ def api_name
14
14
  end
15
15
 
16
16
  load 'roles_active_record/strategy/api_examples.rb'
17
-
18
-
19
-
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :one_role
3
+
4
+ def api_fixture
5
+ load 'fixtures/one_role_setup_unique_check.rb'
6
+ end
7
+
8
+ def api_migrate
9
+ migrate('one_role')
10
+ end
11
+
12
+ def api_name
13
+ :one_role
14
+ end
15
+
16
+ describe "Roles for Active Record: #{api_name}" do
17
+ require "roles_active_record/strategy/user_setup.rb"
18
+
19
+ before do
20
+ api_fixture
21
+ default_user_setup
22
+ end
23
+
24
+ describe '#valid_roles' do
25
+ context ':guest role twice in list of valid roles' do
26
+ it 'roles table should not have duplicate role :guest' do
27
+ Role.all.map(&:name).select{|n| n == 'guest'}.size.should == 1
28
+ end
29
+ end
30
+ end
31
+ end
data/spec/spec_helper.rb CHANGED
@@ -68,7 +68,8 @@ RSpec.configure do |config|
68
68
 
69
69
  config.before(:each) do
70
70
  DatabaseCleaner.start
71
- migrate('users')
71
+ migrate('users')
72
+ api_migrate
72
73
  end
73
74
 
74
75
  config.after(:each) do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 4
9
- version: 0.3.4
8
+ - 5
9
+ version: 0.3.5
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-12-17 00:00:00 +01:00
17
+ date: 2010-12-19 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -385,14 +385,15 @@ files:
385
385
  - sandbox/test.rb
386
386
  - spec/db/database.yml
387
387
  - spec/fixtures/many_roles_setup.rb
388
+ - spec/fixtures/many_roles_setup_unique_check.rb
388
389
  - spec/fixtures/one_role_setup.rb
390
+ - spec/fixtures/one_role_setup_unique_check.rb
389
391
  - spec/generator_spec_helper.rb
390
392
  - spec/migration_spec_helper.rb
391
393
  - spec/migrations/admin_flag/004_add_admin_flag_to_users.rb
392
394
  - spec/migrations/many_roles/002_create_roles.rb
393
395
  - spec/migrations/many_roles/003_create_user_roles.rb
394
- - spec/migrations/one_role/002_create_roles.rb
395
- - spec/migrations/one_role/003_add_role_to_users.rb
396
+ - spec/migrations/one_role/002_add_one_role_strategy.rb
396
397
  - spec/migrations/role_string/002_add_role_string_to_users.rb
397
398
  - spec/migrations/roles_mask/005_add_roles_mask_to_users.rb
398
399
  - spec/migrations/users/001_create_users.rb
@@ -406,9 +407,11 @@ files:
406
407
  - spec/roles_active_record/strategy/api.rb
407
408
  - spec/roles_active_record/strategy/api_examples.rb
408
409
  - spec/roles_active_record/strategy/multi/many_roles_spec.rb
410
+ - spec/roles_active_record/strategy/multi/many_roles_unique_spec.rb
409
411
  - spec/roles_active_record/strategy/multi/roles_mask_spec.rb
410
412
  - spec/roles_active_record/strategy/single/admin_flag_spec.rb
411
413
  - spec/roles_active_record/strategy/single/one_role_spec.rb
414
+ - spec/roles_active_record/strategy/single/one_role_unique_spec.rb
412
415
  - spec/roles_active_record/strategy/single/role_string_spec.rb
413
416
  - spec/roles_active_record/strategy/user_setup.rb
414
417
  - spec/spec_helper.rb
@@ -454,14 +457,15 @@ specification_version: 3
454
457
  summary: Implementation of Roles generic API for Active Record
455
458
  test_files:
456
459
  - spec/fixtures/many_roles_setup.rb
460
+ - spec/fixtures/many_roles_setup_unique_check.rb
457
461
  - spec/fixtures/one_role_setup.rb
462
+ - spec/fixtures/one_role_setup_unique_check.rb
458
463
  - spec/generator_spec_helper.rb
459
464
  - spec/migration_spec_helper.rb
460
465
  - spec/migrations/admin_flag/004_add_admin_flag_to_users.rb
461
466
  - spec/migrations/many_roles/002_create_roles.rb
462
467
  - spec/migrations/many_roles/003_create_user_roles.rb
463
- - spec/migrations/one_role/002_create_roles.rb
464
- - spec/migrations/one_role/003_add_role_to_users.rb
468
+ - spec/migrations/one_role/002_add_one_role_strategy.rb
465
469
  - spec/migrations/role_string/002_add_role_string_to_users.rb
466
470
  - spec/migrations/roles_mask/005_add_roles_mask_to_users.rb
467
471
  - spec/migrations/users/001_create_users.rb
@@ -475,9 +479,11 @@ test_files:
475
479
  - spec/roles_active_record/strategy/api.rb
476
480
  - spec/roles_active_record/strategy/api_examples.rb
477
481
  - spec/roles_active_record/strategy/multi/many_roles_spec.rb
482
+ - spec/roles_active_record/strategy/multi/many_roles_unique_spec.rb
478
483
  - spec/roles_active_record/strategy/multi/roles_mask_spec.rb
479
484
  - spec/roles_active_record/strategy/single/admin_flag_spec.rb
480
485
  - spec/roles_active_record/strategy/single/one_role_spec.rb
486
+ - spec/roles_active_record/strategy/single/one_role_unique_spec.rb
481
487
  - spec/roles_active_record/strategy/single/role_string_spec.rb
482
488
  - spec/roles_active_record/strategy/user_setup.rb
483
489
  - spec/spec_helper.rb
@@ -1,12 +0,0 @@
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
@@ -1,13 +0,0 @@
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