active_record_auto_build_associations 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md
CHANGED
@@ -8,11 +8,22 @@ Now you never have to worry about `NoMethodErrors` when you call `user.address.c
|
|
8
8
|
If you have an associated that should always be there when you need it, whether as a record that is already saved or (if it hasn't been saved yet), a newly built object, you can just add this to your model:
|
9
9
|
|
10
10
|
class User < ActiveRecord::Base
|
11
|
+
include ActiveRecord::AutoBuildAssociations
|
11
12
|
auto_build_association :address
|
12
13
|
end
|
13
14
|
|
14
15
|
This makes it so that any time you do `user.address`, if an existing associated record can't be found, it will automatically call `build_address` for you, ensuring that user.address will always be an instance of `Address` (even if a blank one), instead of sometimes being `nil`.
|
15
16
|
|
17
|
+
This can be very convenient for building forms for records with associations (using fields_for).
|
18
|
+
|
19
|
+
Note: You can't just check `user.address.nil?` to check if the user has supplied an address, because
|
20
|
+
`user.address` will *never* be nill when using `auto_build_association`. Instead you can check
|
21
|
+
`user.address.persisted?`.
|
22
|
+
|
23
|
+
Compatibility
|
24
|
+
=============
|
25
|
+
|
26
|
+
Tested with Rails 3.2.
|
16
27
|
|
17
28
|
To do
|
18
29
|
=====
|
@@ -20,9 +31,14 @@ To do
|
|
20
31
|
* Currently it only works for has_one -- make it work for has_many and belongs_to (belongs_to kind of works but not well)
|
21
32
|
* Add tests
|
22
33
|
|
34
|
+
*Warning*: `belongs_to` doesn't work well because the parent record will try to auto-save the associated record
|
35
|
+
even if it's blank. In other words, simply checking if the associated record is presented/persisted has unwanted
|
36
|
+
**side effects**. So if you call `user.address.persisted?`, that causes the associated address to be
|
37
|
+
built, which may cause unexpected behavior/failure when you try to change the user.
|
38
|
+
|
23
39
|
License
|
24
40
|
=======
|
25
41
|
|
26
|
-
Copyright 2011, Tyler Rick
|
42
|
+
Copyright 2011-2012, Tyler Rick
|
27
43
|
|
28
44
|
This is free software, distributed under the terms of the MIT License.
|
@@ -4,7 +4,7 @@ require "active_record/auto_build_associations/version"
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "active_record_auto_build_associations"
|
7
|
-
s.version = ActiveRecord::AutoBuildAssociations
|
7
|
+
s.version = ActiveRecord::AutoBuildAssociations.version
|
8
8
|
s.authors = ["Tyler Rick"]
|
9
9
|
s.email = ["tyler@tylerrick.com"]
|
10
10
|
s.homepage = ""
|
@@ -15,18 +15,17 @@ module ActiveRecord
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def self.auto_build_association(assoc_name)
|
18
|
-
define_method :"#{assoc_name}
|
18
|
+
define_method :"#{assoc_name}" do
|
19
19
|
if self.class.auto_build_associations? == false
|
20
|
-
|
20
|
+
super()
|
21
21
|
else
|
22
|
-
|
22
|
+
super() || (
|
23
23
|
# TODO: reflect on association; for has_many, it should do assoc_name.build instead
|
24
24
|
send(:"build_#{assoc_name}")
|
25
|
-
|
25
|
+
super()
|
26
26
|
)
|
27
27
|
end
|
28
28
|
end
|
29
|
-
alias_method_chain :"#{assoc_name}", :auto_build
|
30
29
|
end
|
31
30
|
|
32
31
|
end # included
|
metadata
CHANGED
@@ -1,39 +1,35 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_auto_build_associations
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.2
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Tyler Rick
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-11-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: activerecord
|
18
|
-
|
19
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &24482240 !ruby/object:Gem::Requirement
|
20
17
|
none: false
|
21
|
-
requirements:
|
22
|
-
- -
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
25
22
|
type: :runtime
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *24482240
|
25
|
+
description: Automatically calls `build_assoc_model` for you, ensuring that model.assoc_model
|
26
|
+
is always an instance of model and not nil.
|
27
|
+
email:
|
29
28
|
- tyler@tylerrick.com
|
30
29
|
executables: []
|
31
|
-
|
32
30
|
extensions: []
|
33
|
-
|
34
31
|
extra_rdoc_files: []
|
35
|
-
|
36
|
-
files:
|
32
|
+
files:
|
37
33
|
- .gitignore
|
38
34
|
- Gemfile
|
39
35
|
- Rakefile
|
@@ -42,33 +38,30 @@ files:
|
|
42
38
|
- lib/active_record/auto_build_associations.rb
|
43
39
|
- lib/active_record/auto_build_associations/version.rb
|
44
40
|
- lib/active_record_auto_build_associations.rb
|
45
|
-
|
46
|
-
homepage: ""
|
41
|
+
homepage: ''
|
47
42
|
licenses: []
|
48
|
-
|
49
43
|
post_install_message:
|
50
44
|
rdoc_options: []
|
51
|
-
|
52
|
-
require_paths:
|
45
|
+
require_paths:
|
53
46
|
- lib
|
54
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
48
|
none: false
|
56
|
-
requirements:
|
57
|
-
- -
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
version:
|
60
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
54
|
none: false
|
62
|
-
requirements:
|
63
|
-
- -
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
version:
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
66
59
|
requirements: []
|
67
|
-
|
68
60
|
rubyforge_project:
|
69
|
-
rubygems_version: 1.
|
61
|
+
rubygems_version: 1.8.15
|
70
62
|
signing_key:
|
71
63
|
specification_version: 3
|
72
|
-
summary: Automatically calls `build_assoc_model` for you, ensuring that model.assoc_model
|
64
|
+
summary: Automatically calls `build_assoc_model` for you, ensuring that model.assoc_model
|
65
|
+
is always an instance of model and not nil.
|
73
66
|
test_files: []
|
74
|
-
|
67
|
+
has_rdoc:
|