stepford 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -4
- data/lib/stepford/cli.rb +5 -3
- data/lib/stepford/factory_girl.rb +7 -4
- data/lib/stepford/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -37,7 +37,7 @@ Then run:
|
|
37
37
|
|
38
38
|
#### Factory Girl
|
39
39
|
|
40
|
-
The default will assume a `test/factories` directory exists. In that directory, it will create a factory file for each model containing example values for
|
40
|
+
The default will assume a `test/factories` directory exists. In that directory, it will create a factory file for each model containing example values for all attributes except primary keys, foreign keys, created_at, and updated_at:
|
41
41
|
|
42
42
|
bundle exec stepford factories
|
43
43
|
|
@@ -57,10 +57,20 @@ To include associations:
|
|
57
57
|
|
58
58
|
### Stepford Checks Model Associations
|
59
59
|
|
60
|
-
If `--associations` or `--
|
60
|
+
If `--associations` or `--validate-associations` is specified, Stepford first loads Rails and attempts to check your models for broken associations.
|
61
61
|
|
62
62
|
If associations are deemed broken, it will output proposed changes.
|
63
63
|
|
64
|
+
### No IDs
|
65
|
+
|
66
|
+
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.
|
67
|
+
|
68
|
+
### Specifying Models
|
69
|
+
|
70
|
+
Specify `--models` and a comma-delimited list of models to only output the models you specify. If you don't want to overwrite existing factory files, you should direct the output to another file and manually copy each in:
|
71
|
+
|
72
|
+
bundle exec stepford factories --path spec/support/put_into_factories.rb --models foo,bar,foo_bar
|
73
|
+
|
64
74
|
### Troubleshooting
|
65
75
|
|
66
76
|
If you have duplicate factory definitions during Rails load, it may complain. Just move, rename, or remove the offending files and factories and retry.
|
@@ -71,7 +81,7 @@ If you are using STI, you'll need to manually fix the value that goes into the `
|
|
71
81
|
|
72
82
|
Tested with postgreSQL 9.x only.
|
73
83
|
|
74
|
-
If you use
|
84
|
+
If you use Stepford to create factories for existing tests and the tests fail with:
|
75
85
|
|
76
86
|
ActiveRecord::StatementInvalid:
|
77
87
|
PG::Error: ERROR: null value in column "something_id" violates not-null constraint
|
@@ -81,7 +91,7 @@ or maybe:
|
|
81
91
|
ActiveRecord::RecordInvalid:
|
82
92
|
Validation failed: Item The item is required., Pricer The pricer is required., Purchased by A purchaser is required.
|
83
93
|
|
84
|
-
you might either need to modify those factories to set associations that are required or specify `--associations` in
|
94
|
+
you might either need to modify those factories to set associations that are required or specify `--associations` in Stepford to attempt generate them.
|
85
95
|
|
86
96
|
If you specify `--associations`, you might get circular associations and could easily end up with:
|
87
97
|
|
data/lib/stepford/cli.rb
CHANGED
@@ -3,10 +3,12 @@ require 'thor'
|
|
3
3
|
module Stepford
|
4
4
|
class CLI < Thor
|
5
5
|
desc "factories", "create FactoryGirl factories"
|
6
|
-
method_option :single, :desc => "Put all factories into a single file"
|
6
|
+
method_option :single, :desc => "Put all factories into a single file", :type => :boolean
|
7
7
|
method_option :path, :desc => "Pathname of file to contain factories or path of directory to contain factory/factories"
|
8
|
-
method_option :associations, :desc => "Include associations in factories"
|
9
|
-
method_option :validate_associations, :desc => "Validate associations in factories even if not including associations"
|
8
|
+
method_option :associations, :desc => "Include associations in factories", :type => :boolean
|
9
|
+
method_option :validate_associations, :desc => "Validate associations in factories even if not including associations", :type => :boolean
|
10
|
+
method_option :exclude_all_ids, :desc => "Exclude attributes with names ending in _id even if they aren't foreign or primary keys", :type => :boolean
|
11
|
+
method_option :models, :desc => "A comma delimited list of only the models you want to include"
|
10
12
|
def factories()
|
11
13
|
# load Rails environment
|
12
14
|
require './config/environment'
|
@@ -6,17 +6,18 @@ module Stepford
|
|
6
6
|
# guard against circular references
|
7
7
|
factories = {}
|
8
8
|
expected = {}
|
9
|
+
included_models = options[:models] ? options[:models].split(',').collect{|s|s.strip}.compact : nil
|
9
10
|
Dir[File.join('app','models','*.rb').to_s].each do |filename|
|
10
11
|
model_name = File.basename(filename).sub(/.rb$/, '')
|
12
|
+
next if included_models && !included_models.include?(model_name)
|
11
13
|
load File.join('app','models',"#{model_name}.rb")
|
12
14
|
model_class = model_name.camelize.constantize
|
13
15
|
next unless model_class.ancestors.include?(ActiveRecord::Base)
|
14
16
|
factory = (factories[model_name.to_sym] ||= [])
|
15
|
-
|
16
|
-
foreign_keys = []
|
17
|
+
excluded_attributes = Array.wrap(model_class.primary_key).collect{|pk|pk.to_sym} + [:updated_at, :created_at]
|
17
18
|
association_lines = model_class.reflections.collect {|association_name, reflection|
|
18
19
|
(expected[reflection.class_name.underscore.to_sym] ||= []) << model_name
|
19
|
-
|
20
|
+
excluded_attributes << reflection.foreign_key.to_sym
|
20
21
|
assc_sym = reflection.name.to_sym
|
21
22
|
clas_sym = reflection.class_name.underscore.to_sym
|
22
23
|
# we have to do the part above to not set arbitrary values in foreign key attributes
|
@@ -32,7 +33,9 @@ module Stepford
|
|
32
33
|
nil
|
33
34
|
end
|
34
35
|
}.compact.sort.each {|l|factory << l}
|
35
|
-
model_class.columns.collect {|c|
|
36
|
+
model_class.columns.collect {|c|
|
37
|
+
"#{c.name.to_sym} #{Stepford::Common.value_for(c)}" unless (excluded_attributes.include?(c.name.to_sym) || ((c.name.downcase.end_with?('_id') && options[:exclude_all_ids])))
|
38
|
+
}.compact.sort.each {|l|factory << l}
|
36
39
|
end
|
37
40
|
|
38
41
|
if options[:associations] || options[:validate_associations]
|
data/lib/stepford/version.rb
CHANGED