has_one_autocreate 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/Manifest +9 -0
- data/README.rdoc +45 -0
- data/Rakefile +18 -0
- data/has_one_autocreate.gemspec +30 -0
- data/init.rb +1 -0
- data/lib/has_one_autocreate.rb +48 -0
- data/spec/has_one_autocreate_spec.rb +76 -0
- data/spec/models.rb +7 -0
- data/spec/spec_helper.rb +12 -0
- metadata +71 -0
data/CHANGELOG
ADDED
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
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? Add this to your environment.rb and run the following command.
|
35
|
+
|
36
|
+
config.gem 'has_one_autocreate'
|
37
|
+
|
38
|
+
$ rake gems:install
|
39
|
+
|
40
|
+
== Docs
|
41
|
+
|
42
|
+
http://rdoc.info/projects/jqr/has_one_autocreate
|
43
|
+
|
44
|
+
Homepage:: http://github.com/jqr/has_one_autocreate
|
45
|
+
License:: Copyright (c) 2008 Elijah Miller <mailto:elijah.miller@gmail.com>, released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
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://github.com/jqr/has_one_autocreate"
|
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
|
+
task :test => :spec
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{has_one_autocreate}
|
5
|
+
s.version = "1.0.0"
|
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{2009-12-03}
|
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.homepage = %q{http://github.com/jqr/has_one_autocreate}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Has_one_autocreate", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{has_one_autocreate}
|
18
|
+
s.rubygems_version = %q{1.3.5}
|
19
|
+
s.summary = %q{Automatic creation and building for has_one relationships.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
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,76 @@
|
|
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
|
+
describe 'with an unsaved user' do
|
50
|
+
|
51
|
+
before(:each) do
|
52
|
+
@user = User.new(:name => 'Dr. User')
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'before accessing the profile' do
|
56
|
+
it "has NOT created the profile" do
|
57
|
+
Profile.count.should == 0
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'after accessing the profile' do
|
62
|
+
before(:each) do
|
63
|
+
@profile = @user.profile
|
64
|
+
end
|
65
|
+
|
66
|
+
it "does not create the profile" do
|
67
|
+
Profile.count.should_not == 0
|
68
|
+
end
|
69
|
+
|
70
|
+
it "is a profile" do
|
71
|
+
@profile.should be_kind_of(Profile)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
data/spec/models.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -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,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_one_autocreate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elijah Miller
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-03 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Automatic creation and building for has_one relationships.
|
17
|
+
email: elijah.miller@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- CHANGELOG
|
24
|
+
- lib/has_one_autocreate.rb
|
25
|
+
- README.rdoc
|
26
|
+
files:
|
27
|
+
- CHANGELOG
|
28
|
+
- init.rb
|
29
|
+
- lib/has_one_autocreate.rb
|
30
|
+
- Manifest
|
31
|
+
- Rakefile
|
32
|
+
- README.rdoc
|
33
|
+
- spec/has_one_autocreate_spec.rb
|
34
|
+
- spec/models.rb
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
- has_one_autocreate.gemspec
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/jqr/has_one_autocreate
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --line-numbers
|
44
|
+
- --inline-source
|
45
|
+
- --title
|
46
|
+
- Has_one_autocreate
|
47
|
+
- --main
|
48
|
+
- README.rdoc
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "1.2"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: has_one_autocreate
|
66
|
+
rubygems_version: 1.3.5
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Automatic creation and building for has_one relationships.
|
70
|
+
test_files: []
|
71
|
+
|