permissive 0.2.4.alpha → 0.2.5.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4.alpha
1
+ 0.2.5.alpha
@@ -81,27 +81,24 @@ module Permissive
81
81
  eoc
82
82
  end
83
83
 
84
- if model.instance_methods.include?('role=') && !model.respond_to?(:permissive_role_defined?)
85
- puts "role= defined but role_defined? is false"
84
+ if model.column_names.include?('role')
86
85
  model.class_eval do
87
- def role_with_permissive=(role_name)
88
- self.permissions = self.class.permissions[:global].roles[role_name.to_s.downcase.to_sym]
89
- self.role_without_permissive = role_name.to_s.downcase
86
+ def role=(role_name)
87
+ if role_name
88
+ self.permissions = self.class.permissions[:global].roles[role_name.to_s.downcase.to_sym]
89
+ if respond_to?(:write_attribute)
90
+ write_attribute(:role, role_name.to_s.downcase.strip)
91
+ end
92
+ else
93
+ self.permissions = []
94
+ end
90
95
  end
91
96
  end
92
- model.alias_method_chain :role=, :permissive
93
97
  else
94
98
  model.class_eval do
95
99
  def role=(role_name)
96
100
  self.permissions = self.class.permissions[:global].roles[role_name.to_s.downcase.to_sym]
97
101
  end
98
-
99
- class << self
100
- def permissive_role_defined?
101
- true
102
- end
103
- protected :permissive_role_defined?
104
- end
105
102
  end
106
103
  end
107
104
  end
@@ -138,7 +135,7 @@ module Permissive
138
135
  @role = name.to_s.to_sym
139
136
  roles[@role] ||= []
140
137
  instance_eval(&block) if block_given?
141
- if model.instance_methods.include?('role') && !model.instance_methods.include?("is_#{name}?")
138
+ if model.column_names.include?('role') && !model.instance_methods.include?("is_#{name}?")
142
139
  model.class_eval <<-eoc
143
140
  def is_#{name}?
144
141
  role == #{name.to_s.downcase.inspect}
@@ -61,4 +61,26 @@ describe Permissive, "roles" do
61
61
  user.can?(:flee).should be_false
62
62
  end
63
63
  end
64
+
65
+ describe "for models that store the role" do
66
+ before :each do
67
+ PermissiveSpecHelper.db_up
68
+ end
69
+
70
+ it "should, like, actually store it" do
71
+ user = UserWithRole.create!(:role => 'hungry_person')
72
+ UserWithRole.find(user.id).role.should == 'hungry_person'
73
+ end
74
+
75
+ it "should meta-program an 'is_role?' method" do
76
+ user = UserWithRole.create!(:role => 'hungry_person')
77
+ user.should respond_to :is_hungry_person?
78
+ end
79
+
80
+ it "should return `true` from 'is_role?' when a user has the correct role" do
81
+ user = UserWithRole.create!(:role => 'hungry_person')
82
+ user.is_hungry_person?.should be_true
83
+ user.is_sleepy_person?.should be_false
84
+ end
85
+ end
64
86
  end
@@ -41,4 +41,5 @@ end
41
41
 
42
42
  # Setup the logging
43
43
  PermissiveSpecHelper.clear_log
44
- ActiveRecord::Base.logger = Logger.new(PermissiveSpecHelper.log_path)
44
+ ActiveRecord::Base.logger = Logger.new(PermissiveSpecHelper.log_path)
45
+ PermissiveSpecHelper.db_up
@@ -1,13 +1,23 @@
1
- # Setup some basic models to test with. We'll set permissions on both,
2
- # and then test :scope'd permissions through both.
3
- class Permissive::Organization < ActiveRecord::Base
4
- set_table_name :permissive_organizations
5
- end
6
-
7
- class Permissive::User < ActiveRecord::Base
8
- set_table_name :permissive_users
9
- end
10
-
11
- class UserWithRole < ActiveRecord::Base
12
- set_table_name :permissive_users_with_roles
13
- end
1
+ if defined?(Permissive)
2
+ # Setup some basic models to test with. We'll set permissions on both,
3
+ # and then test :scope'd permissions through both.
4
+ class Permissive::Organization < ActiveRecord::Base
5
+ set_table_name :permissive_organizations
6
+ end
7
+
8
+ class Permissive::User < ActiveRecord::Base
9
+ set_table_name :permissive_users
10
+ end
11
+
12
+ class UserWithRole < ActiveRecord::Base
13
+ set_table_name :permissive_users_with_roles
14
+
15
+ has_permissions do
16
+ to :eat, 0
17
+ to :sleep, 1
18
+
19
+ role(:hungry_person) { can :eat }
20
+ role(:sleepy_person) { can :sleep }
21
+ end
22
+ end
23
+ end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 4
8
+ - 5
9
9
  - alpha
10
- version: 0.2.4.alpha
10
+ version: 0.2.5.alpha
11
11
  platform: ruby
12
12
  authors:
13
13
  - Flip Sasser
@@ -48,10 +48,16 @@ files:
48
48
  - lib/permissive/permission.rb
49
49
  - lib/permissive/permission_definition.rb
50
50
  - rails/init.rb
51
+ - spec/checking_permissions_spec.rb
52
+ - spec/defining_permissions_spec.rb
51
53
  - spec/has_permissions_spec.rb
54
+ - spec/metaprogramming_spec.rb
52
55
  - spec/rcov.opts
56
+ - spec/roles_spec.rb
57
+ - spec/scoping_permissions_spec.rb
53
58
  - spec/spec.opts
54
59
  - spec/spec_helper.rb
60
+ - spec/spec_models.rb
55
61
  has_rdoc: true
56
62
  homepage: http://github.com/flipsasser/permissive
57
63
  licenses: []