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 CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ tmtags
6
+
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 `belogs_to` or `has_one` association.
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 terminal.
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: `ruby test/*.rb` and make sure if everything is workig correctly.
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
@@ -1,3 +1,3 @@
1
1
  module FieldsForBuilder
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/get_or_build.rb CHANGED
@@ -1,21 +1,6 @@
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
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)
@@ -1,6 +1,8 @@
1
- require File.expand_path('../test_helper', __FILE__)
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
@@ -0,0 +1,10 @@
1
+ class Company < ActiveRecord::Base
2
+ belongs_to :location
3
+ has_one :user
4
+ end
5
+
6
+ class User < ActiveRecord::Base
7
+ belongs_to :company
8
+ end
9
+
10
+ class Location < ActiveRecord::Base; 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
- 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
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.1
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-22 00:00:00.000000000Z
12
+ date: 2012-03-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70280247996960 !ruby/object:Gem::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: *70280247996960
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: &70280247996520 !ruby/object:Gem::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: *70280247996520
46
+ version_requirements: *70105780473140
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rake
38
- requirement: &70280247996100 !ruby/object:Gem::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: *70280247996100
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/models/company.rb
64
- - test/models/location.rb
65
- - test/models/user.rb
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: -3989985633040186247
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: -3989985633040186247
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/models/company.rb
100
- - test/models/location.rb
101
- - test/models/user.rb
112
+ - test/fields_for_helper_test.rb
113
+ - test/support/connections.rb
114
+ - test/support/models.rb
102
115
  - test/test_helper.rb
@@ -1,4 +0,0 @@
1
- class Company < ActiveRecord::Base
2
- belongs_to :location
3
- has_one :user
4
- end
@@ -1 +0,0 @@
1
- class Location < ActiveRecord::Base; end
data/test/models/user.rb DELETED
@@ -1,3 +0,0 @@
1
- class User < ActiveRecord::Base
2
- belongs_to :company
3
- end