GetOrBuild 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 fields_for_builder.gemspec
4
+ gemspec
@@ -0,0 +1,43 @@
1
+ # GetOrBuild
2
+
3
+ This is a association builder which helps to generate or get associated object with `belogs_to` or `has_one` association.
4
+
5
+ ##Installation
6
+
7
+ Add this line to your Gemfile
8
+ `gem 'get_or_build'`
9
+ then as usually update installed gem by typing `bundle` in terminal.
10
+
11
+ ## Using with ActiveRecord
12
+
13
+ Assume you have model `Company` which has one `User` and belongs to `Location`:
14
+
15
+ class Company < ActiveRecord::Base
16
+ belongs_to :location
17
+ has_one :user
18
+ accepts_nested_attributes_for :user
19
+ accepts_nested_attrbiutes_for :location
20
+ end
21
+
22
+ class User < ActiveRecord::Base; end
23
+ class Location < ActiveRecord::Base; end
24
+
25
+ When you are building form for company with nested attributes for location or user you are able now use new methods like `user_or_build` or `location_or_build`:
26
+
27
+ form_for :company do |f|
28
+ f.fields_for :user, f.object.user_or_build do |fu|
29
+ fu.text_field :name
30
+ f.fields_for :location, f.object.location_or_build do |fl|
31
+ fl.text_field :address
32
+
33
+ No longer need to call `f.object.user || f.object.build_user`
34
+
35
+ ## Using with NoSQL databases
36
+
37
+ Just include in your document module `GetOrBuild::AssociationBuilder` and it will attach magick methods automatically
38
+
39
+ ## Contributing
40
+ You are welcome! Please, run test before pull request: `ruby test/*.rb` and make sure if everything is workig correctly.
41
+
42
+ ## TODO
43
+ * tests for `MongoMapper`
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "get_or_build/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "GetOrBuild"
7
+ s.version = FieldsForBuilder::VERSION
8
+ s.authors = ["Andrey Koleshko"]
9
+ s.email = ["ka8725@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Provides method which allows get or build belongs_to or has_one association}
12
+ s.description = %q{Assume user belongs_to company and you want to get company object within one method call. You can do it like this: user.company_or_build}
13
+
14
+ s.rubyforge_project = "get_or_build"
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 "activerecord"
23
+ s.add_development_dependency "sqlite3"
24
+ s.add_development_dependency "rake"
25
+ # s.add_runtime_dependency "rest-client"
26
+ end
@@ -0,0 +1,21 @@
1
+ require "get_or_build/version"
2
+
3
+ module GetOrBuild
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
20
+
21
+ ActiveRecord::Base.send :include, GetOrBuild::AssociationBuilder if defined?(ActiveRecord)
@@ -0,0 +1,3 @@
1
+ module FieldsForBuilder
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class ActiveRecordTest < TestHelper
4
+ def test_with_new_record
5
+ c = Company.new
6
+ assert_not_nil c.location_or_build
7
+ assert_not_nil c.user_or_build
8
+ end
9
+
10
+ def test_new_record_with_params
11
+ c = Company.new
12
+ assert_not_nil c.location_or_build(:address => 'Address'), 'Address'
13
+ assert_not_nil c.user_or_build(:name => 'User'), 'User'
14
+ end
15
+
16
+ def test_with_persisted_record
17
+ user = User.new(:name => 'Mister Duncan')
18
+ location = Location.new(:address => 'Beverly Hills')
19
+ c = Company.new(:user => user, :location => location)
20
+ assert_equal c.user_or_build.name, user.name
21
+ assert_equal c.location_or_build.address, location.address
22
+ end
23
+
24
+ def test_method_missing
25
+ c = Company.new
26
+ assert_raise NoMethodError do
27
+ c.my_method
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ class Company < ActiveRecord::Base
2
+ belongs_to :location
3
+ has_one :user
4
+ end
@@ -0,0 +1 @@
1
+ class Location < ActiveRecord::Base; end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ belongs_to :company
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'active_record'
3
+ require 'get_or_build'
4
+ Dir[File.expand_path('../models/*.rb', __FILE__)].each {|f| require f}
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
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: GetOrBuild
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrey Koleshko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-22 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70313126662620 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70313126662620
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70313126662180 !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: *70313126662180
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70313126661740 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70313126661740
47
+ description: ! 'Assume user belongs_to company and you want to get company object
48
+ within one method call. You can do it like this: user.company_or_build'
49
+ email:
50
+ - ka8725@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - README.md
58
+ - Rakefile
59
+ - get_or_build.gemspec
60
+ - lib/get_or_build.rb
61
+ - lib/get_or_build/version.rb
62
+ - test/active_record_test.rb
63
+ - test/models/company.rb
64
+ - test/models/location.rb
65
+ - test/models/user.rb
66
+ - test/test_helper.rb
67
+ homepage: ''
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ segments:
80
+ - 0
81
+ hash: 1273128156565898464
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
89
+ - 0
90
+ hash: 1273128156565898464
91
+ requirements: []
92
+ rubyforge_project: get_or_build
93
+ rubygems_version: 1.8.10
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Provides method which allows get or build belongs_to or has_one association
97
+ test_files:
98
+ - test/active_record_test.rb
99
+ - test/models/company.rb
100
+ - test/models/location.rb
101
+ - test/models/user.rb
102
+ - test/test_helper.rb