fabrication 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -11,14 +11,6 @@ Currently supported object types are...
11
11
 
12
12
  By default it will lazily generate active record associations. So if you have a has_many :widgets defined, it will not actually generate the widgets until the association is accessed. You can override this by appending "!" to the name of the parameter when defining the field in the Fabricator.
13
13
 
14
- ### Important Thing To Note! ###
15
-
16
- 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.
17
-
18
- Fabricator(:user) do
19
- some_delegated_something_or_other! { Fabricate(:something) }
20
- end
21
-
22
14
  ### Installation ###
23
15
 
24
16
  Add this to your gemfile.
@@ -48,7 +40,11 @@ They are really easy to configure! Just add this to your `config/application.rb`
48
40
 
49
41
  Packaged with the gem is a generator which will load some handy cucumber table steps into your step_definitions folder. You can get them by running `rails g fabrication:cucumber_steps`.
50
42
 
51
- To generating a single "widget" object (expecting a Fabricator(:widget) to be defined):
43
+ To generate a single "widget" object (expecting a Fabricator(:widget) to be defined):
44
+
45
+ Given 1 widget
46
+
47
+ To generate a single "widget" object with specified attributes:
52
48
 
53
49
  Given the following widget:
54
50
  | name | widget_1 |
@@ -56,6 +52,10 @@ To generating a single "widget" object (expecting a Fabricator(:widget) to be de
56
52
 
57
53
  To generate multiple "widget" objects:
58
54
 
55
+ Given 10 widgets
56
+
57
+ To generate multiple "widget" objects with specified attributes:
58
+
59
59
  Given the following widgets:
60
60
  | name | color |
61
61
  | widget_1 | red |
@@ -68,7 +68,7 @@ To generate "wockets" nested within "widget" objects:
68
68
  | title | Amazing |
69
69
  | category | fancy |
70
70
 
71
- That will use the most recently defined "widget" and pass it into the Fabricator. That obviously requires your "wocket" to have a setter for a "widget".
71
+ That will use the most recently defined "widget" and pass it into the Fabricator. That requires your "wocket" to have a setter for a "widget".
72
72
 
73
73
  ### Usage ###
74
74
 
@@ -196,3 +196,4 @@ To run rake successfully:
196
196
  * monsterlabs - Rails 3 Generators
197
197
  * Brandon Weiss (brandonweiss) - Sequal Model support
198
198
  * Tim Pope (tpope) - Singularize generated fabricator names
199
+ * hakanensari - Default attribute cucumber steps
data/lib/fabrication.rb CHANGED
@@ -57,3 +57,9 @@ class Fabricate
57
57
  end
58
58
 
59
59
  end
60
+
61
+ class Fabricator
62
+ def self.name_for(name)
63
+ Fabrication::Support.name_for(name)
64
+ end
65
+ end
@@ -43,7 +43,7 @@ class Fabrication::Fabricator
43
43
  end
44
44
 
45
45
  # Adds support for reload! in the Rails 2.3.x console
46
- if defined? ActionController and ActionController::Dispatcher.respond_to? :reload_application
46
+ if defined? ActionController::Dispatcher and ActionController::Dispatcher.respond_to? :reload_application
47
47
  ActionController::Dispatcher.to_prepare :fabrication do
48
48
  Fabrication.clear_definitions
49
49
  end
@@ -35,6 +35,10 @@ class Fabrication::Support
35
35
  end
36
36
  end
37
37
 
38
+ def name_for(name)
39
+ name.gsub(" ", "_").singularize.to_sym
40
+ end
41
+
38
42
  end
39
43
 
40
44
  end
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '0.9.4'
2
+ VERSION = '0.9.5'
3
3
  end
@@ -1,54 +1,53 @@
1
1
  module FabricationMethods
2
2
  def create_from_table(model_name, table, extra = {})
3
- fabricator_name = model_name.gsub(/\W+/, '_').downcase.singularize.to_sym
3
+ fabricator_name = generate_fabricator_name(model_name)
4
4
  is_singular = model_name.to_s.singularize == model_name.to_s
5
- hashes = if is_singular
6
- [table.rows_hash]
7
- else
8
- table.hashes
9
- end
5
+ hashes = is_singular ? [table.rows_hash] : table.hashes
10
6
  @they = hashes.map do |hash|
11
7
  hash = hash.merge(extra).inject({}) {|h,(k,v)| h.update(k.gsub(/\W+/,'_').to_sym => v)}
12
- object = Fabricate.build(fabricator_name, hash)
13
- yield object if block_given?
14
- object.save!
15
- object
8
+ Fabricate(fabricator_name, hash)
16
9
  end
17
10
  if is_singular
18
11
  @it = @they.last
19
12
  instance_variable_set("@#{fabricator_name}", @it)
20
13
  end
21
14
  end
15
+
16
+ def create_with_default_attributes(model_name, count, extra = {})
17
+ fabricator_name = generate_fabricator_name(model_name)
18
+ is_singular = count == 1
19
+ @they = count.times.map { Fabricate(fabricator_name, extra) }
20
+ if is_singular
21
+ @it = @they.last
22
+ instance_variable_set("@#{fabricator_name}", @it)
23
+ end
24
+ end
25
+
26
+ def generate_fabricator_name(model_name)
27
+ model_name.gsub(/\W+/, '_').downcase.singularize.to_sym
28
+ end
22
29
  end
23
30
 
24
31
  World(FabricationMethods)
25
32
 
26
- Given %r{^the following (.+):$} do |model_name, table|
33
+ Given /^(\d+) ([^"]*)$/ do |count, model_name|
34
+ create_with_default_attributes(model_name, count.to_i)
35
+ end
36
+
37
+ Given /^the following ([^"]*):$/ do |model_name, table|
27
38
  create_from_table(model_name, table)
28
39
  end
29
40
 
30
- Given %r{^that (.+) has the following (.+):$} do |parent, child, table|
31
- child= child.gsub(/\W+/,'_')
32
- is_child_plural = child.pluralize == child
41
+ Given /^that ([^"]*) has the following ([^"]*):$/ do |parent, child, table|
33
42
  parent = parent.gsub(/\W+/,'_').downcase.sub(/^_/, '')
34
43
  parent_instance = instance_variable_get("@#{parent}")
35
- parent_class = parent_instance.class
36
- if assoc = parent_class.reflect_on_association(child.to_sym) || parent_class.reflect_on_association(child.pluralize.to_sym)
37
- parent = (assoc.options[:as] || parent).to_s
38
- child = (assoc.options[:class_name] || assoc.options[:source] || child).to_s
39
- # source will always be singular, repluralize based on original argument
40
- child = child.pluralize if is_child_plural
41
- end
42
- if child.classify.constantize.method_defined?(parent.pluralize)
43
- create_from_table(child, table, parent.pluralize => [parent_instance])
44
- elsif child.classify.constantize.method_defined?(parent)
45
- create_from_table(child, table, parent => parent_instance)
46
- else
47
- create_from_table(child, table)
48
- if assoc.macro == :has_many
49
- parent_instance.send("#{assoc.name}=", @they)
50
- else
51
- parent_instance.send("#{assoc.name}=", @they.first)
52
- end
44
+ child = child.gsub(/\W+/,'_').downcase
45
+
46
+ child_class = Fabrication::Support.class_for(child.singularize)
47
+ if child_class && !child_class.new.respond_to?("#{parent}=")
48
+ parent = parent.pluralize
49
+ parent_instance = [parent_instance]
53
50
  end
51
+
52
+ create_from_table(child, table, parent => parent_instance)
54
53
  end
@@ -51,4 +51,30 @@ describe Fabrication::Support do
51
51
 
52
52
  end
53
53
 
54
+ describe ".name_for" do
55
+
56
+ context "whitespace delimited" do
57
+ context "singular" do
58
+ subject { Fabrication::Support.name_for("interesting location") }
59
+ it { should == :interesting_location }
60
+ end
61
+ context "plural" do
62
+ subject { Fabrication::Support.name_for("interesting locations") }
63
+ it { should == :interesting_location }
64
+ end
65
+ end
66
+
67
+ context "underscored" do
68
+ context "singular" do
69
+ subject { Fabrication::Support.name_for("interesting_location") }
70
+ it { should == :interesting_location }
71
+ end
72
+ context "plural" do
73
+ subject { Fabrication::Support.name_for("interesting_locations") }
74
+ it { should == :interesting_location }
75
+ end
76
+ end
77
+
78
+ end
79
+
54
80
  end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fabricator do
4
+
5
+ describe ".name_for" do
6
+
7
+ it "delegates to Fabrication::Support" do
8
+ Fabrication::Support.should_receive(:name_for).with("interesting location")
9
+ Fabricator.name_for("interesting location")
10
+ end
11
+
12
+ end
13
+
14
+ end
data/spec/fabricators.rb CHANGED
@@ -11,6 +11,7 @@ Fabricator(:location) do
11
11
  lat 35
12
12
  lng 40
13
13
  end
14
+ Fabricator(:interesting_location, :from => :location)
14
15
 
15
16
  Fabricator(:person) do
16
17
  first_name "John"
@@ -42,7 +43,7 @@ end
42
43
  Fabricator(:author) do
43
44
  name 'George Orwell'
44
45
  books(:count => 4) do |author, i|
45
- Fabricate(:book, :title => "book title #{i}", :author => author)
46
+ Fabricate.build(:book, :title => "book title #{i}", :author => author)
46
47
  end
47
48
  end
48
49
 
data/spec/spec_helper.rb CHANGED
@@ -6,3 +6,9 @@ require 'ffaker'
6
6
  # Requires supporting files with custom matchers and macros, etc,
7
7
  # in ./support/ and its subdirectories.
8
8
  Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
9
+
10
+ Rspec.configure do |config|
11
+ config.before(:all) do
12
+ clear_mongodb
13
+ end
14
+ end
@@ -9,12 +9,6 @@ def clear_mongodb
9
9
  Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
10
10
  end
11
11
 
12
- Rspec.configure do |config|
13
- config.before(:all) do
14
- clear_mongodb
15
- end
16
- end
17
-
18
12
  class Author
19
13
  include Mongoid::Document
20
14
 
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: 49
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 9
8
- - 4
9
- version: 0.9.4
9
+ - 5
10
+ version: 0.9.5
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-12-02 00:00:00 -05:00
18
+ date: 2011-01-17 00:00:00 -05: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: 7
28
30
  segments:
29
31
  - 2
30
32
  - 2
@@ -33,97 +35,119 @@ dependencies:
33
35
  type: :development
34
36
  version_requirements: *id001
35
37
  - !ruby/object:Gem::Dependency
36
- name: ffaker
38
+ name: cucumber
37
39
  prerelease: false
38
40
  requirement: &id002 !ruby/object:Gem::Requirement
39
41
  none: false
40
42
  requirements:
41
43
  - - ">="
42
44
  - !ruby/object:Gem::Version
45
+ hash: 55
46
+ segments:
47
+ - 0
48
+ - 10
49
+ - 0
50
+ version: 0.10.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: ffaker
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 15
43
62
  segments:
44
63
  - 0
45
64
  - 4
46
65
  - 0
47
66
  version: 0.4.0
48
67
  type: :development
49
- version_requirements: *id002
68
+ version_requirements: *id003
50
69
  - !ruby/object:Gem::Dependency
51
70
  name: activerecord
52
71
  prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
72
+ requirement: &id004 !ruby/object:Gem::Requirement
54
73
  none: false
55
74
  requirements:
56
75
  - - ">="
57
76
  - !ruby/object:Gem::Version
77
+ hash: 1
58
78
  segments:
59
79
  - 3
60
80
  - 0
61
81
  - 3
62
82
  version: 3.0.3
63
83
  type: :development
64
- version_requirements: *id003
84
+ version_requirements: *id004
65
85
  - !ruby/object:Gem::Dependency
66
86
  name: sqlite3-ruby
67
87
  prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
88
+ requirement: &id005 !ruby/object:Gem::Requirement
69
89
  none: false
70
90
  requirements:
71
91
  - - "="
72
92
  - !ruby/object:Gem::Version
93
+ hash: 25
73
94
  segments:
74
95
  - 1
75
96
  - 3
76
97
  - 1
77
98
  version: 1.3.1
78
99
  type: :development
79
- version_requirements: *id004
100
+ version_requirements: *id005
80
101
  - !ruby/object:Gem::Dependency
81
102
  name: bson_ext
82
103
  prerelease: false
83
- requirement: &id005 !ruby/object:Gem::Requirement
104
+ requirement: &id006 !ruby/object:Gem::Requirement
84
105
  none: false
85
106
  requirements:
86
- - - "="
107
+ - - ">="
87
108
  - !ruby/object:Gem::Version
109
+ hash: 25
88
110
  segments:
89
111
  - 1
90
112
  - 1
91
- - 4
92
- version: 1.1.4
113
+ - 5
114
+ version: 1.1.5
93
115
  type: :development
94
- version_requirements: *id005
116
+ version_requirements: *id006
95
117
  - !ruby/object:Gem::Dependency
96
118
  name: mongoid
97
119
  prerelease: false
98
- requirement: &id006 !ruby/object:Gem::Requirement
120
+ requirement: &id007 !ruby/object:Gem::Requirement
99
121
  none: false
100
122
  requirements:
101
123
  - - "="
102
124
  - !ruby/object:Gem::Version
125
+ hash: 15424093
103
126
  segments:
104
127
  - 2
105
128
  - 0
106
129
  - 0
107
- - beta
108
- - 20
109
- version: 2.0.0.beta.20
130
+ - rc
131
+ - 4
132
+ version: 2.0.0.rc.4
110
133
  type: :development
111
- version_requirements: *id006
134
+ version_requirements: *id007
112
135
  - !ruby/object:Gem::Dependency
113
136
  name: sequel
114
137
  prerelease: false
115
- requirement: &id007 !ruby/object:Gem::Requirement
138
+ requirement: &id008 !ruby/object:Gem::Requirement
116
139
  none: false
117
140
  requirements:
118
141
  - - "="
119
142
  - !ruby/object:Gem::Version
143
+ hash: 71
120
144
  segments:
121
145
  - 3
122
146
  - 16
123
147
  - 0
124
148
  version: 3.16.0
125
149
  type: :development
126
- version_requirements: *id007
150
+ version_requirements: *id008
127
151
  description: Fabrication is an object generation framework for ActiveRecord, Mongoid, and Sequel. It has a sensible syntax and lazily generates ActiveRecord associations!
128
152
  email:
129
153
  - paul@hashrocket.com
@@ -162,6 +186,7 @@ files:
162
186
  - spec/fabrication/sequence_spec.rb
163
187
  - spec/fabrication/support_spec.rb
164
188
  - spec/fabrication_spec.rb
189
+ - spec/fabricator_spec.rb
165
190
  - spec/fabricators/cool_object_fabricator.rb
166
191
  - spec/fabricators.rb
167
192
  - spec/spec_helper.rb
@@ -184,6 +209,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
184
209
  requirements:
185
210
  - - ">="
186
211
  - !ruby/object:Gem::Version
212
+ hash: 3
187
213
  segments:
188
214
  - 0
189
215
  version: "0"
@@ -192,6 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
218
  requirements:
193
219
  - - ">="
194
220
  - !ruby/object:Gem::Version
221
+ hash: 3
195
222
  segments:
196
223
  - 0
197
224
  version: "0"