sudo_attributes 0.5.0 → 0.5.1
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 +1 -1
- data/lib/sudo_attributes.rb +61 -3
- data/spec/sudo_attributes_spec.rb +26 -22
- data/sudo_attributes.gemspec +3 -4
- metadata +6 -6
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.1
|
data/lib/sudo_attributes.rb
CHANGED
@@ -1,10 +1,27 @@
|
|
1
1
|
module SudoAttributes
|
2
2
|
|
3
3
|
module Base
|
4
|
+
|
5
|
+
# Protect attributes using ActiveRecord's built in <tt>attr_protected</tt> class macro.
|
6
|
+
# When invoked, it also adds other sudo_attributes class and instance methods to model such as +sudo_create+
|
7
|
+
#
|
8
|
+
# ==== Example
|
9
|
+
# # Define attributes which are protected from mass assignment
|
10
|
+
# class User < ActiveRecord::Base
|
11
|
+
# sudo_attr_protected :admin
|
12
|
+
# end
|
4
13
|
def sudo_attr_protected(*attrs)
|
5
14
|
Private::set_attributes(self, attrs, :protected)
|
6
15
|
end
|
7
16
|
|
17
|
+
# Protect attributes using ActiveRecord's built in <tt>attr_accessible</tt> class macro.
|
18
|
+
# When invoked, it also adds other sudo_attributes class and instance methods to model such as +sudo_create+
|
19
|
+
#
|
20
|
+
# ==== Example
|
21
|
+
# # Define attributes which are not protected from mass assignment
|
22
|
+
# class User < ActiveRecord::Base
|
23
|
+
# sudo_attr_accessible :admin
|
24
|
+
# end
|
8
25
|
def sudo_attr_accessible(*attrs)
|
9
26
|
Private::set_attributes(self, attrs, :accessible)
|
10
27
|
end
|
@@ -12,6 +29,7 @@ module SudoAttributes
|
|
12
29
|
|
13
30
|
module Private
|
14
31
|
|
32
|
+
# Used internally to assign protected attributes and include additional sudo_attributes functionality
|
15
33
|
def self.set_attributes(klass, attrs, type)
|
16
34
|
unless attrs.empty?
|
17
35
|
raise "Invalid argument passed to has_sudo_attributes" unless attrs.all? {|a| a.is_a? Symbol }
|
@@ -23,34 +41,74 @@ module SudoAttributes
|
|
23
41
|
klass.send :include, SudoAttributes::InstanceMethods
|
24
42
|
end
|
25
43
|
end
|
26
|
-
|
27
|
-
# Added to ActiveRecord model only if
|
44
|
+
|
45
|
+
# Added to ActiveRecord model only if sudo_attr_(accessible|protected) is called
|
28
46
|
module ClassMethods
|
47
|
+
# Creates an object with protected attributes and saves it to the database, if validations pass.
|
48
|
+
# The resulting object is returned whether the object was saved successfully to the database or not.
|
49
|
+
#
|
50
|
+
# Unlike ActiveRecord::Base.create, the <tt>attributes</tt> parameter can only be a Hash. This Hash describes the
|
51
|
+
# attributes on the objects that are to be created.
|
52
|
+
#
|
53
|
+
# ==== Example
|
54
|
+
# # Create a single new object where admin is a protected attribute
|
55
|
+
# User.sudo_create(:first_name => 'Pete', :admin => true)
|
29
56
|
def sudo_create(attributes=nil)
|
30
57
|
instance = sudo_new(attributes)
|
31
58
|
instance.save
|
32
59
|
instance
|
33
60
|
end
|
34
61
|
|
62
|
+
# Creates an object just like sudo_create but calls save! instead of save so an exception is raised if the record is invalid
|
63
|
+
#
|
64
|
+
# ==== Example
|
65
|
+
# # Create a single new object where admin is a protected attribute
|
66
|
+
# User.sudo_create!(:first_name => 'Pete', :admin => true)
|
35
67
|
def sudo_create!(attributes=nil)
|
36
68
|
instance = sudo_new(attributes)
|
37
69
|
instance.save!
|
38
70
|
instance
|
39
71
|
end
|
40
72
|
|
73
|
+
# Instantiates an object just like ActiveRecord::Base.new, but allowing mass assignment of protected attributes
|
74
|
+
#
|
75
|
+
# ==== Example
|
76
|
+
# # Instantiate an object where admin is a protected attribute
|
77
|
+
# User.sudo_new(:first_name => 'Pete', :admin => true)
|
41
78
|
def sudo_new(attributes=nil)
|
42
79
|
instance = new(nil)
|
43
80
|
instance.send(:attributes=, attributes, false)
|
44
81
|
instance
|
45
82
|
end
|
83
|
+
|
84
|
+
# Alias of +sudo_new+
|
85
|
+
def sudo_build(attributes=nil)
|
86
|
+
sudo_new(attributes)
|
87
|
+
end
|
46
88
|
end
|
47
|
-
|
89
|
+
|
90
|
+
# Added to ActiveRecord model only if sudo_attr_(accessible|protected) is called
|
48
91
|
module InstanceMethods
|
92
|
+
|
93
|
+
# Updates attributes of a model, including protected ones, from the passed-in hash and saves the
|
94
|
+
# record. If the object is invalid, the saving will fail and false will be returned.
|
95
|
+
#
|
96
|
+
# ==== Example
|
97
|
+
# # Updated protected attributes on an instance of User
|
98
|
+
# @user = User.find(params[:id])
|
99
|
+
# @user.sudo_update_attributes(params[:user])
|
49
100
|
def sudo_update_attributes(new_attributes)
|
50
101
|
self.send(:attributes=, new_attributes, false)
|
51
102
|
save
|
52
103
|
end
|
53
104
|
|
105
|
+
# Updates its receiver just like +sudo_update_attributes+ but calls <tt>save!</tt> instead
|
106
|
+
# of +save+, so an exception is raised if the record is invalid.
|
107
|
+
#
|
108
|
+
# ==== Example
|
109
|
+
# # Updated protected attributes on an instance of User
|
110
|
+
# @user = User.find(params[:id])
|
111
|
+
# @user.sudo_update_attributes!(params[:user])
|
54
112
|
def sudo_update_attributes!(new_attributes)
|
55
113
|
self.send(:attributes=, new_attributes, false)
|
56
114
|
save!
|
@@ -55,28 +55,32 @@ describe "Cat" do
|
|
55
55
|
|
56
56
|
end
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
@cat.
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
58
|
+
# Tests for sudo_new and sudo_build, aliases of each other
|
59
|
+
[:sudo_new, :sudo_build].each do |sudo_method|
|
60
|
+
|
61
|
+
context "SudoAttributes #{sudo_method} initializer" do
|
62
|
+
before(:each) { @cat = Cat.send(sudo_method, @attributes)}
|
63
|
+
|
64
|
+
it "should have a name" do
|
65
|
+
@cat.name.should == @attributes[:name]
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should set the name with sudo_update_attributes" do
|
69
|
+
@cat.sudo_update_attributes(:name => "Portia")
|
70
|
+
@cat.name.should == "Portia"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should have a color" do
|
74
|
+
@cat.color.should == @attributes[:color]
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should not have an id" do
|
78
|
+
@cat.id.should be_nil
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should have an age" do
|
82
|
+
@cat.age.should == @attributes[:age]
|
83
|
+
end
|
80
84
|
end
|
81
85
|
end
|
82
86
|
|
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.5.
|
8
|
+
s.version = "0.5.1"
|
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{2011-01-
|
12
|
+
s.date = %q{2011-01-23}
|
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 = [
|
@@ -35,7 +35,7 @@ Gem::Specification.new do |s|
|
|
35
35
|
s.homepage = %q{http://github.com/beerlington/sudo_attributes}
|
36
36
|
s.rdoc_options = ["--charset=UTF-8"]
|
37
37
|
s.require_paths = ["lib"]
|
38
|
-
s.rubygems_version = %q{1.
|
38
|
+
s.rubygems_version = %q{1.4.2}
|
39
39
|
s.summary = %q{Override ActiveRecord protected attributes with mass assignment}
|
40
40
|
s.test_files = [
|
41
41
|
"spec/spec_helper.rb",
|
@@ -43,7 +43,6 @@ Gem::Specification.new do |s|
|
|
43
43
|
]
|
44
44
|
|
45
45
|
if s.respond_to? :specification_version then
|
46
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
46
|
s.specification_version = 3
|
48
47
|
|
49
48
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
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:
|
5
|
-
prerelease:
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 1
|
10
|
+
version: 0.5.1
|
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: 2011-01-
|
18
|
+
date: 2011-01-23 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
134
|
requirements: []
|
135
135
|
|
136
136
|
rubyforge_project:
|
137
|
-
rubygems_version: 1.
|
137
|
+
rubygems_version: 1.4.2
|
138
138
|
signing_key:
|
139
139
|
specification_version: 3
|
140
140
|
summary: Override ActiveRecord protected attributes with mass assignment
|