fabrication 0.8.1 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +3 -3
- data/lib/fabrication/fabricator.rb +1 -1
- data/lib/fabrication/generator/active_record.rb +7 -2
- data/lib/fabrication/generator/base.rb +6 -2
- data/lib/fabrication/schematic.rb +1 -1
- data/lib/fabrication/sequencer.rb +1 -1
- data/lib/fabrication/version.rb +1 -1
- data/lib/rails/generators/fabrication/model/model_generator.rb +3 -3
- data/lib/rails/generators/fabrication/model/templates/fabricator.rb +1 -1
- data/spec/fabrication/generator/active_record_spec.rb +1 -1
- data/spec/fabrication_spec.rb +5 -0
- data/spec/support/active_record.rb +2 -0
- metadata +18 -9
data/README.markdown
CHANGED
@@ -14,9 +14,9 @@ By default it will lazily generate active record associations. So if you have a
|
|
14
14
|
|
15
15
|
If you are fabricating an activerecord backed object and it has attributes that are not columns in the underlying table, Fabrication will lazily generate them even if they are not defined associations. You can easily work around this by adding a "!" to the end of the attribute definition in the Fabricator.
|
16
16
|
|
17
|
-
Fabricator(:user) do
|
18
|
-
|
19
|
-
end
|
17
|
+
Fabricator(:user) do
|
18
|
+
some_delegated_something_or_other! { Fabricate(:something) }
|
19
|
+
end
|
20
20
|
|
21
21
|
### Installation ###
|
22
22
|
|
@@ -4,12 +4,17 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
|
|
4
4
|
defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
|
5
5
|
end
|
6
6
|
|
7
|
+
def associations
|
8
|
+
@associations ||= klass.reflections.keys
|
9
|
+
end
|
10
|
+
|
7
11
|
def method_missing(method_name, *args, &block)
|
8
12
|
method_name = method_name.to_s
|
9
13
|
if block_given?
|
10
14
|
options = args.first || {}
|
11
|
-
|
12
|
-
|
15
|
+
if !options[:force] && associations.include?(method_name.to_sym)
|
16
|
+
count = options[:count] || 0
|
17
|
+
|
13
18
|
# copy the original getter
|
14
19
|
instance.instance_variable_set("@__#{method_name}_original", instance.method(method_name).clone)
|
15
20
|
|
@@ -3,16 +3,18 @@ class Fabrication::Generator::Base
|
|
3
3
|
def self.supports?(klass); true end
|
4
4
|
|
5
5
|
def generate(options={:save => true}, attributes=[], callbacks={})
|
6
|
+
self.instance = klass.new
|
6
7
|
process_attributes(attributes)
|
7
8
|
|
8
9
|
callbacks[:after_build].each { |callback| callback.call(instance) } if callbacks[:after_build]
|
9
10
|
after_generation(options)
|
10
11
|
callbacks[:after_create].each { |callback| callback.call(instance) } if callbacks[:after_create]
|
12
|
+
|
11
13
|
instance
|
12
14
|
end
|
13
15
|
|
14
16
|
def initialize(klass)
|
15
|
-
self.
|
17
|
+
self.klass = klass
|
16
18
|
end
|
17
19
|
|
18
20
|
def method_missing(method_name, *args, &block)
|
@@ -21,7 +23,7 @@ class Fabrication::Generator::Base
|
|
21
23
|
|
22
24
|
protected
|
23
25
|
|
24
|
-
attr_accessor :instance
|
26
|
+
attr_accessor :klass, :instance
|
25
27
|
|
26
28
|
def after_generation(options); end
|
27
29
|
|
@@ -36,6 +38,8 @@ class Fabrication::Generator::Base
|
|
36
38
|
instance.send("#{method_name}=", value)
|
37
39
|
end
|
38
40
|
|
41
|
+
def post_initialize; end
|
42
|
+
|
39
43
|
def process_attributes(attributes)
|
40
44
|
attributes.each do |attribute|
|
41
45
|
if Proc === attribute.value
|
@@ -108,7 +108,7 @@ class Fabrication::Schematic
|
|
108
108
|
def to_hash(attrs, overrides)
|
109
109
|
hash = defined?(HashWithIndifferentAccess) ? HashWithIndifferentAccess.new : {}
|
110
110
|
attributes.inject(hash) do |hash, attribute|
|
111
|
-
value = attribute.value
|
111
|
+
value = Proc === attribute.value ? attribute.value.call : attribute.value
|
112
112
|
hash.merge(attribute.name => value)
|
113
113
|
end.merge(overrides)
|
114
114
|
end
|
data/lib/fabrication/version.rb
CHANGED
@@ -4,11 +4,11 @@ module Fabrication
|
|
4
4
|
module Generators
|
5
5
|
class ModelGenerator < Base
|
6
6
|
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
7
|
-
class_option :dir, :type => :string, :default => "
|
7
|
+
class_option :dir, :type => :string, :default => "spec/fabricators", :desc => "The directory where the fabricators should go"
|
8
8
|
class_option :extension, :type => :string, :default => "rb", :desc => "file extension name"
|
9
|
-
|
9
|
+
|
10
10
|
def create_fabrication_file
|
11
|
-
template 'fabricator.rb', File.join(options[:dir], "#{table_name}.#{options[:extension].to_s}")
|
11
|
+
template 'fabricator.rb', File.join(options[:dir], "#{table_name}_fabricator.#{options[:extension].to_s}")
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
@@ -17,7 +17,7 @@ describe Fabrication::Generator::ActiveRecord do
|
|
17
17
|
|
18
18
|
describe "#after_generation" do
|
19
19
|
let(:instance) { mock(:instance) }
|
20
|
-
let(:generator) { Fabrication::Generator::ActiveRecord.new(
|
20
|
+
let(:generator) { Fabrication::Generator::ActiveRecord.new(Company) }
|
21
21
|
|
22
22
|
before { generator.send(:instance=, instance) }
|
23
23
|
|
data/spec/fabrication_spec.rb
CHANGED
@@ -167,6 +167,7 @@ describe Fabrication do
|
|
167
167
|
Fabricator(:company) do
|
168
168
|
name { Faker::Company.name }
|
169
169
|
divisions!(:count => 4)
|
170
|
+
non_field { "hi" }
|
170
171
|
after_create { |o| o.update_attribute(:city, "Jacksonville Beach") }
|
171
172
|
end
|
172
173
|
|
@@ -187,6 +188,10 @@ describe Fabrication do
|
|
187
188
|
Division.find_all_by_company_id(company.id).count.should == 4
|
188
189
|
end
|
189
190
|
|
191
|
+
it 'generates non-database backed fields immediately' do
|
192
|
+
company.instance_variable_get(:@non_field).should == 'hi'
|
193
|
+
end
|
194
|
+
|
190
195
|
it 'executes after build blocks' do
|
191
196
|
other_company.name.should == 'Crazysauce'
|
192
197
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fabrication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 57
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 8
|
8
|
-
-
|
9
|
-
version: 0.8.
|
9
|
+
- 3
|
10
|
+
version: 0.8.3
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Paul Elliott
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-30 00:00:00 -04:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
@@ -25,6 +26,7 @@ dependencies:
|
|
25
26
|
requirements:
|
26
27
|
- - ">="
|
27
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 62196427
|
28
30
|
segments:
|
29
31
|
- 2
|
30
32
|
- 0
|
@@ -42,6 +44,7 @@ dependencies:
|
|
42
44
|
requirements:
|
43
45
|
- - ">="
|
44
46
|
- !ruby/object:Gem::Version
|
47
|
+
hash: 15
|
45
48
|
segments:
|
46
49
|
- 0
|
47
50
|
- 4
|
@@ -57,6 +60,7 @@ dependencies:
|
|
57
60
|
requirements:
|
58
61
|
- - ">="
|
59
62
|
- !ruby/object:Gem::Version
|
63
|
+
hash: 7
|
60
64
|
segments:
|
61
65
|
- 3
|
62
66
|
- 0
|
@@ -72,6 +76,7 @@ dependencies:
|
|
72
76
|
requirements:
|
73
77
|
- - ">="
|
74
78
|
- !ruby/object:Gem::Version
|
79
|
+
hash: 27
|
75
80
|
segments:
|
76
81
|
- 1
|
77
82
|
- 3
|
@@ -85,13 +90,14 @@ dependencies:
|
|
85
90
|
requirement: &id005 !ruby/object:Gem::Requirement
|
86
91
|
none: false
|
87
92
|
requirements:
|
88
|
-
- - "
|
93
|
+
- - "="
|
89
94
|
- !ruby/object:Gem::Version
|
95
|
+
hash: 31
|
90
96
|
segments:
|
91
97
|
- 1
|
92
98
|
- 0
|
93
|
-
-
|
94
|
-
version: 1.0.
|
99
|
+
- 4
|
100
|
+
version: 1.0.4
|
95
101
|
type: :development
|
96
102
|
version_requirements: *id005
|
97
103
|
- !ruby/object:Gem::Dependency
|
@@ -100,15 +106,16 @@ dependencies:
|
|
100
106
|
requirement: &id006 !ruby/object:Gem::Requirement
|
101
107
|
none: false
|
102
108
|
requirements:
|
103
|
-
- - "
|
109
|
+
- - "="
|
104
110
|
- !ruby/object:Gem::Version
|
111
|
+
hash: 62196423
|
105
112
|
segments:
|
106
113
|
- 2
|
107
114
|
- 0
|
108
115
|
- 0
|
109
116
|
- beta
|
110
|
-
-
|
111
|
-
version: 2.0.0.beta.
|
117
|
+
- 18
|
118
|
+
version: 2.0.0.beta.18
|
112
119
|
type: :development
|
113
120
|
version_requirements: *id006
|
114
121
|
description: Fabrication is an object generation framework for ActiveRecord and Mongoid. It has a flexible syntax and lazily generates ActiveRecord associations!
|
@@ -167,6 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
167
174
|
requirements:
|
168
175
|
- - ">="
|
169
176
|
- !ruby/object:Gem::Version
|
177
|
+
hash: 3
|
170
178
|
segments:
|
171
179
|
- 0
|
172
180
|
version: "0"
|
@@ -175,6 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
183
|
requirements:
|
176
184
|
- - ">="
|
177
185
|
- !ruby/object:Gem::Version
|
186
|
+
hash: 3
|
178
187
|
segments:
|
179
188
|
- 0
|
180
189
|
version: "0"
|