sudo_attributes 0.3.0 → 0.4.0
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/.gitignore +1 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +24 -0
- data/README.textile +9 -9
- data/Rakefile +0 -2
- data/VERSION +1 -1
- data/lib/sudo_attributes.rb +20 -44
- data/spec/spec_helper.rb +8 -16
- data/spec/sudo_attributes_spec.rb +1 -1
- data/sudo_attributes.gemspec +4 -2
- metadata +6 -4
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
gemcutter (0.6.1)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.4.0)
|
7
|
+
gemcutter (>= 0.1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rubyforge (>= 2.0.0)
|
10
|
+
json_pure (1.4.6)
|
11
|
+
rake (0.8.7)
|
12
|
+
rspec (1.3.1)
|
13
|
+
rubyforge (2.0.4)
|
14
|
+
json_pure (>= 1.1.7)
|
15
|
+
sqlite3-ruby (1.3.1)
|
16
|
+
|
17
|
+
PLATFORMS
|
18
|
+
ruby
|
19
|
+
|
20
|
+
DEPENDENCIES
|
21
|
+
jeweler (= 1.4.0)
|
22
|
+
rake
|
23
|
+
rspec (~> 1.3.1)
|
24
|
+
sqlite3-ruby
|
data/README.textile
CHANGED
@@ -22,28 +22,28 @@ It's as easy as adding one method call to your models like so:
|
|
22
22
|
|
23
23
|
<pre>
|
24
24
|
class User < ActiveRecord::Base
|
25
|
-
|
25
|
+
sudo_attr_protected :admin
|
26
26
|
end
|
27
27
|
</pre>
|
28
28
|
|
29
29
|
h2. Class Methods
|
30
30
|
|
31
|
-
The class
|
31
|
+
The class methods *sudo_attr_protected* and *sudo_attr_accessible* will be available to all ActiveRecord models. When called, it adds numerous 'sudo' methods to the class. You may still use the default methods @attr_protected@ or @attr_accessible@ provided by rails, but you must still call @has_sudo_attributes@ in order to gain access to the sudo methods.
|
32
32
|
|
33
33
|
Here are four different ways it can be used:
|
34
34
|
|
35
|
-
@
|
35
|
+
@sudo_attr_protected :attribute1, :attribute2@ - Defines protected attributes
|
36
36
|
|
37
|
-
@
|
37
|
+
@sudo_attr_accessible :attribute1, :attribute2@ - Defines accessible attributes
|
38
38
|
|
39
|
-
@
|
39
|
+
@sudo_attr_protected@ or @sudo_attr_accessible@ - With no arguments, it will rely on calls to @attr_protected@ or @attr_accessible@
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
Any model that calls @has_sudo_attributes@ will also be able to create new instances that override protected attributes using the following methods:
|
41
|
+
Any model that calls @sudo_attr_*@ will also be able to create new instances that override protected attributes using the following methods:
|
44
42
|
|
45
43
|
@Model.sudo_create@ - Uses same syntax as @Model.create@ to instantiate and save an object with protected attributes
|
46
44
|
|
45
|
+
@Model.sudo_create!@ - Similar to @Model.sudo_create@, but it raises an ActiveRecord::RecordInvalid exception if there are invalid attributes
|
46
|
+
|
47
47
|
@Model.sudo_new@ - Uses same syntax as @Model.new@ to instantiate, but not save an object with protected attributes
|
48
48
|
|
49
49
|
h2. Instance Methods
|
@@ -58,7 +58,7 @@ h2. Examples
|
|
58
58
|
|
59
59
|
<pre>
|
60
60
|
class User < ActiveRecord::Base
|
61
|
-
|
61
|
+
sudo_attr_protected :admin
|
62
62
|
end
|
63
63
|
</pre>
|
64
64
|
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/sudo_attributes.rb
CHANGED
@@ -1,48 +1,27 @@
|
|
1
1
|
module SudoAttributes
|
2
|
-
|
3
|
-
module Base
|
4
|
-
# This is the method that will be called from each model you want to enable SudoAttributes in
|
5
|
-
def has_sudo_attributes(*attrs)
|
6
|
-
raise "Invalid argument passed to has_sudo_attributes" unless valid_attributes? attrs
|
7
2
|
|
8
|
-
|
9
|
-
|
10
|
-
self
|
11
|
-
self.send :include, SudoAttributes::InstanceMethods
|
12
|
-
end
|
13
|
-
|
14
|
-
private
|
15
|
-
|
16
|
-
def valid_attributes?(attrs)
|
17
|
-
attrs.empty? || hash_syntax?(attrs) || all_symbols?(attrs)
|
18
|
-
end
|
19
|
-
|
20
|
-
# True if argument is in the form ":protected => :field1" or ":accessible => :field2"
|
21
|
-
def hash_syntax?(attrs)
|
22
|
-
return false unless attrs.size == 1
|
23
|
-
|
24
|
-
hash = attrs.first
|
25
|
-
|
26
|
-
hash.is_a?(Hash) && (hash.has_key?(:protected) || hash.has_key?(:accessible))
|
3
|
+
module Base
|
4
|
+
def sudo_attr_protected(*attrs)
|
5
|
+
Private::set_attributes(self, attrs, :protected)
|
27
6
|
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
attrs.all? {|e| e.class == Symbol}
|
7
|
+
|
8
|
+
def sudo_attr_accessible(*attrs)
|
9
|
+
Private::set_attributes(self, attrs, :accessible)
|
32
10
|
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Private
|
33
14
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
self.attr_protected *attrs
|
38
|
-
else
|
39
|
-
key = attrs[0].has_key?(:protected) ? :protected : :accessible
|
15
|
+
def self.set_attributes(klass, attrs, type)
|
16
|
+
unless attrs.empty?
|
17
|
+
raise "Invalid argument passed to has_sudo_attributes" unless attrs.all? {|a| a.is_a? Symbol }
|
40
18
|
|
41
|
-
#
|
42
|
-
self.send("attr_#{key}", *attrs[0][key])
|
19
|
+
klass.send("attr_#{type}", *attrs)
|
43
20
|
end
|
21
|
+
|
22
|
+
klass.extend SudoAttributes::ClassMethods
|
23
|
+
klass.send :include, SudoAttributes::InstanceMethods
|
44
24
|
end
|
45
|
-
|
46
25
|
end
|
47
26
|
|
48
27
|
# Added to ActiveRecord model only if has_sudo_attributes is called
|
@@ -60,16 +39,13 @@ module SudoAttributes
|
|
60
39
|
end
|
61
40
|
|
62
41
|
def sudo_new(attributes=nil)
|
63
|
-
new(
|
42
|
+
instance = new(nil)
|
43
|
+
instance.send(:attributes=, attributes, false)
|
44
|
+
instance
|
64
45
|
end
|
65
46
|
end
|
66
47
|
|
67
48
|
module InstanceMethods
|
68
|
-
def initialize(attributes=nil, attributes_protected=true)
|
69
|
-
super(nil)
|
70
|
-
send(:attributes=, attributes, attributes_protected)
|
71
|
-
end
|
72
|
-
|
73
49
|
def sudo_update_attributes(new_attributes)
|
74
50
|
self.send(:attributes=, new_attributes, false)
|
75
51
|
save
|
@@ -77,4 +53,4 @@ module SudoAttributes
|
|
77
53
|
end
|
78
54
|
end
|
79
55
|
|
80
|
-
ActiveRecord::Base.
|
56
|
+
ActiveRecord::Base.extend SudoAttributes::Base
|
data/spec/spec_helper.rb
CHANGED
@@ -18,13 +18,12 @@ end
|
|
18
18
|
|
19
19
|
module SudoAttributesTest
|
20
20
|
ARGUMENTS = [
|
21
|
-
|
22
|
-
|
23
|
-
:name
|
24
|
-
nil # No arguments passed in
|
21
|
+
"sudo_attr_protected :name",
|
22
|
+
"sudo_attr_accessible :color, :age",
|
23
|
+
"attr_protected :name; sudo_attr_protected"
|
25
24
|
]
|
26
25
|
|
27
|
-
def self.build_cat_class(
|
26
|
+
def self.build_cat_class(argument)
|
28
27
|
|
29
28
|
# Remove the Cat class if it's already been defined in previous run
|
30
29
|
Object.class_eval { remove_const "Cat" if const_defined? "Cat" }
|
@@ -33,16 +32,9 @@ module SudoAttributesTest
|
|
33
32
|
klass = Class.new(ActiveRecord::Base)
|
34
33
|
Object.const_set("Cat", klass)
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
41
|
-
else
|
42
|
-
Cat.class_eval do
|
43
|
-
validates_presence_of :color
|
44
|
-
has_sudo_attributes arguments
|
45
|
-
end
|
46
|
-
end
|
35
|
+
Cat.class_eval %{
|
36
|
+
validates_presence_of :color
|
37
|
+
#{argument}
|
38
|
+
}
|
47
39
|
end
|
48
40
|
end
|
data/sudo_attributes.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sudo_attributes}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Peter Brown"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-11-19}
|
13
13
|
s.description = %q{Adds 'sudo' methods to update protected ActiveRecord attributes with mass assignment}
|
14
14
|
s.email = %q{github@lette.us}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
22
24
|
"LICENSE",
|
23
25
|
"README.textile",
|
24
26
|
"Rakefile",
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sudo_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 4
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Peter Brown
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-11-19 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -76,6 +76,8 @@ extra_rdoc_files:
|
|
76
76
|
files:
|
77
77
|
- .document
|
78
78
|
- .gitignore
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
79
81
|
- LICENSE
|
80
82
|
- README.textile
|
81
83
|
- Rakefile
|