role_play 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,4 +1,9 @@
1
- == 1.2.0
1
+ == 1.2.1 (2012.02.01)
2
+
3
+ * Remove use of ActiveSupport::Concern.
4
+ * Correct has_role? behavior after add_role on an invalid object.
5
+
6
+ == 1.2.0 (2011.01.27)
2
7
 
3
8
  * Use class_attribute to provide Rails 3.1+ support.
4
9
 
data/lib/role_play.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require 'role'
2
2
  require 'role_assignment'
3
- require 'active_support/concern'
4
3
 
5
4
  module RolePlay
6
- extend ActiveSupport::Concern
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
7
8
 
8
9
  module ClassMethods
9
10
  def has_roles(*roles)
@@ -23,32 +24,28 @@ module RolePlay
23
24
  end
24
25
  end
25
26
 
26
- module InstanceMethods
27
- def can_have_role?(role_name)
28
- available_roles.include?(role_name)
29
- end
30
-
31
- def has_role?(rolename)
32
- !self.roles.find_by_name(rolename.to_s).nil?
33
- end
27
+ def can_have_role?(role_name)
28
+ available_roles.include?(role_name)
29
+ end
34
30
 
35
- def add_role rolename
36
- return false unless can_have_role?(rolename)
37
- return true if has_role?(rolename)
38
- self.roles << Role.find_or_create_by_name(rolename.to_s)
39
- self.save
40
- end
31
+ def has_role?(rolename)
32
+ self.roles.map(&:name).include?(rolename.to_s)
33
+ end
41
34
 
42
- def remove_role name
43
- return true if !has_role?(name)
44
- role_assignment = self.role_assignments.detect { |x| x.role.name == name.to_s }
45
- role_assignment.destroy if role_assignment
46
- end
35
+ def add_role(rolename)
36
+ return false unless can_have_role?(rolename)
37
+ return true if has_role?(rolename)
38
+ self.roles << Role.find_or_create_by_name(rolename.to_s)
39
+ end
47
40
 
48
- def clear_roles
49
- role_assignments.clear
50
- end
41
+ def remove_role(name)
42
+ return true if !has_role?(name)
43
+ role = self.roles.detect { |r| r.name == name.to_s }
44
+ self.roles.delete(role) if role
45
+ end
51
46
 
47
+ def clear_roles
48
+ role_assignments.clear
52
49
  end
53
50
  end
54
51
 
data/role_play.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  Gem::Specification.new do |s|
4
- s.version = "1.2.0"
4
+ s.version = "1.2.1"
5
5
 
6
6
  s.name = 'role_play'
7
7
  s.summary = %q{Simple roles for ActiveRecord}
@@ -0,0 +1,58 @@
1
+ require 'test_helper'
2
+
3
+ class ValidatedRoleable < ActiveRecord::Base
4
+ has_roles :sample_role_1, :sample_role_2
5
+
6
+ validates :test_attr, :presence => true
7
+ end
8
+
9
+ class InvalidRecordRolePlayTest < ActiveSupport::TestCase
10
+ def setup
11
+ @record = ValidatedRoleable.new
12
+ assert !@record.valid?, "Precondition: record not valid"
13
+ end
14
+
15
+ def teardown
16
+ [Role, RoleAssignment, ValidatedRoleable].each {|klass| klass.delete_all }
17
+ end
18
+
19
+ def test_should_add_role_to_new_record
20
+ assert_difference "@record.roles.size", 1 do
21
+ @record.add_role(:sample_role_1)
22
+ end
23
+ end
24
+
25
+ # test that role? works on new records
26
+ def test_should_know_state_of_role_on_new_record
27
+ assert !@record.sample_role_1?, "Precondition: ? method is false"
28
+ @record.add_role(:sample_role_1)
29
+ assert @record.sample_role_1?, "? method should be correct now"
30
+ end
31
+
32
+ def test_should_save_association_when_saving_new_record
33
+ @record.add_role(:sample_role_1)
34
+ assert_equal 0, RoleAssignment.count, "nothing saved to DB yet"
35
+
36
+ @record.test_attr = 'foo'
37
+ assert @record.save, "save should succeed now"
38
+ assert_equal 1, RoleAssignment.count, "role assignment should save to DB"
39
+ end
40
+
41
+ def test_should_autosave_when_record_not_new_and_valid
42
+ @record.test_attr = 'foo'
43
+ assert @record.save, 'Precondition: record valid & saved'
44
+ assert_difference "RoleAssignment.count", 1 do
45
+ @record.add_role(:sample_role_1)
46
+ end
47
+ end
48
+
49
+ def test_should_autosave_when_record_not_new_and_ivalid
50
+ @record.test_attr = 'foo'
51
+ assert @record.save, 'Precondition: record valid'
52
+ @record.test_attr = nil
53
+ assert !@record.valid?, 'Precondition: back to an invalid state'
54
+ assert_difference "RoleAssignment.count", 1 do
55
+ @record.add_role(:sample_role_1)
56
+ end
57
+ end
58
+ end
@@ -140,7 +140,7 @@ class RolePlayTest < ActiveSupport::TestCase
140
140
  @roleable_sample.invalid_role?
