save-changes-to 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.yardopts +1 -0
- data/LICENSE +10 -0
- data/README.markdown +46 -0
- data/lib/cody_robbins/save_changes_to/instance_methods.rb +34 -0
- data/lib/cody_robbins/save_changes_to/railtie.rb +11 -0
- data/lib/save-changes-to.rb +2 -0
- data/save-changes-to.gemspec +19 -0
- metadata +76 -0
data/.gitignore
ADDED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--charset UTF-8 --markup markdown lib/**/*.rb - LICENSE
|
data/LICENSE
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
The MIT License
|
2
|
+
===============
|
3
|
+
|
4
|
+
© 2011 Cody Robbins
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
7
|
+
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
9
|
+
|
10
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
Save Changes To for Rails
|
2
|
+
=========================
|
3
|
+
|
4
|
+
This Rails plugin makes a `save_changes_to` method available in `ApplicationController` which takes the name of an ActiveRecord model and updates the attributes of an instance of that model an appropriately named instance variable in the controller with the attributes in `params` corresponding to the model name.
|
5
|
+
|
6
|
+
Full documentation is at [RubyDoc.info](http://rubydoc.info/gems/save-changes-to).
|
7
|
+
|
8
|
+
Example
|
9
|
+
-------
|
10
|
+
|
11
|
+
The following
|
12
|
+
|
13
|
+
class UserController < ApplicationController
|
14
|
+
def update
|
15
|
+
save_changes_to(:user)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
is the equivalent of
|
20
|
+
|
21
|
+
class UserController < ApplicationController
|
22
|
+
def update
|
23
|
+
@user.update_attributes(params[:user])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
Colophon
|
28
|
+
--------
|
29
|
+
|
30
|
+
### Tested with
|
31
|
+
|
32
|
+
* Rails 3.0.5 — 20 May 2011
|
33
|
+
|
34
|
+
### Contributing
|
35
|
+
|
36
|
+
* [Source](https://github.com/codyrobbins/save-changes-to)
|
37
|
+
* [Bug reports](https://github.com/codyrobbins/save-changes-to/issues)
|
38
|
+
|
39
|
+
To send patches, please fork on GitHub and submit a pull request.
|
40
|
+
|
41
|
+
### Credits
|
42
|
+
|
43
|
+
© 2011 [Cody Robbins](http://codyrobbins.com/). See LICENSE for details.
|
44
|
+
|
45
|
+
* [Homepage](http://codyrobbins.com/software/save-changes-to)
|
46
|
+
* [Follow me on Twitter](http://twitter.com/codyrobbins)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module CodyRobbins
|
2
|
+
module SaveChangesTo
|
3
|
+
module InstanceMethods
|
4
|
+
# Updates the attributes of an instance of a model in an appropriately named instance variable in the controller with the attributes in `params` corresponding to the model name.
|
5
|
+
#
|
6
|
+
# * You pass the name of the model to update to the method.
|
7
|
+
# * The key in `params` assumed to contain the attributes of the model is extrapolated from the model name. For example, if the model is `User` then this key would be `:user`.
|
8
|
+
# * The instance variable that is updated is assumed to be named according to the model. For example, if the model is `User` then the instance variable will be `@user`.
|
9
|
+
#
|
10
|
+
# @param name [Symbol, String] The name of the model to update.
|
11
|
+
# @return [boolean] Whether the update was successful.
|
12
|
+
#
|
13
|
+
# @example The following two actions are equivalent.
|
14
|
+
#
|
15
|
+
# class UserController < ApplicationController
|
16
|
+
# # The usual Rails way.
|
17
|
+
# def update
|
18
|
+
# @user.update_attributes(params[:user])
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# # Using save-changes-to.
|
22
|
+
# def update
|
23
|
+
# save_changes_to(:user)
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
def save_changes_to(name)
|
27
|
+
model = instance_variable(name)
|
28
|
+
attributes = params[name]
|
29
|
+
|
30
|
+
model.update_attributes(attributes)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'save-changes-to'
|
3
|
+
s.version = '1.0'
|
4
|
+
s.summary = 'Save changes to Rails models from params in one call.'
|
5
|
+
s.homepage = 'http://codyrobbins.com/software/save-changes-to'
|
6
|
+
s.author = 'Cody Robbins'
|
7
|
+
s.email = 'cody@codyrobbins.com'
|
8
|
+
|
9
|
+
s.post_install_message = '
|
10
|
+
-------------------------------------------------------------
|
11
|
+
Follow me on Twitter: http://twitter.com/codyrobbins
|
12
|
+
-------------------------------------------------------------
|
13
|
+
|
14
|
+
'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split
|
17
|
+
|
18
|
+
s.add_dependency('easier-instance-variable-access')
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: save-changes-to
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "1.0"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cody Robbins
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-24 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: easier-instance-variable-access
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
description:
|
28
|
+
email: cody@codyrobbins.com
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files: []
|
34
|
+
|
35
|
+
files:
|
36
|
+
- .gitignore
|
37
|
+
- .yardopts
|
38
|
+
- LICENSE
|
39
|
+
- README.markdown
|
40
|
+
- lib/cody_robbins/save_changes_to/instance_methods.rb
|
41
|
+
- lib/cody_robbins/save_changes_to/railtie.rb
|
42
|
+
- lib/save-changes-to.rb
|
43
|
+
- save-changes-to.gemspec
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://codyrobbins.com/software/save-changes-to
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message: "\n\
|
49
|
+
-------------------------------------------------------------\n\
|
50
|
+
Follow me on Twitter: http://twitter.com/codyrobbins\n\
|
51
|
+
-------------------------------------------------------------\n\n"
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.6.2
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Save changes to Rails models from params in one call.
|
75
|
+
test_files: []
|
76
|
+
|