get_or_build 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -34,10 +34,22 @@ No longer need to call `f.object.user || f.object.build_user`
34
34
 
35
35
  ## Using with NoSQL databases
36
36
 
37
- Just include in your document module `GetOrBuild::AssociationBuilder` and it will attach magick methods automatically
37
+ Just include in your document module `GetOrBuild::AssociationBuilder` and it will attach magic methods automatically
38
+
39
+ ## Extension for fields_for method
40
+
41
+ You are able also to use option `:build_association` for `fields_for` method to build association if it doesn't exist yet:
42
+
43
+ form_for :company do |f|
44
+ f.fields_for :user, :build_association => true do |fu|
45
+ fu.text_field :name
46
+ f.fields_for :user, :build_association => true do |fl|
47
+ fl.text_field :address
48
+
49
+ If you initialize associated object user for company before run `fields_for` helper then option `:build_association` won't give any effect
38
50
 
39
51
  ## Contributing
40
- You are welcome! Please, run test before pull request: `rake` and make sure if everything is workig correctly.
52
+ You are welcome! Please, run test before pull request: `rake` and make sure if everything is working correctly.
41
53
 
42
54
  ## TODO
43
- * tests for `MongoMapper`
55
+ * Support for NoSQL databases
@@ -11,12 +11,12 @@ module GetOrBuild
11
11
  # Here the method `company_or_build` for `User` instane `u` was generated
12
12
  def method_missing(method, *args)
13
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
14
+ if result = send($1, *args) # if result = send("company", *args)
15
+ result # result
16
+ else # else
17
+ builder_method = "build_#{$1}" # builder_method = "build_company"
18
+ send(builder_method, *args) if respond_to?(builder_method) # send(builder_method, *args) if respond_to?(builder_method)
19
+ end # end
20
20
  else
21
21
  super
22
22
  end
@@ -5,8 +5,15 @@ module GetOrBuild
5
5
  def fields_for_with_association_builder(record_name, record_object = nil, fields_options = {}, &block)
6
6
  fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
7
7
 
8
- if fields_options[:build_association] && @object.respond_to?("build_#{record_name}") && !record_object
9
- record_object = @object.send("build_#{record_name}")
8
+ if fields_options.delete(:build_association)
9
+ association_name = case record_name
10
+ when String, Symbol
11
+ record_name
12
+ when Array
13
+ ActiveModel::Naming.param_key(record_name.last)
14
+ end
15
+ build_method = "build_#{association_name}"
16
+ record_object = @object.send(build_method) if !record_object && !@object.send(association_name) && @object.respond_to?(build_method)
10
17
  end
11
18
 
12
19
  fields_for_without_association_builder(record_name, record_object, fields_options, &block)
@@ -1,3 +1,3 @@
1
1
  module FieldsForBuilder
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -10,6 +10,7 @@ class FieldsFor < ActionView::TestCase
10
10
  form_for company, :url => '' do |f|
11
11
  f.fields_for :user, :build_association => true do |ff|
12
12
  assert_not_nil ff.object
13
+ assert_not_nil ff.text_field(:name)
13
14
  end
14
15
  end
15
16
  end
@@ -19,10 +20,43 @@ class FieldsFor < ActionView::TestCase
19
20
  assert_equal company.user, nil
20
21
  form_for company, :url => '' do |f|
21
22
  f.fields_for :user, :build_association => false do |ff|
22
- assert_equal ff.object, nil
23
+ assert_equal nil, ff.object
24
+ assert_equal nil, ff.text_field(:name)
23
25
  end
24
26
  f.fields_for :user do |ff|
25
- assert_equal ff.object, nil
27
+ assert_equal nil, ff.object
28
+ assert_equal nil, ff.text_field(:name)
29
+ end
30
+ end
31
+ end
32
+
33
+ def test_for_existing_record1
34
+ company = Company.new
35
+ user = company.build_user(:name => 'Mister Proper')
36
+ form_for company, :url => '' do |f|
37
+ f.fields_for :user, :build_association => true do |ff|
38
+ assert_not_nil ff.text_field(:name)
39
+ end
40
+ end
41
+ end
42
+
43
+ def test_for_existing_record2
44
+ company = Company.new
45
+ user = company.build_user(:name => 'Mister Proper')
46
+ form_for company, :url => '' do |f|
47
+ f.fields_for :user do |ff|
48
+ assert_not_nil ff.text_field(:name)
49
+ end
50
+ end
51
+ end
52
+
53
+ def test_nested_params
54
+ company = Company.new
55
+ user = company.build_user(:name => 'New User')
56
+ form_for company, :url => '' do |f|
57
+ f.fields_for [:admin, user], :build_association => true do |ff|
58
+ assert_not_nil ff.object
59
+ assert_equal user.name, ff.object.name
26
60
  end
27
61
  end
28
62
  end
@@ -1,10 +1,13 @@
1
1
  class Company < ActiveRecord::Base
2
2
  belongs_to :location
3
3
  has_one :user
4
+ accepts_nested_attributes_for :user
5
+ accepts_nested_attributes_for :location
4
6
  end
5
7
 
6
8
  class User < ActiveRecord::Base
7
9
  belongs_to :company
10
+ accepts_nested_attributes_for :company
8
11
  end
9
12
 
10
13
  class Location < ActiveRecord::Base; end
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.2
4
+ version: 0.0.3
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-24 00:00:00.000000000Z
12
+ date: 2012-03-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70105780474200 !ruby/object:Gem::Requirement
16
+ requirement: &70327775366480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70105780474200
24
+ version_requirements: *70327775366480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: actionpack
27
- requirement: &70105780473680 !ruby/object:Gem::Requirement
27
+ requirement: &70327775365680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70105780473680
35
+ version_requirements: *70327775365680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70105780473140 !ruby/object:Gem::Requirement
38
+ requirement: &70327775364740 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70105780473140
46
+ version_requirements: *70327775364740
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70105780472620 !ruby/object:Gem::Requirement
49
+ requirement: &70327775364040 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70105780472620
57
+ version_requirements: *70327775364040
58
58
  description: ! 'Assume user belongs_to company and you want to get company object
59
59
  within one method call. You can do it like this: user.company_or_build'
60
60
  email:
@@ -91,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  segments:
93
93
  - 0
94
- hash: -1527763818141659073
94
+ hash: 2580832862823747020
95
95
  required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  none: false
97
97
  requirements:
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  version: '0'
101
101
  segments:
102
102
  - 0
103
- hash: -1527763818141659073
103
+ hash: 2580832862823747020
104
104
  requirements: []
105
105
  rubyforge_project: get_or_build
106
106
  rubygems_version: 1.8.10