141
141
  }
142
142
  end
143
-
143
+
144
144
  def test_should_clear_roles
145
145
  @roleable_sample.add_role(:sample_role_1)
146
146
  assert_difference "RoleAssignment.count", -1 do
data/test/test_helper.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  #require 'rails/generators/test_case'
3
3
  require 'test/unit'
4
4
  require 'rubygems'
5
+ require 'active_support/core_ext'
5
6
  require 'active_record'
6
7
 
7
8
  ENV['RAILS_ENV'] = 'test'
@@ -19,6 +20,10 @@ ActiveRecord::Schema.define(:version => 1) do
19
20
  create_table :roleable_samples do |t|
20
21
  end
21
22
 
23
+ create_table :validated_roleables do |t|
24
+ t.string :test_attr
25
+ end
26
+
22
27
  create_table :roles do |t|
23
28
  t.string :name
24
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: role_play
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-27 00:00:00.000000000 Z
12
+ date: 2012-02-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70301719859880 !ruby/object:Gem::Requirement
16
+ requirement: &70267020196080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -24,10 +24,10 @@ dependencies:
24
24
  version: '4.0'
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *70301719859880
27
+ version_requirements: *70267020196080
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: railties
30
- requirement: &70301719858760 !ruby/object:Gem::Requirement
30
+ requirement: &70267020195240 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
33
  - - ! '>='
@@ -38,10 +38,10 @@ dependencies:
38
38
  version: '4.0'
39
39
  type: :runtime
40
40
  prerelease: false
41
- version_requirements: *70301719858760
41
+ version_requirements: *70267020195240
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rake
44
- requirement: &70301719857620 !ruby/object:Gem::Requirement
44
+ requirement: &70267020194220 !ruby/object:Gem::Requirement
45
45
  none: false
46
46
  requirements:
47
47
  - - ! '>='
@@ -49,10 +49,10 @@ dependencies:
49
49
  version: 0.8.7
50
50
  type: :development
51
51
  prerelease: false
52
- version_requirements: *70301719857620
52
+ version_requirements: *70267020194220
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: sqlite3
55
- requirement: &70301719857080 !ruby/object:Gem::Requirement
55
+ requirement: &70267020193700 !ruby/object:Gem::Requirement
56
56
  none: false
57
57
  requirements:
58
58
  - - ! '>='
@@ -60,7 +60,7 @@ dependencies:
60
60
  version: '0'
61
61
  type: :development
62
62
  prerelease: false
63
- version_requirements: *70301719857080
63
+ version_requirements: *70267020193700
64
64
  description: Rails 3-ified version of the beloved plugin
65
65
  email: hello@mintdigital.com
66
66
  executables: []
@@ -87,6 +87,7 @@ files:
87
87
  - rails/init.rb
88
88
  - role_play.gemspec
89
89
  - tasks/has_roles_tasks.rake
90
+ - test/invalid_record_role_play_test.rb
90
91
  - test/migration_generator_test.rb
91
92
  - test/role_play_test.rb
92
93
  - test/test_helper.rb