johnsbrn-has_permission 0.2.1 → 0.2.2
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.yml +1 -1
- data/lib/permission/base.rb +8 -0
- data/test/has_permission_test.rb +8 -0
- data/test/test_helper.rb +11 -3
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/permission/base.rb
CHANGED
@@ -36,6 +36,14 @@ module Permission
|
|
36
36
|
object.update_attributes(attributes.reject{|key,value| !can_write?(key) })
|
37
37
|
end
|
38
38
|
|
39
|
+
def attribute_names
|
40
|
+
object.attribute_names.reject{|key| !can_read?(key) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def column_names
|
44
|
+
object.column_names.reject{|key| !can_read?(key) }
|
45
|
+
end
|
46
|
+
|
39
47
|
def attributes
|
40
48
|
object.attributes.reject{|key,value| !can_read?(key) }
|
41
49
|
end
|
data/test/has_permission_test.rb
CHANGED
@@ -63,6 +63,14 @@ class HasPermissionTest < Test::Unit::TestCase
|
|
63
63
|
assert_equal [:read_access], @model.with_permission(nil).attributes.keys
|
64
64
|
end
|
65
65
|
|
66
|
+
should "only allow readable attributes for attribute_names" do
|
67
|
+
assert_equal [:read_access], @model.with_permission(nil).attribute_names
|
68
|
+
end
|
69
|
+
|
70
|
+
should "only allow readable attributes for column_names" do
|
71
|
+
assert_equal [:read_access], @model.with_permission(nil).column_names
|
72
|
+
end
|
73
|
+
|
66
74
|
should "only allow writeable attribute for update attribute" do
|
67
75
|
assert_raise PermissionException do
|
68
76
|
@model.with_permission(nil).update_attribute(:no_access, "test")
|
data/test/test_helper.rb
CHANGED
@@ -21,6 +21,14 @@ class Model
|
|
21
21
|
value
|
22
22
|
end
|
23
23
|
|
24
|
+
def column_names
|
25
|
+
[:read_access, :no_access]
|
26
|
+
end
|
27
|
+
|
28
|
+
def attribute_names
|
29
|
+
[:read_access, :no_access]
|
30
|
+
end
|
31
|
+
|
24
32
|
def attributes
|
25
33
|
{:read_access => "test", :no_access => "test"}
|
26
34
|
end
|
@@ -56,9 +64,9 @@ module Permission
|
|
56
64
|
end
|
57
65
|
|
58
66
|
def can_write?(attr_name)
|
59
|
-
case attr_name
|
60
|
-
when
|
61
|
-
when
|
67
|
+
case attr_name.to_s
|
68
|
+
when "write_access" : true
|
69
|
+
when "no_access" : false
|
62
70
|
else true
|
63
71
|
end
|
64
72
|
end
|