active_record_auto_build_associations 0.0.2

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_record_auto_build_associations.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/Readme.md ADDED
@@ -0,0 +1,28 @@
1
+ ActiveRecord::AutoBuildAssociations
2
+ ===================================
3
+
4
+ Now you never have to remember to manually call `build_address` or `comments.build` again.
5
+
6
+ Now you never have to worry about `NoMethodErrors` when you call `user.address.country`.
7
+
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
+
10
+ class User < ActiveRecord::Base
11
+ auto_build_association :address
12
+ end
13
+
14
+ 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
+ To do
18
+ =====
19
+
20
+ * 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
+ * Add tests
22
+
23
+ License
24
+ =======
25
+
26
+ Copyright 2011, Tyler Rick
27
+
28
+ This is free software, distributed under the terms of the MIT License.
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "active_record/auto_build_associations/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "active_record_auto_build_associations"
7
+ s.version = ActiveRecord::AutoBuildAssociations::Version
8
+ s.authors = ["Tyler Rick"]
9
+ s.email = ["tyler@tylerrick.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Automatically calls `build_assoc_model` for you, ensuring that model.assoc_model is always an instance of model and not nil.}
12
+ s.description = s.summary
13
+
14
+ s.add_dependency 'activerecord'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'active_record'
3
+ rescue LoadError
4
+ module ActiveRecord
5
+ end
6
+ end
7
+
8
+ module ActiveRecord::AutoBuildAssociations
9
+ Version = "0.0.2"
10
+ end
@@ -0,0 +1,34 @@
1
+ module ActiveRecord
2
+ module AutoBuildAssociations
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ end
7
+
8
+ included do
9
+ # can be turned off from tests, for example
10
+ cattr_accessor :auto_build_associations
11
+ @@auto_build_associations = true
12
+ def self.auto_build_associations?
13
+ #puts "self.auto_build_associations? returning #{@@auto_build_associations.inspect}"
14
+ @@auto_build_associations
15
+ end
16
+
17
+ def self.auto_build_association(assoc_name)
18
+ define_method :"#{assoc_name}_with_auto_build" do
19
+ if self.class.auto_build_associations? == false
20
+ send(:"#{assoc_name}_without_auto_build")
21
+ else
22
+ send(:"#{assoc_name}_without_auto_build") || (
23
+ # TODO: reflect on association; for has_many, it should do assoc_name.build instead
24
+ send(:"build_#{assoc_name}")
25
+ send(:"#{assoc_name}_without_auto_build")
26
+ )
27
+ end
28
+ end
29
+ alias_method_chain :"#{assoc_name}", :auto_build
30
+ end
31
+
32
+ end # included
33
+ end
34
+ end
@@ -0,0 +1,2 @@
1
+ require "active_record"
2
+ require "active_record/auto_build_associations"
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_auto_build_associations
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Tyler Rick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-24 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activerecord
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: Automatically calls `build_assoc_model` for you, ensuring that model.assoc_model is always an instance of model and not nil.
28
+ email:
29
+ - tyler@tylerrick.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Rakefile
40
+ - Readme.md
41
+ - active_record_auto_build_associations.gemspec
42
+ - lib/active_record/auto_build_associations.rb
43
+ - lib/active_record/auto_build_associations/version.rb
44
+ - lib/active_record_auto_build_associations.rb
45
+ has_rdoc: true
46
+ homepage: ""
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.5.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Automatically calls `build_assoc_model` for you, ensuring that model.assoc_model is always an instance of model and not nil.
73
+ test_files: []
74
+