stepford 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +25 -33
  2. data/lib/stepford/version.rb +1 -1
  3. metadata +2 -2
data/README.md CHANGED
@@ -131,17 +131,6 @@ Then you can just use `deep_create`, `deep_create_list`, `deep_build`, `deep_bui
131
131
 
132
132
  deep_create(:foo)
133
133
 
134
- ##### Stopping Circular References
135
-
136
- If you have a circular reference (A has NOT NULL foreign key to B that has NOT NULL foreign key to C that has NOT NULL foreign key to A) either via schema where the foreign key is not also a primary key of the model with the belongs_to, or there is an ActiveRecord presence validation), there is a workaround. First, prepopulate one of the models involved in the interdependency chain in the database as part of test setup, or if the ids are NOT NULL but are not foreign key constrained (i.e. if you can enter an invalid ID into the foreign key column, which implies possible referential integrity issues) then you may be able to set them with an invalid id. Take that foreign id and then use the following to ensure that it will set that foreign id or instance. This is done at a global level which may not work for you, but it makes it convenient to put into your spec/spec_helper.rb, etc. For example, let's say your bar has NOT NULL on bartender_id and waiter_id, and in turn bartender and waiter both have a NOT NULL bar_id, and neither enforce foreign keys. Maybe you have preloaded data for waiter somehow as the id '123', but want to set bartender to just use an invalid id '-1', and you want to do it when they are on their second loop. You could use:
137
-
138
- Stepford::FactoryGirl.stop_circular_refs = {
139
- [:bartender, :bar] => {on_loop: 2, set_foreign_key_to: -1},
140
- [:waiter, :bar] => {on_loop: 2, set_to: Waiter.find(123)},
141
- }
142
-
143
- Leave out :on_loop or set :on_loop to 0 to use the set instead of attempting to build/create.
144
-
145
134
  ##### Debugging
146
135
 
147
136
  Add somewhere after the require:
@@ -154,11 +143,11 @@ Stepford has a CLI with a circular reference checker and a generator to automati
154
143
 
155
144
  ##### Refs
156
145
 
157
- Check ActiveRecord circular dependencies where the foreign key for a belongs_to is not also a primary key of the model, or there is an ActiveRecord presence validation keeping an association from being null:
146
+ Check ActiveRecord circular dependencies find circular chains of dependencies where foreign keys that are not primary keys of the models are all not nullable in the schema or not nullable because of ActiveRecord presence validation:
158
147
 
159
148
  bundle exec stepford circular
160
149
 
161
- Then it outputs the circular dependencies, e.g.:
150
+ Example of output:
162
151
 
163
152
  The following non-nullable foreign keys used in ActiveRecord model associations are involved in circular dependencies:
164
153
 
@@ -188,11 +177,11 @@ Then it outputs the circular dependencies, e.g.:
188
177
 
189
178
  ###### Creating Factories
190
179
 
191
- Autogenerate `test/factories.rb` from all model files in `app/models`:
180
+ To autogenerate `test/factories.rb` from all model files in `app/models`:
192
181
 
193
182
  bundle exec stepford factories
194
183
 
195
- If you want one file per model, use `--multiple`. The default path is `test/factories`, which it assumes exists. In that directory, it will create a factory file for each model. If you want separate factory files in `spec/factories`, you'd use:
184
+ If you want one file per model, specify `--multiple`. Use `--path` to specify the directory path or factories.rb pathname. The default path is `test/factories`, which it assumes exists. In that directory, it will create a factory file for each model. If you want separate factory files in `spec/factories`, you'd use:
196
185
 
197
186
  bundle exec stepford factories --path spec/factories --multiple
198
187
 
@@ -214,16 +203,6 @@ Specify `--models` and a comma-delimited list of models to only output the model
214
203
 
215
204
  bundle exec stepford factories --path spec/support/put_into_factories.rb --models foo,bar,foo_bar
216
205
 
217
- ###### Traits
218
-
219
- To generate traits for each attribute that would be included with `--attributes`, but isn't because `--attributes` is not specified:
220
-
221
- bundle exec stepford factories --attribute-traits
222
-
223
- To generate traits for each association that would be included with `--associations`, but isn't because `--associations` is not specified:
224
-
225
- bundle exec stepford factories --association-traits
226
-
227
206
  ###### Associations
228
207
 
229
208
  If you use Stepford::FactoryGirl (or deep_* methods in rspec) to automatically generate factories, you may not need to generate associations, because that sets them for you. If you do choose to use associations, note that these will likely create factories with interdependence issues. When there are NOT NULLs on foreign keys and/or presence validations, etc. you can't just use `after(:create)` or `after(:build)` to set associations, and without those you can have issues with "Trait not registered" or "Factory not registered". Later versions of FactoryGirl may make this easier, and be sure to see notes from Josh down in the troubleshooting section.
@@ -252,7 +231,17 @@ If associations are deemed broken, it will output proposed changes.
252
231
 
253
232
  If working with a legacy schema, you may have models with foreign_key columns that you don't have associations defined for in the model. If that is the case, we don't want to assign arbitrary integers to them and try to create a record. If that is the case, try `--exclude-all-ids`, which will exclude those ids as attributes defined in the factories and you can add associations as needed to get things working.
254
233
 
255
- ###### How NOT NULL, and Other Database Constraints and Active Record Validations Are Handled
234
+ ###### Traits
235
+
236
+ To generate traits for each attribute that would be included with `--attributes`, but isn't because `--attributes` is not specified:
237
+
238
+ bundle exec stepford factories --attribute-traits
239
+
240
+ To generate traits for each association that would be included with `--associations`, but isn't because `--associations` is not specified:
241
+
242
+ bundle exec stepford factories --association-traits
243
+
244
+ ###### Constraints and Validations
256
245
 
257
246
  If the ActiveRecord column `null` property for the attribute is true for the attribute or foreign key for the association, or if there is a presence validator for an attribute or foreign key for the association, then that attribute or association will be defined by the default factory.
258
247
 
@@ -295,18 +284,15 @@ Here are a few rspecs that test the FactoryGirl factories and the Stepford deep_
295
284
  end
296
285
  end
297
286
 
298
-
299
287
  ##### Troubleshooting
300
288
 
301
289
  If you have duplicate factory definitions during Rails load, it may complain. Just move, rename, or remove the offending files and factories and retry.
302
290
 
303
- The CLI produces factories that use Ruby 1.9 hash syntax. If you aren't using Ruby 1.9, it may not fail during generation, but it might later when loading the factories.
304
-
305
- If you are using STI, you'll need to manually fix the value that goes into the `type` attribute, or you can remove those.
291
+ The factories CLI produces factories that use Ruby 1.9 hash syntax. If you aren't using Ruby 1.9, it may not fail during generation, but it might later when loading the factories.
306
292
 
307
- Tested with postgreSQL 9.x only.
293
+ If you are using STI, you'll need to manually fix the value that goes into the `type` attribute.
308
294
 
309
- If you use Stepford to create factories for existing tests and the tests fail with:
295
+ If you use Stepford to create factories for existing tests and the tests fail with something like:
310
296
 
311
297
  ActiveRecord::StatementInvalid:
312
298
  PG::Error: ERROR: null value in column "something_id" violates not-null constraint
@@ -316,11 +302,17 @@ or maybe:
316
302
  ActiveRecord::RecordInvalid:
317
303
  Validation failed: Item The item is required., Pricer The pricer is required., Purchased by A purchaser is required.
318
304
 
319
- or you might get:
305
+ then try to use the deep_* methods to build or create.
306
+
307
+ If you get:
320
308
 
321
309
  SystemStackError:
322
310
  stack level too deep
323
311
 
312
+ then note that associations and traits can lead to circular dependencies. Trying generating factories without associations or traits (the default), and use the deep_* methods to create.
313
+
314
+ If you still see the 'stack level too deep' error, use the circular CLI to find interreferencing non-nullable foreign keys and fix them.
315
+
324
316
  ThoughtBot's Josh Clayton provided some suggestions for this, including using methods to generate more complex object structures:
325
317
 
326
318
  def post_containing_comment_by_author
@@ -1,3 +1,3 @@
1
1
  module Stepford
2
- VERSION = '0.12.0'
2
+ VERSION = '0.12.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stepford
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.12.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-12 00:00:00.000000000 Z
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor