build_associated 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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 build_associated.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ A very simple gem. This allows you to use the following code in ActiveRecord.
2
+
3
+ ```ruby
4
+ class PlanSku < ActiveRecord::Base
5
+ belongs_to :sku
6
+ belongs_to :plan_cost
7
+ belogns_to :plan_coverage
8
+
9
+ build_associated :sku
10
+ build_associated :plan_cost
11
+ build_associated :plan_coverage
12
+ end
13
+
14
+ plan = PlanSku.new
15
+ plan.sku # Instead of nil, it now returns PlanSku.build_sku
16
+ ```
17
+
18
+ Tested on Rails3.1. Use at own risk.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "build_associated/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "build_associated"
7
+ s.version = BuildAssociated::VERSION
8
+ s.authors = ["Adam Kerr"]
9
+ s.email = ["ajrkerr@gmail.com"]
10
+ s.homepage = "http://github.com/ajrkerr/build_associated"
11
+ s.summary = %q{Have ActiveRecord automagically build an association when accessed. Useful for belongs_to associations which should never be nil.}
12
+ s.description = %q{Have ActiveRecord automagically build an association when accessed. Useful for belongs_to associations which should never be nil.}
13
+
14
+ s.rubyforge_project = "build_associated"
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
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ s.add_runtime_dependency "activerecord"
25
+ end
@@ -0,0 +1,3 @@
1
+ module BuildAssociated
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ require "build_associated/version"
2
+
3
+ module BuildAssociated
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def build_associated association_name
8
+ alias_method "_#{association_name}", association_name
9
+ remove_method association_name
10
+
11
+ define_method association_name do
12
+ self.send("_#{association_name}") || self.send("build_#{association_name}")
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class ActiveRecord::Base
19
+ include BuildAssociated
20
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: build_associated
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Kerr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &21016040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *21016040
25
+ description: Have ActiveRecord automagically build an association when accessed. Useful
26
+ for belongs_to associations which should never be nil.
27
+ email:
28
+ - ajrkerr@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - README.md
36
+ - Rakefile
37
+ - build_associated.gemspec
38
+ - lib/build_associated.rb
39
+ - lib/build_associated/version.rb
40
+ homepage: http://github.com/ajrkerr/build_associated
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project: build_associated
60
+ rubygems_version: 1.8.10
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Have ActiveRecord automagically build an association when accessed. Useful
64
+ for belongs_to associations which should never be nil.
65
+ test_files: []