let_me_mass_assign_protected_attributes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/Readme.md +97 -0
- data/let_me_mass_assign_protected_attributes.gemspec +18 -0
- data/lib/let_me_mass_assign_protected_attributes.rb +61 -0
- data/lib/let_me_mass_assign_protected_attributes/version.rb +3 -0
- metadata +63 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/Readme.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
Let me mass-assign protected attributes!
|
2
|
+
========================================
|
3
|
+
|
4
|
+
Normally when you try to do a mass-assignment (or a `new`/`create`) and you include in your attributes hash an attribute that is protected (by an `attr_accessible` or `attr_protected` somewhere in your model), Active Record will complain with a warning such as this in your log file:
|
5
|
+
|
6
|
+
WARNING: Can't mass-assign protected attributes: is_admin, state
|
7
|
+
|
8
|
+
But what if you really *want* to mass-assign those protected attributes anyway and you have a legitimate reason for doing so?
|
9
|
+
|
10
|
+
For example, sometimes you need to create a record in your tests with certain pre-determined values for all of its attributes, *including* the protected ones.
|
11
|
+
|
12
|
+
You *could* always do a regular (non-mass) attribute assignment for each of your protected attributes after your main mass-assignment/`new`/`create`...
|
13
|
+
|
14
|
+
user = User.new(
|
15
|
+
name: 'Some Name',
|
16
|
+
email: 'test@example.com',
|
17
|
+
...
|
18
|
+
) do |user|
|
19
|
+
user.is_admin = true
|
20
|
+
user.state = :active
|
21
|
+
end
|
22
|
+
|
23
|
+
But now there's an easier way!
|
24
|
+
|
25
|
+
user = User.unprotected_new(
|
26
|
+
name: 'Some Name',
|
27
|
+
email: 'test@example.com',
|
28
|
+
...
|
29
|
+
is_admin: true,
|
30
|
+
state: :active
|
31
|
+
)
|
32
|
+
|
33
|
+
In addition to `unprotected_new`, this gem provides these instance methods to `ActiveRecord::Base`:
|
34
|
+
|
35
|
+
* `unprotected_attributes=`
|
36
|
+
* `unprotected_update_attributes`/`unprotected_update_attributes!`
|
37
|
+
|
38
|
+
and these class methods:
|
39
|
+
|
40
|
+
* `unprotected_new`
|
41
|
+
* `unprotected_create`/`unprotected_create!`
|
42
|
+
|
43
|
+
Thank you, Active Record, for always protecting me when I want you to. But sometimes I don't. Don't protect me for a second while I sneak in and change some protected attributes...
|
44
|
+
|
45
|
+
More examples:
|
46
|
+
|
47
|
+
user = User.unprotected_create!(
|
48
|
+
name: 'Some Name',
|
49
|
+
email: 'test@example.com',
|
50
|
+
...
|
51
|
+
is_admin: true,
|
52
|
+
state: :active
|
53
|
+
)
|
54
|
+
|
55
|
+
if user.unprotected_update_attributes(name: 'New name', is_admin: false)
|
56
|
+
...
|
57
|
+
end
|
58
|
+
|
59
|
+
It also appears to work with associations:
|
60
|
+
|
61
|
+
user.memberships.unprotected_create!(state: 'active')
|
62
|
+
|
63
|
+
Installation
|
64
|
+
============
|
65
|
+
|
66
|
+
Add to your Gemfile:
|
67
|
+
|
68
|
+
group :test do
|
69
|
+
gem 'let_me_mass_assign_protected_attributes'
|
70
|
+
end
|
71
|
+
|
72
|
+
Further reading
|
73
|
+
===============
|
74
|
+
|
75
|
+
* http://api.rubyonrails.org/classes/ActiveRecord/Base.html (for information about `attr_accessible`/`attr_protected`)
|
76
|
+
* http://railscasts.com/episodes/26-hackers-love-mass-assignment (for why you *should* be using `attr_accessible`/`attr_protected`)
|
77
|
+
|
78
|
+
To do
|
79
|
+
=====
|
80
|
+
|
81
|
+
* Add tests
|
82
|
+
|
83
|
+
Contributing
|
84
|
+
============
|
85
|
+
|
86
|
+
Comments and contributions are welcome.
|
87
|
+
|
88
|
+
Please feel free to fork the project at https://github.com/TylerRick/let_me_mass_assign_protected_attributes and to send pull requests.
|
89
|
+
|
90
|
+
Bugs can be reported at https://github.com/TylerRick/let_me_mass_assign_protected_attributes/issues
|
91
|
+
|
92
|
+
License
|
93
|
+
=======
|
94
|
+
|
95
|
+
Copyright 2011, Tyler Rick
|
96
|
+
|
97
|
+
This is free software, distributed under the terms of the MIT License.
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "let_me_mass_assign_protected_attributes/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "let_me_mass_assign_protected_attributes"
|
7
|
+
s.version = LetMeMassAssignProtectedAttributes::Version
|
8
|
+
s.authors = ["Tyler Rick"]
|
9
|
+
s.email = ["tyler@tylerrick.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Thank you, Active Record, for not letting me mass-assign protected attributes. But sometimes I want to anyway. This gem provides unprotected_update_attributes, unprotected_create, and related methods to ActiveRecord::Base.}
|
12
|
+
s.description = s.summary
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "let_me_mass_assign_protected_attributes/version"
|
2
|
+
|
3
|
+
module LetMeMassAssignProtectedAttributes
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def unprotected_attributes=(attributes)
|
7
|
+
self.tap do
|
8
|
+
attributes.each do |key, value|
|
9
|
+
self.send "#{key}=", value
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# update_attributes doesn't set attributes protected via attr_protected (instead, it logs a warning informing you that you are trying to mass-assign protected attributes), so this method is convenient if you have a hash of protected attributes that you want to set
|
15
|
+
def unprotected_update_attributes(attributes)
|
16
|
+
self.tap do
|
17
|
+
self.unprotected_attributes = attributes
|
18
|
+
save
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Updates an object just like Base.unprotected_update_attributes but calls save! instead of save so an exception is raised if the record is invalid.
|
23
|
+
def unprotected_update_attributes!(attributes)
|
24
|
+
self.tap do
|
25
|
+
self.unprotected_attributes = attributes
|
26
|
+
save!
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
# The same as a normal new, only it lets you initialize/set attr_protected attributes
|
32
|
+
def unprotected_new(attributes = {})
|
33
|
+
if attributes.is_a?(Array)
|
34
|
+
attributes.each {|attributes| unprotected_new(attributes) }
|
35
|
+
else
|
36
|
+
self.unprotected_attributes = attributes
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# The same as a normal create, only it lets you initialize/set attr_protected attributes
|
41
|
+
def unprotected_create(attributes = {})
|
42
|
+
if attributes.is_a?(Array)
|
43
|
+
attributes.each {|attributes| unprotected_create(attributes) }
|
44
|
+
else
|
45
|
+
new.unprotected_update_attributes(attributes)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def unprotected_create!(attributes = {})
|
50
|
+
if attributes.is_a?(Array)
|
51
|
+
attributes.each {|attributes| unprotected_create!(attributes) }
|
52
|
+
else
|
53
|
+
new.unprotected_update_attributes!(attributes)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
ActiveRecord::Base.class_eval do
|
60
|
+
include LetMeMassAssignProtectedAttributes
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: let_me_mass_assign_protected_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tyler Rick
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-09-08 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Thank you, Active Record, for not letting me mass-assign protected attributes. But sometimes I want to anyway. This gem provides unprotected_update_attributes, unprotected_create, and related methods to ActiveRecord::Base.
|
18
|
+
email:
|
19
|
+
- tyler@tylerrick.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- Rakefile
|
30
|
+
- Readme.md
|
31
|
+
- let_me_mass_assign_protected_attributes.gemspec
|
32
|
+
- lib/let_me_mass_assign_protected_attributes.rb
|
33
|
+
- lib/let_me_mass_assign_protected_attributes/version.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: ""
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.5.2
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Thank you, Active Record, for not letting me mass-assign protected attributes. But sometimes I want to anyway. This gem provides unprotected_update_attributes, unprotected_create, and related methods to ActiveRecord::Base.
|
62
|
+
test_files: []
|
63
|
+
|