fabrication 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -34,6 +34,7 @@ Define your fabricators.
34
34
  name "Fun Factory"
35
35
  employees(:count => 20) { |company, i| Fabricate(:drone, :company => company, :name => "Drone #{i}") }
36
36
  location! { Fabricate(:location) }
37
+ after_build { |company| company.name = "Another #{company.name}" }
37
38
  after_create { |company| company.update_attribute(:ceo, Fabricate(:drone, :name => 'Lead Drone') }
38
39
  end
39
40
 
@@ -42,6 +43,7 @@ Breaking down the above, we are defining a "company" fabricator, which will gene
42
43
  * The object has a name field, which is statically filled with "Fun Factory".
43
44
  * It has a has_many association to employees and will generate an array of 20 records as indicated by the :count => 20. The block is passed the company object being fabricated and index of the array being created.
44
45
  * It has a belongs_to association to location and this will be generated immediately with the company object. This is because of the "!" after the association name.
46
+ * After the object is built but before it is saved, it will update the name to "Another Fun Factory".
45
47
  * After the object is created, it will update the "ceo" association with a new "drone" record.
46
48
 
47
49
  Alternatively, you can Fabricate(:company) without first defining the Fabricator. Doing so will create an empty Fabricator called ":company" and prevent you from defining the Fabricator explicitly later.
@@ -118,3 +120,11 @@ To run rake successfully:
118
120
  3. Install bundler
119
121
  4. Run `bundle install` from the project root
120
122
  5. Run `rake` and the test suite should be all green!
123
+
124
+ ### Contributors ###
125
+
126
+ * Les Hill (leshill)
127
+ * Jim Remsik (bigtiger)
128
+ * Dave Ott (daveott)
129
+ * Matt (winescout)
130
+ * Lar Van Der Jagt (supaspoida)
@@ -46,3 +46,10 @@ class Fabrication::Fabricator
46
46
  end
47
47
 
48
48
  end
49
+
50
+ # Adds support for reload! in the Rails 2.3.x console
51
+ if defined? ActionController and ActionController::Dispatcher.respond_to? :reload_application
52
+ ActionController::Dispatcher.to_prepare :fabrication do
53
+ Fabrication.clear_definitions
54
+ end
55
+ end
@@ -2,14 +2,23 @@ class Fabrication::Generator::Base
2
2
 
3
3
  def self.supports?(klass); true end
4
4
 
5
+ def after_build(&block)
6
+ callbacks[:after_build] = block if block_given?
7
+ end
8
+
5
9
  def after_create(&block)
6
- self.after_create_block = block if block_given?
10
+ callbacks[:after_create] = block if block_given?
11
+ end
12
+
13
+ def callbacks
14
+ @callbacks ||= {}
7
15
  end
8
16
 
9
17
  def generate(options={:save => true}, attributes=[])
10
18
  process_attributes(attributes)
19
+ callbacks[:after_build].call(instance) if callbacks.include?(:after_build)
11
20
  after_generation(options)
12
- after_create_block.call(instance) if after_create_block
21
+ callbacks[:after_create].call(instance) if callbacks.include?(:after_create)
13
22
  instance
14
23
  end
15
24
 
@@ -23,11 +32,9 @@ class Fabrication::Generator::Base
23
32
 
24
33
  protected
25
34
 
26
- def after_generation(options); end
27
-
28
- private
35
+ attr_accessor :instance
29
36
 
30
- attr_accessor :after_create_block, :instance
37
+ def after_generation(options); end
31
38
 
32
39
  def assign(method_name, param)
33
40
  value = nil
@@ -43,8 +50,9 @@ class Fabrication::Generator::Base
43
50
 
44
51
  def process_attributes(attributes)
45
52
  attributes.each do |attribute|
46
- if attribute.name == :after_create
47
- after_create(&attribute.value)
53
+ case attribute.name
54
+ when :after_create; after_create(&attribute.value)
55
+ when :after_build; after_build(&attribute.value)
48
56
  else
49
57
  if Proc === attribute.value
50
58
  method_missing(attribute.name, attribute.params, &attribute.value)
@@ -26,10 +26,10 @@ class Fabrication::Support
26
26
  ['test', 'spec'].map do |folder|
27
27
  path = File.expand_path(File.join(folder, 'fabricators'))
28
28
 
29
- require("#{path}.rb") if File.exists?("#{path}.rb")
29
+ load("#{path}.rb") if File.exists?("#{path}.rb")
30
30
 
31
31
  File.directory? path and Dir[File.join(path, '*.rb')].each do |file|
32
- require file
32
+ load file
33
33
  end
34
34
  end
35
35
  end
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '0.6.2'
2
+ VERSION = '0.6.3'
3
3
  end
@@ -102,10 +102,12 @@ describe Fabrication do
102
102
 
103
103
  Fabricator(:other_company, :from => :company) do
104
104
  divisions(:count => 2) { Fabricate(:division) }
105
+ after_build { |o| o.name = "Crazysauce" }
105
106
  end
106
107
  end
107
108
 
108
109
  let(:company) { Fabricate(:company, :name => "Hashrocket") }
110
+ let(:other_company) { Fabricate(:other_company) }
109
111
 
110
112
  it 'generates field blocks immediately' do
111
113
  company.name.should == "Hashrocket"
@@ -115,6 +117,10 @@ describe Fabrication do
115
117
  Division.find_all_by_company_id(company.id).count.should == 4
116
118
  end
117
119
 
120
+ it 'executes after build blocks' do
121
+ other_company.name.should == 'Crazysauce'
122
+ end
123
+
118
124
  it 'executes after create blocks' do
119
125
  company.city.should == 'Jacksonville Beach'
120
126
  end
@@ -124,7 +130,7 @@ describe Fabrication do
124
130
  end
125
131
 
126
132
  it 'overrides inherited associations' do
127
- Fabricate(:other_company).divisions.count.should == 2
133
+ other_company.divisions.count.should == 2
128
134
  Division.count.should == 2
129
135
  end
130
136
 
@@ -0,0 +1 @@
1
+ Fabricator(:awesome_object, :from => :object)
@@ -0,0 +1 @@
1
+ Fabricator(:cool_object, :from => :object)
@@ -9,11 +9,19 @@ 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
+
12
20
  describe "#after_create" do
13
21
  subject { Fabrication::Generator::Base.new(Object) }
14
22
  before { subject.after_create { "something" } }
15
23
  it "stores the after create block" do
16
- Proc.should === subject.send(:after_create_block)
24
+ Proc.should === subject.callbacks[:after_create]
17
25
  end
18
26
  end
19
27
 
data/spec/support_spec.rb CHANGED
@@ -36,17 +36,7 @@ describe Fabrication::Support do
36
36
 
37
37
  describe ".find_definitions" do
38
38
 
39
- before(:all) do
40
- File.open("spec/fabricators.rb", "w") do |f|
41
- f.write("Fabricator(:awesome_object, :from => :object)")
42
- end
43
- File.open("spec/fabricators/cool_object.rb", "w") do |f|
44
- f.write("Fabricator(:cool_object, :from => :object)")
45
- end
46
- Fabrication::Support.find_definitions
47
- File.delete("spec/fabricators.rb")
48
- File.delete("spec/fabricators/cool_object.rb")
49
- end
39
+ before(:all) { Fabrication::Support.find_definitions }
50
40
 
51
41
  it "has an awesome object" do
52
42
  Fabrication::Fabricator.schematics[:awesome_object].should be
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fabrication
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 2
10
- version: 0.6.2
9
+ - 3
10
+ version: 0.6.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Paul Elliott
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-16 00:00:00 -04:00
18
+ date: 2010-08-18 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -136,6 +136,8 @@ files:
136
136
  - lib/fabrication.rb
137
137
  - spec/fabrication_spec.rb
138
138
  - spec/fabricator_spec.rb
139
+ - spec/fabricators/cool_object_fabricator.rb
140
+ - spec/fabricators.rb
139
141
  - spec/generator/active_record_spec.rb
140
142
  - spec/generator/base_spec.rb
141
143
  - spec/generator/mongoid_spec.rb