fabrication 0.7.1 → 0.8.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.
- data/README.markdown +22 -0
- data/lib/fabrication/generator/base.rb +7 -23
- data/lib/fabrication/schematic.rb +21 -1
- data/lib/fabrication/version.rb +1 -1
- data/lib/rails/generators/fabrication/model/model_generator.rb +15 -0
- data/lib/rails/generators/fabrication/model/templates/fabricator.rb +5 -0
- data/lib/rails/generators/fabrication_generator.rb +11 -0
- data/spec/fabrication/generator/base_spec.rb +0 -16
- data/spec/fabrication_spec.rb +20 -0
- data/spec/fabricators.rb +9 -0
- metadata +7 -4
data/README.markdown
CHANGED
@@ -34,6 +34,15 @@ Now you can define fabricators in any of the following locations.
|
|
34
34
|
|
35
35
|
They are automatically loaded, so no additional requires are necessary.
|
36
36
|
|
37
|
+
### Rails 3 Generators ###
|
38
|
+
|
39
|
+
They are really easy to configure! Just add this to your `config/application.rb`:
|
40
|
+
|
41
|
+
config.generators do |g|
|
42
|
+
g.test_framework :rspec, :fixture => true
|
43
|
+
g.fixture_replacement :fabrication, :dir => "spec/fabricators"
|
44
|
+
end
|
45
|
+
|
37
46
|
### Usage ###
|
38
47
|
|
39
48
|
Define your fabricators.
|
@@ -70,6 +79,18 @@ You can also explicitly specify the class being fabricated with the :class_name
|
|
70
79
|
type "LLC"
|
71
80
|
end
|
72
81
|
|
82
|
+
The callbacks will be stacked when inheriting from other fabricators. For example, when you define something like this:
|
83
|
+
|
84
|
+
Fabricator(:user) do
|
85
|
+
after_create { |user| user.confirm! }
|
86
|
+
end
|
87
|
+
|
88
|
+
Fabricator(:admin, :from => :user) do
|
89
|
+
after_create { |user| user.make_admin! }
|
90
|
+
end
|
91
|
+
|
92
|
+
When calling `Fabricate(:admin)`, the user callback will be executed first and then the admin callback.
|
93
|
+
|
73
94
|
### Fabricating ###
|
74
95
|
|
75
96
|
Now that your Fabricators are defined, you can start generating some objects! To generate the LLC from the previous example, just do this:
|
@@ -135,3 +156,4 @@ To run rake successfully:
|
|
135
156
|
* Matt (winescout)
|
136
157
|
* Lar Van Der Jagt (supaspoida)
|
137
158
|
* Justin Smestad (jsmestad)
|
159
|
+
* chanks
|
@@ -2,23 +2,12 @@ class Fabrication::Generator::Base
|
|
2
2
|
|
3
3
|
def self.supports?(klass); true end
|
4
4
|
|
5
|
-
def
|
6
|
-
callbacks[:after_build] = block if block_given?
|
7
|
-
end
|
8
|
-
|
9
|
-
def after_create(&block)
|
10
|
-
callbacks[:after_create] = block if block_given?
|
11
|
-
end
|
12
|
-
|
13
|
-
def callbacks
|
14
|
-
@callbacks ||= {}
|
15
|
-
end
|
16
|
-
|
17
|
-
def generate(options={:save => true}, attributes=[])
|
5
|
+
def generate(options={:save => true}, attributes=[], callbacks={})
|
18
6
|
process_attributes(attributes)
|
19
|
-
|
7
|
+
|
8
|
+
callbacks[:after_build].each { |callback| callback.call(instance) } if callbacks[:after_build]
|
20
9
|
after_generation(options)
|
21
|
-
callbacks[:after_create].call(instance) if callbacks
|
10
|
+
callbacks[:after_create].each { |callback| callback.call(instance) } if callbacks[:after_create]
|
22
11
|
instance
|
23
12
|
end
|
24
13
|
|
@@ -49,15 +38,10 @@ class Fabrication::Generator::Base
|
|
49
38
|
|
50
39
|
def process_attributes(attributes)
|
51
40
|
attributes.each do |attribute|
|
52
|
-
|
53
|
-
|
54
|
-
when :after_build; after_build(&attribute.value)
|
41
|
+
if Proc === attribute.value
|
42
|
+
method_missing(attribute.name, attribute.params, &attribute.value)
|
55
43
|
else
|
56
|
-
|
57
|
-
method_missing(attribute.name, attribute.params, &attribute.value)
|
58
|
-
else
|
59
|
-
method_missing(attribute.name, attribute.value)
|
60
|
-
end
|
44
|
+
method_missing(attribute.name, attribute.value)
|
61
45
|
end
|
62
46
|
end
|
63
47
|
end
|
@@ -13,6 +13,16 @@ class Fabrication::Schematic
|
|
13
13
|
instance_eval(&block) if block_given?
|
14
14
|
end
|
15
15
|
|
16
|
+
def after_build(&block)
|
17
|
+
callbacks[:after_build] ||= []
|
18
|
+
callbacks[:after_build] << block
|
19
|
+
end
|
20
|
+
|
21
|
+
def after_create(&block)
|
22
|
+
callbacks[:after_create] ||= []
|
23
|
+
callbacks[:after_create] << block
|
24
|
+
end
|
25
|
+
|
16
26
|
def attribute(name)
|
17
27
|
attributes.select { |a| a.name == name }.first
|
18
28
|
end
|
@@ -22,16 +32,26 @@ class Fabrication::Schematic
|
|
22
32
|
@attributes ||= []
|
23
33
|
end
|
24
34
|
|
35
|
+
attr_writer :callbacks
|
36
|
+
def callbacks
|
37
|
+
@callbacks ||= {}
|
38
|
+
end
|
39
|
+
|
25
40
|
def generate(options={}, overrides={}, &block)
|
26
41
|
attributes = merge(overrides, &block).attributes
|
27
42
|
if options[:attributes]
|
28
43
|
to_hash(attributes, overrides)
|
29
44
|
else
|
30
|
-
generator.new(klass).generate(options, attributes)
|
45
|
+
generator.new(klass).generate(options, attributes, callbacks)
|
31
46
|
end
|
32
47
|
end
|
33
48
|
|
34
49
|
def initialize_copy(original)
|
50
|
+
self.callbacks = {}
|
51
|
+
original.callbacks.each do |type, callbacks|
|
52
|
+
self.callbacks[type] = callbacks.clone
|
53
|
+
end
|
54
|
+
|
35
55
|
self.attributes = original.attributes.map do |a|
|
36
56
|
Fabrication::Attribute.new(a.name, a.params, a.value)
|
37
57
|
end
|
data/lib/fabrication/version.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/generators/fabrication_generator'
|
2
|
+
|
3
|
+
module Fabrication
|
4
|
+
module Generators
|
5
|
+
class ModelGenerator < Base
|
6
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
7
|
+
class_option :dir, :type => :string, :default => "test/fabricators", :desc => "The directory where the fabricators should go"
|
8
|
+
class_option :extension, :type => :string, :default => "rb", :desc => "file extension name"
|
9
|
+
|
10
|
+
def create_fabrication_file
|
11
|
+
template 'fabricator.rb', File.join(options[:dir], "#{table_name}.#{options[:extension].to_s}")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
|
3
|
+
module Fabrication
|
4
|
+
module Generators
|
5
|
+
class Base < Rails::Generators::NamedBase #:nodoc:
|
6
|
+
def self.source_root
|
7
|
+
@_fabrication_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'fabrication', generator_name, 'templates'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -9,22 +9,6 @@ describe Fabrication::Generator::Base do
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
describe "#after_build" do
|
13
|
-
subject { Fabrication::Generator::Base.new(Object) }
|
14
|
-
before { subject.after_build { "something" } }
|
15
|
-
it "stores the after create block" do
|
16
|
-
Proc.should === subject.callbacks[:after_build]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "#after_create" do
|
21
|
-
subject { Fabrication::Generator::Base.new(Object) }
|
22
|
-
before { subject.after_create { "something" } }
|
23
|
-
it "stores the after create block" do
|
24
|
-
Proc.should === subject.callbacks[:after_create]
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
12
|
describe "#generate" do
|
29
13
|
|
30
14
|
let(:generator) { Fabrication::Generator::Base.new(Person) }
|
data/spec/fabrication_spec.rb
CHANGED
@@ -219,6 +219,26 @@ describe Fabrication do
|
|
219
219
|
end
|
220
220
|
end
|
221
221
|
|
222
|
+
context 'with multiple callbacks' do
|
223
|
+
let(:child) { Fabricate(:child) }
|
224
|
+
|
225
|
+
it "runs the first callback" do
|
226
|
+
child.first_name.should == "Johnny"
|
227
|
+
end
|
228
|
+
|
229
|
+
it "runs the second callback" do
|
230
|
+
child.age.should == 10
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
context 'with multiple, inherited callbacks' do
|
235
|
+
let(:senior) { Fabricate(:senior) }
|
236
|
+
|
237
|
+
it "runs the parent callbacks first" do
|
238
|
+
senior.age.should == 70
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
222
242
|
context 'with a parent fabricator' do
|
223
243
|
|
224
244
|
context 'and a previously defined parent' do
|
data/spec/fabricators.rb
CHANGED
@@ -20,6 +20,15 @@ Fabricator(:person) do
|
|
20
20
|
location
|
21
21
|
end
|
22
22
|
|
23
|
+
Fabricator(:child, :from => :person) do
|
24
|
+
after_build { |child| child.first_name = 'Johnny' }
|
25
|
+
after_build { |child| child.age = 10 }
|
26
|
+
end
|
27
|
+
|
28
|
+
Fabricator(:senior, :from => :child) do
|
29
|
+
after_build { |senior| senior.age *= 7 }
|
30
|
+
end
|
31
|
+
|
23
32
|
# ActiveRecord Objects
|
24
33
|
Fabricator(:division) do
|
25
34
|
name "Division Name"
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 8
|
8
|
+
- 0
|
9
|
+
version: 0.8.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Paul Elliott
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-13 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -132,6 +132,9 @@ files:
|
|
132
132
|
- lib/fabrication/support.rb
|
133
133
|
- lib/fabrication/version.rb
|
134
134
|
- lib/fabrication.rb
|
135
|
+
- lib/rails/generators/fabrication/model/model_generator.rb
|
136
|
+
- lib/rails/generators/fabrication/model/templates/fabricator.rb
|
137
|
+
- lib/rails/generators/fabrication_generator.rb
|
135
138
|
- spec/fabrication/attribute_spec.rb
|
136
139
|
- spec/fabrication/fabricator_spec.rb
|
137
140
|
- spec/fabrication/generator/active_record_spec.rb
|