get_or_build 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.md +4 -4
- data/Rakefile +14 -0
- data/get_or_build.gemspec +1 -0
- data/lib/get_or_build/association_builder.rb +25 -0
- data/lib/get_or_build/fields_for_builder.rb +19 -0
- data/lib/get_or_build/version.rb +1 -1
- data/lib/get_or_build.rb +4 -19
- data/test/active_record_test.rb +4 -2
- data/test/fields_for_helper_test.rb +29 -0
- data/test/support/connections.rb +19 -0
- data/test/support/models.rb +10 -0
- data/test/test_helper.rb +3 -19
- metadata +29 -16
- data/test/models/company.rb +0 -4
- data/test/models/location.rb +0 -1
- data/test/models/user.rb +0 -3
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# GetOrBuild
|
2
2
|
|
3
|
-
This is a association builder which helps to generate or get associated object with `
|
3
|
+
This is a association builder which helps to generate or get associated object with `belongs_to` or `has_one` association.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your Gemfile
|
8
8
|
`gem 'get_or_build'`
|
9
|
-
then as usually update installed gem by typing `bundle` in
|
9
|
+
then as usually update installed gem by typing `bundle` in your command-line interface.
|
10
10
|
|
11
11
|
## Using with ActiveRecord
|
12
12
|
|
@@ -37,7 +37,7 @@ No longer need to call `f.object.user || f.object.build_user`
|
|
37
37
|
Just include in your document module `GetOrBuild::AssociationBuilder` and it will attach magick methods automatically
|
38
38
|
|
39
39
|
## Contributing
|
40
|
-
You are welcome! Please, run test before pull request: `
|
40
|
+
You are welcome! Please, run test before pull request: `rake` and make sure if everything is workig correctly.
|
41
41
|
|
42
42
|
## TODO
|
43
|
-
* tests for `MongoMapper`
|
43
|
+
* tests for `MongoMapper`
|
data/Rakefile
CHANGED
@@ -1 +1,15 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
1
3
|
require "bundler/gem_tasks"
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the get_or_build gem.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
data/get_or_build.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
# specify any dependencies here; for example:
|
22
22
|
s.add_development_dependency "activerecord"
|
23
|
+
s.add_development_dependency "actionpack"
|
23
24
|
s.add_development_dependency "sqlite3"
|
24
25
|
s.add_development_dependency "rake"
|
25
26
|
# s.add_runtime_dependency "rest-client"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module GetOrBuild
|
2
|
+
module AssociationBuilder
|
3
|
+
# This is ghost method which generates methods like: "association_name_or_build", for example:
|
4
|
+
# class User < ActiveRecord::Base
|
5
|
+
# belongs_to :company
|
6
|
+
# end
|
7
|
+
#
|
8
|
+
# u = User.new
|
9
|
+
# u.company # => nil
|
10
|
+
# u.company_or_build # => #<Company:0x007fe7218be420>
|
11
|
+
# Here the method `company_or_build` for `User` instane `u` was generated
|
12
|
+
def method_missing(method, *args)
|
13
|
+
if method =~ /(\w+)_or_build$/
|
14
|
+
if result = send($1, *args)
|
15
|
+
result
|
16
|
+
else
|
17
|
+
builder_method = "build_#{$1}"
|
18
|
+
send(builder_method, *args) if respond_to?(builder_method)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module GetOrBuild
|
2
|
+
module FieldsForBuilder
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
def fields_for_with_association_builder(record_name, record_object = nil, fields_options = {}, &block)
|
6
|
+
fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
|
7
|
+
|
8
|
+
if fields_options[:build_association] && @object.respond_to?("build_#{record_name}") && !record_object
|
9
|
+
record_object = @object.send("build_#{record_name}")
|
10
|
+
end
|
11
|
+
|
12
|
+
fields_for_without_association_builder(record_name, record_object, fields_options, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method_chain :fields_for, :association_builder
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/get_or_build/version.rb
CHANGED
data/lib/get_or_build.rb
CHANGED
@@ -1,21 +1,6 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
4
|
-
module AssociationBuilder
|
5
|
-
# f.object.location_or_build
|
6
|
-
def method_missing(method, *args)
|
7
|
-
if method =~ /(\w+)_or_build$/
|
8
|
-
if result = send($1, *args)
|
9
|
-
result
|
10
|
-
else
|
11
|
-
builder_method = "build_#{$1}"
|
12
|
-
send(builder_method, *args) if respond_to?(builder_method)
|
13
|
-
end
|
14
|
-
else
|
15
|
-
super
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
1
|
+
require 'get_or_build/version'
|
2
|
+
require 'get_or_build/association_builder'
|
3
|
+
require 'get_or_build/fields_for_builder'
|
20
4
|
|
21
5
|
ActiveRecord::Base.send :include, GetOrBuild::AssociationBuilder if defined?(ActiveRecord)
|
6
|
+
ActionView::Helpers::FormBuilder.send :include, GetOrBuild::FieldsForBuilder if defined?(ActionView)
|
data/test/active_record_test.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActiveRecordTest < Test::Unit::TestCase
|
4
|
+
include Support::ActiveRecordConnection
|
2
5
|
|
3
|
-
class ActiveRecordTest < TestHelper
|
4
6
|
def test_with_new_record
|
5
7
|
c = Company.new
|
6
8
|
assert_not_nil c.location_or_build
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'action_view'
|
3
|
+
|
4
|
+
class FieldsFor < ActionView::TestCase
|
5
|
+
include Support::ActiveRecordConnection
|
6
|
+
|
7
|
+
def test_fields_for_should_receive_build_association_param1
|
8
|
+
company = Company.new
|
9
|
+
assert_equal company.user, nil
|
10
|
+
form_for company, :url => '' do |f|
|
11
|
+
f.fields_for :user, :build_association => true do |ff|
|
12
|
+
assert_not_nil ff.object
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_fields_for_should_receive_build_association_param2
|
18
|
+
company = Company.new
|
19
|
+
assert_equal company.user, nil
|
20
|
+
form_for company, :url => '' do |f|
|
21
|
+
f.fields_for :user, :build_association => false do |ff|
|
22
|
+
assert_equal ff.object, nil
|
23
|
+
end
|
24
|
+
f.fields_for :user do |ff|
|
25
|
+
assert_equal ff.object, nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Support
|
2
|
+
module ActiveRecordConnection
|
3
|
+
def setup
|
4
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
|
5
|
+
:database => ":memory:")
|
6
|
+
ActiveRecord::Base.connection.create_table(:companies) do |t|
|
7
|
+
t.integer :location_id
|
8
|
+
t.string :name
|
9
|
+
end
|
10
|
+
ActiveRecord::Base.connection.create_table(:users) do |t|
|
11
|
+
t.integer :company_id
|
12
|
+
t.string :name
|
13
|
+
end
|
14
|
+
ActiveRecord::Base.connection.create_table(:locations) do |t|
|
15
|
+
t.string :address
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,22 +1,6 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'active_record'
|
3
|
+
require 'action_view'
|
3
4
|
require 'get_or_build'
|
4
|
-
|
5
|
-
|
6
|
-
class TestHelper < Test::Unit::TestCase
|
7
|
-
def setup
|
8
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
|
9
|
-
:database => ":memory:")
|
10
|
-
ActiveRecord::Base.connection.create_table(:companies) do |t|
|
11
|
-
t.integer :location_id
|
12
|
-
t.string :name
|
13
|
-
end
|
14
|
-
ActiveRecord::Base.connection.create_table(:users) do |t|
|
15
|
-
t.integer :company_id
|
16
|
-
t.string :name
|
17
|
-
end
|
18
|
-
ActiveRecord::Base.connection.create_table(:locations) do |t|
|
19
|
-
t.string :address
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
5
|
+
require 'support/models'
|
6
|
+
require 'support/connections'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: get_or_build
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-24 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70105780474200 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,21 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70105780474200
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: actionpack
|
27
|
+
requirement: &70105780473680 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70105780473680
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: sqlite3
|
27
|
-
requirement: &
|
38
|
+
requirement: &70105780473140 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,10 +43,10 @@ dependencies:
|
|
32
43
|
version: '0'
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *70105780473140
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rake
|
38
|
-
requirement: &
|
49
|
+
requirement: &70105780472620 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,7 +54,7 @@ dependencies:
|
|
43
54
|
version: '0'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *70105780472620
|
47
58
|
description: ! 'Assume user belongs_to company and you want to get company object
|
48
59
|
within one method call. You can do it like this: user.company_or_build'
|
49
60
|
email:
|
@@ -58,11 +69,13 @@ files:
|
|
58
69
|
- Rakefile
|
59
70
|
- get_or_build.gemspec
|
60
71
|
- lib/get_or_build.rb
|
72
|
+
- lib/get_or_build/association_builder.rb
|
73
|
+
- lib/get_or_build/fields_for_builder.rb
|
61
74
|
- lib/get_or_build/version.rb
|
62
75
|
- test/active_record_test.rb
|
63
|
-
- test/
|
64
|
-
- test/
|
65
|
-
- test/models
|
76
|
+
- test/fields_for_helper_test.rb
|
77
|
+
- test/support/connections.rb
|
78
|
+
- test/support/models.rb
|
66
79
|
- test/test_helper.rb
|
67
80
|
homepage: ''
|
68
81
|
licenses: []
|
@@ -78,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
78
91
|
version: '0'
|
79
92
|
segments:
|
80
93
|
- 0
|
81
|
-
hash: -
|
94
|
+
hash: -1527763818141659073
|
82
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
96
|
none: false
|
84
97
|
requirements:
|
@@ -87,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
100
|
version: '0'
|
88
101
|
segments:
|
89
102
|
- 0
|
90
|
-
hash: -
|
103
|
+
hash: -1527763818141659073
|
91
104
|
requirements: []
|
92
105
|
rubyforge_project: get_or_build
|
93
106
|
rubygems_version: 1.8.10
|
@@ -96,7 +109,7 @@ specification_version: 3
|
|
96
109
|
summary: Provides method which allows get or build belongs_to or has_one association
|
97
110
|
test_files:
|
98
111
|
- test/active_record_test.rb
|
99
|
-
- test/
|
100
|
-
- test/
|
101
|
-
- test/models
|
112
|
+
- test/fields_for_helper_test.rb
|
113
|
+
- test/support/connections.rb
|
114
|
+
- test/support/models.rb
|
102
115
|
- test/test_helper.rb
|
data/test/models/company.rb
DELETED
data/test/models/location.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
class Location < ActiveRecord::Base; end
|
data/test/models/user.rb
DELETED