create-new 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 +51 -0
- data/create-new.gemspec +20 -0
- data/lib/cody_robbins/create_new/instance_methods.rb +35 -0
- data/lib/cody_robbins/create_new/railtie.rb +11 -0
- data/lib/create-new.rb +2 -0
- metadata +87 -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,51 @@
|
|
1
|
+
Create New for Rails
|
2
|
+
=======================
|
3
|
+
|
4
|
+
This Rails plugin makes a `create_new` method available in `ApplicationController` which
|
5
|
+
|
6
|
+
* takes the name of an ActiveRecord model and
|
7
|
+
* sets an appropriately named instance variable in the controller
|
8
|
+
* with a new instance of the model
|
9
|
+
* initialized with the attributes in `params` corresponding to the model name.
|
10
|
+
|
11
|
+
Full documentation is at [RubyDoc.info](http://rubydoc.info/gems/create-new).
|
12
|
+
|
13
|
+
Example
|
14
|
+
-------
|
15
|
+
|
16
|
+
The following
|
17
|
+
|
18
|
+
class UserController < ApplicationController
|
19
|
+
def create
|
20
|
+
create_new(:user)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
is the equivalent of
|
25
|
+
|
26
|
+
class UserController < ApplicationController
|
27
|
+
def create
|
28
|
+
@user = User.new(params[:user])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Colophon
|
33
|
+
--------
|
34
|
+
|
35
|
+
### Tested with
|
36
|
+
|
37
|
+
* Rails 3.0.5 — 20 May 2011
|
38
|
+
|
39
|
+
### Contributing
|
40
|
+
|
41
|
+
* [Source](https://github.com/codyrobbins/create-new)
|
42
|
+
* [Bug reports](https://github.com/codyrobbins/create-new/issues)
|
43
|
+
|
44
|
+
To send patches, please fork on GitHub and submit a pull request.
|
45
|
+
|
46
|
+
### Credits
|
47
|
+
|
48
|
+
© 2011 [Cody Robbins](http://codyrobbins.com/). See LICENSE for details.
|
49
|
+
|
50
|
+
* [Homepage](http://codyrobbins.com/software/create-new)
|
51
|
+
* [Follow me on Twitter](http://twitter.com/codyrobbins)
|
data/create-new.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'create-new'
|
3
|
+
s.version = '1.0'
|
4
|
+
s.summary = 'Create a new instance of a Rails model in controller actions in one call.'
|
5
|
+
s.homepage = 'http://codyrobbins.com/software/create-new'
|
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
|
+
s.add_dependency('to-class')
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module CodyRobbins
|
2
|
+
module CreateNew
|
3
|
+
module InstanceMethods
|
4
|
+
# Creates a new instance of an ActiveRecord model and assigns it to an instance variable.
|
5
|
+
#
|
6
|
+
# * You pass the name of the model to create 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 the new instance is assigned to will 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 create.
|
11
|
+
# @return Returns the new instance of the model.
|
12
|
+
#
|
13
|
+
# @example The following two actions are equivalent.
|
14
|
+
#
|
15
|
+
# class UserController < ApplicationController
|
16
|
+
# # The usual Rails way.
|
17
|
+
# def create
|
18
|
+
# @user = User.new(params[:user])
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# # Using create-new.
|
22
|
+
# def create
|
23
|
+
# create_new(:user)
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
def create_new(name)
|
27
|
+
model = name.to_class
|
28
|
+
attributes = params[name]
|
29
|
+
object = model.new(attributes)
|
30
|
+
|
31
|
+
set_instance_variable(name, object)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/create-new.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: create-new
|
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
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: to-class
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
description:
|
39
|
+
email: cody@codyrobbins.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- .yardopts
|
49
|
+
- LICENSE
|
50
|
+
- README.markdown
|
51
|
+
- create-new.gemspec
|
52
|
+
- lib/cody_robbins/create_new/instance_methods.rb
|
53
|
+
- lib/cody_robbins/create_new/railtie.rb
|
54
|
+
- lib/create-new.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://codyrobbins.com/software/create-new
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message: "\n\
|
60
|
+
-------------------------------------------------------------\n\
|
61
|
+
Follow me on Twitter: http://twitter.com/codyrobbins\n\
|
62
|
+
-------------------------------------------------------------\n\n"
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.6.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Create a new instance of a Rails model in controller actions in one call.
|
86
|
+
test_files: []
|
87
|
+
|