fabrication 2.14.1 → 2.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad8f6d00828ef2c427facbc0d1ee74c0ecb94cc3
4
- data.tar.gz: 2e032870ae0816c39d87d07e7c71e8daeddbfeeb
3
+ metadata.gz: 7ca83ffb9ee5c148c97f69e49b53abea8a7d6824
4
+ data.tar.gz: 16786e33e23d47aa85002d4b625d42e48464806d
5
5
  SHA512:
6
- metadata.gz: dd36d36887c0e11ca4ff79f989504a7a4d5ff3c982376ea54ffacd7e63340d2db7a9b5f02d2af4be58f6cd9753d23a34eb9411291b1267666dbc031ecd2022b6
7
- data.tar.gz: cff7cb786ac414d4bc1278f9d63a1ae15a592aa4bfaac660f855c2df047fbc135cb1dd9116479167034aecc7b9409d88f9fba9209cc8471ba545fd7e2d15a9c4
6
+ metadata.gz: 499cd61316ffe755dae558da80a46e475d46d5b42a2728666eb952c9e691281feb1d2dd6a49882b5430de2ff3ae1d512c68321c108e3d4aa7f3122c919498252
7
+ data.tar.gz: 5970658104e0ab9e9572c7f68272ba9e8ff9f8167da59ace22e3053b720cba0056cdebc806fb4561594343c2cbaa90bd2da1243ad294ee4bf4ea344b26036b57
@@ -6,7 +6,7 @@ Fabrication is an object generation framework for Ruby.
6
6
 
7
7
  ## Compatibility
8
8
 
9
- Fabrication is tested against Ruby 1.9.3, 2.0.0, 2.1.x, and Rubinius.
9
+ Fabrication is tested against Ruby 1.9.3 and above.
10
10
 
11
11
  [![Build Status](https://secure.travis-ci.org/paulelliott/fabrication.png)](http://travis-ci.org/paulelliott/fabrication)
12
12
  [![Code Climate](https://codeclimate.com/github/paulelliott/fabrication.png)](https://codeclimate.com/github/paulelliott/fabrication)
@@ -12,10 +12,44 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
12
12
  end
13
13
  end
14
14
 
15
- protected
15
+ def build_default_expansion(field_name, params)
16
+ # When an inverse field is found, we can override this in the fabricator so we
17
+ # don't run into an endless recursion
18
+ build_args = {}
19
+ if inverse_of = inverse_of(field_name)
20
+ inverse_name = inverse_of.name.to_s
21
+ if belongs_to_or_has_one?(inverse_of)
22
+ build_args[inverse_name] = nil
23
+ else
24
+ build_args[inverse_name] = []
25
+ end
26
+ end
27
+
28
+ if params[:count]
29
+ fabricator_name = params[:fabricator] || Fabrication::Support.singularize(field_name.to_s)
30
+ proc { Fabricate.build(params[:fabricator] || fabricator_name, build_args) }
31
+ else
32
+ fabricator_name = params[:fabricator] || field_name
33
+ proc { Fabricate(params[:fabricator] || fabricator_name, build_args) }
34
+ end
35
+ end
36
+
37
+ private
16
38
 
17
- def validate_instance
18
- _instance.valid?
39
+ # Returns the inverse field name of a given relation
40
+ def inverse_of(name)
41
+ reflection = _klass.reflections.fetch(name.to_s, nil) || # >= Rails 4.3
42
+ _klass.reflections.fetch(name.to_sym, nil) # < Rails 4.3
43
+ reflection.inverse_of if reflection
19
44
  end
20
45
 
46
+ # Returns true if the reflection is a belongs_to or has_one association
47
+ def belongs_to_or_has_one?(reflection)
48
+ if reflection.respond_to?(:macro) # < Rails 4.3
49
+ return true if reflection.macro == :belongs_to || reflection.macro == :has_one
50
+ else # >= Rails 5.3
51
+ return true if reflection.instance_of?(ActiveRecord::Reflection::BelongsToReflection) ||
52
+ reflection.instance_of?(ActiveRecord::Reflection::HasOneReflection)
53
+ end
54
+ end
21
55
  end
@@ -19,7 +19,6 @@ class Fabrication::Generator::Base
19
19
  def create(attributes=[], callbacks=[])
20
20
  build(attributes, callbacks)
21
21
  execute_callbacks(callbacks[:before_validation])
22
- validate_instance
23
22
  execute_callbacks(callbacks[:after_validation])
24
23
  execute_callbacks(callbacks[:before_save])
25
24
  execute_callbacks(callbacks[:before_create])
@@ -80,7 +79,14 @@ class Fabrication::Generator::Base
80
79
  _attributes[method_name] || super
81
80
  end
82
81
 
83
- def validate_instance; end
82
+ def build_default_expansion(name, params)
83
+ if params[:count]
84
+ name = Fabrication::Support.singularize(name.to_s)
85
+ proc { Fabricate.build(params[:fabricator] || name) }
86
+ else
87
+ proc { Fabricate(params[:fabricator] || name) }
88
+ end
89
+ end
84
90
 
85
91
  protected
86
92
 
@@ -8,10 +8,6 @@ class Fabrication::Generator::DataMapper < Fabrication::Generator::Base
8
8
  self._instance = _klass.new(_attributes)
9
9
  end
10
10
 
11
- def validate_instance
12
- _instance.valid?
13
- end
14
-
15
11
  protected
16
12
 
17
13
  def persist
@@ -12,8 +12,4 @@ class Fabrication::Generator::Mongoid < Fabrication::Generator::Base
12
12
  end
13
13
  end
14
14
 
15
- def validate_instance
16
- _instance.valid?
17
- end
18
-
19
15
  end
@@ -26,10 +26,6 @@ class Fabrication::Generator::Sequel < Fabrication::Generator::Base
26
26
  _instance.save
27
27
  end
28
28
 
29
- def validate_instance
30
- _instance.valid?
31
- end
32
-
33
29
  private
34
30
 
35
31
  def load_instance_hooks
@@ -105,12 +105,7 @@ class Fabrication::Schematic::Definition
105
105
  end
106
106
 
107
107
  def generate_value(name, params)
108
- if params[:count]
109
- name = Fabrication::Support.singularize(name.to_s)
110
- proc { Fabricate.build(params[:fabricator] || name) }
111
- else
112
- proc { Fabricate(params[:fabricator] || name) }
113
- end
108
+ generator.new(klass).build_default_expansion(name, params)
114
109
  end
115
110
 
116
111
  def merge(overrides={}, &block)
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '2.14.1'
2
+ VERSION = '2.15.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fabrication
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.14.1
4
+ version: 2.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Elliott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-08 00:00:00.000000000 Z
11
+ date: 2016-03-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fabrication is an object generation framework for ActiveRecord, Mongoid,
14
14
  DataMapper, Sequel, or any other Ruby object.
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  version: '0'
70
70
  requirements: []
71
71
  rubyforge_project:
72
- rubygems_version: 2.4.6
72
+ rubygems_version: 2.5.1
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: Implementing the factory pattern in Ruby so you don't have to.