jqr-has_one_autocreate 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/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ v0.0.2. Initial gem, cleaned up project and abstracted tests some.
2
+ v0.0.1. Initial release.
data/Manifest ADDED
@@ -0,0 +1,9 @@
1
+ CHANGELOG
2
+ init.rb
3
+ lib/has_one_autocreate.rb
4
+ Manifest
5
+ Rakefile
6
+ README.rdoc
7
+ spec/has_one_autocreate_spec.rb
8
+ spec/models.rb
9
+ spec/spec_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ = Has One Autocreate
2
+
3
+ Automatic creation of a has_one associated object when it is first accessed.
4
+
5
+ == Examples
6
+
7
+ class User < ActiveRecord::Base
8
+ has_one :profile, :autocreate => true
9
+ end
10
+
11
+ class Profile < ActiveRecord::Base
12
+ belongs_to :user
13
+ end
14
+
15
+ # make a single user
16
+ >> user = User.create(:name => 'Dr. User')
17
+ => #<User id: 1, name: "Dr. User">
18
+
19
+ # look for any profile objects
20
+ >> Profile.count
21
+ => 0
22
+
23
+ # access the user's profile object to see it automatically created
24
+ >> user.profile
25
+ => #<Profile id: 1, title: nil, user_id: 1>
26
+
27
+
28
+ == Install
29
+
30
+ As a Rails plugin.
31
+
32
+ ./script/plugin install git://github.com/jqr/has_one_autocreate.git
33
+
34
+ Prefer gems?
35
+
36
+ gem sources -a http://gems.github.com
37
+ gem install jqr-has_one_autocreate
38
+
39
+
40
+ Homepage:: http://github.com/jqr/has_one_autocreate/tree/master
41
+ License:: Copyright (c) 2008 Elijah Miller <mailto:elijah.miller@gmail.com>, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ require 'echoe'
4
+ Echoe.new 'has_one_autocreate' do |p|
5
+ p.description = "Automatic creation and building for has_one relationships."
6
+ # p.url = "http://has_one_autocreate.rubyforge.org"
7
+ p.author = "Elijah Miller"
8
+ p.email = "elijah.miller@gmail.com"
9
+ p.retain_gemspec = true
10
+ p.need_tar_gz = false
11
+ p.extra_deps = [
12
+ ]
13
+ p.ignore_pattern = ['spec/test.sqlite3']
14
+ end
15
+
16
+ desc 'Default: run specs'
17
+ task :default => :spec
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_files = FileList["spec/**/*_spec.rb"]
20
+ end
21
+
22
+ task :test => :spec
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{has_one_autocreate}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Elijah Miller"]
9
+ s.date = %q{2008-12-07}
10
+ s.description = %q{Automatic creation and building for has_one relationships.}
11
+ s.email = %q{elijah.miller@gmail.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/has_one_autocreate.rb", "README.rdoc"]
13
+ s.files = ["CHANGELOG", "init.rb", "lib/has_one_autocreate.rb", "Manifest", "Rakefile", "README.rdoc", "spec/has_one_autocreate_spec.rb", "spec/models.rb", "spec/spec_helper.rb", "has_one_autocreate.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Has_one_autocreate", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{has_one_autocreate}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Automatic creation and building for has_one relationships.}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<echoe>, [">= 0"])
28
+ else
29
+ s.add_dependency(%q<echoe>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<echoe>, [">= 0"])
33
+ end
34
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'has_one_autocreate'
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+
4
+ # Functional prototype of automatic create/build on a has_one relationship.
5
+ # TODO: needs to be loaded before cache_fu extensions.
6
+ module HasOneAutocreate
7
+ # TODO: alias_method_chainize these
8
+ module ClassMethods
9
+ def has_one(*args)
10
+ options = args.extract_options!
11
+ autocreate = options.delete(:autocreate)
12
+ args << options
13
+ super(*args)
14
+
15
+ if autocreate
16
+ reflections[args.first].send "autocreate=", autocreate
17
+ end
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ # TODO: Move to find_target once we're in front of cache_fu.
23
+ def reload
24
+ value = find_target
25
+ if value
26
+ @target = value
27
+ elsif method = @reflection.send(:autocreate)
28
+ @target =
29
+ case method
30
+ when true
31
+ create!
32
+ when Symbol, String
33
+ self.send(method)
34
+ when false
35
+ # nop
36
+ else
37
+ # default
38
+ # ActiveRecord::Associations::HasOneAssociation.autocreate
39
+ end
40
+ end
41
+ loaded
42
+ end
43
+ end
44
+ end
45
+
46
+ ActiveRecord::Base.extend HasOneAutocreate::ClassMethods
47
+ ActiveRecord::Associations::HasOneAssociation.send :include, HasOneAutocreate::InstanceMethods
48
+ ActiveRecord::Reflection::AssociationReflection.send :attr_accessor, :autocreate
@@ -0,0 +1,49 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+
4
+ describe HasOneAutocreate do
5
+ before(:all) do
6
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
7
+ :database => TEST_DATABASE_FILE)
8
+
9
+ User.connection.create_table :users do |t|
10
+ t.string :name
11
+ end
12
+
13
+ Profile.connection.create_table :profiles do |t|
14
+ t.integer :user_id
15
+ end
16
+ end
17
+
18
+ after(:all) do
19
+ User.connection.drop_table(:users)
20
+ Profile.connection.drop_table(:profiles)
21
+
22
+ FileUtils.rm(TEST_DATABASE_FILE) if File.exist?(TEST_DATABASE_FILE)
23
+ end
24
+
25
+ before(:each) do
26
+ @user = User.create(:name => 'Dr. User')
27
+ end
28
+
29
+ describe 'before accessing the profile' do
30
+ it "has NOT created the profile" do
31
+ Profile.count.should == 0
32
+ end
33
+ end
34
+
35
+ describe 'after accessing the profile' do
36
+ before(:each) do
37
+ @profile = @user.profile
38
+ end
39
+
40
+ it "creates the profile" do
41
+ Profile.count.should_not == 0
42
+ end
43
+
44
+ it "is a profile" do
45
+ @profile.should be_kind_of(Profile)
46
+ end
47
+ end
48
+
49
+ end
data/spec/models.rb ADDED
@@ -0,0 +1,7 @@
1
+ class User < ActiveRecord::Base
2
+ has_one :profile, :autocreate => true
3
+ end
4
+
5
+ class Profile < ActiveRecord::Base
6
+ belongs_to :user
7
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec'
2
+ require 'fileutils'
3
+
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'has_one_autocreate')
5
+
6
+ require File.join(File.dirname(__FILE__), 'models')
7
+
8
+ TEST_DATABASE_FILE = File.join(File.dirname(__FILE__), 'test.sqlite3')
9
+
10
+ Spec::Runner.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jqr-has_one_autocreate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Elijah Miller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-07 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Automatic creation and building for has_one relationships.
25
+ email: elijah.miller@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - CHANGELOG
32
+ - lib/has_one_autocreate.rb
33
+ - README.rdoc
34
+ files:
35
+ - CHANGELOG
36
+ - init.rb
37
+ - lib/has_one_autocreate.rb
38
+ - Manifest
39
+ - Rakefile
40
+ - README.rdoc
41
+ - spec/has_one_autocreate_spec.rb
42
+ - spec/models.rb
43
+ - spec/spec_helper.rb
44
+ - has_one_autocreate.gemspec
45
+ has_rdoc: true
46
+ homepage: ""
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --line-numbers
50
+ - --inline-source
51
+ - --title
52
+ - Has_one_autocreate
53
+ - --main
54
+ - README.rdoc
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "1.2"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: has_one_autocreate
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Automatic creation and building for has_one relationships.
76
+ test_files: []
77
+