stepford 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +39 -25
- data/lib/stepford/cli.rb +2 -2
- data/lib/stepford/factory_girl_generator.rb +5 -5
- data/lib/stepford/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -99,9 +99,7 @@ It works just about the same as `Stepford::FactoryGirl` except it is called `Ste
|
|
99
99
|
|
100
100
|
##### RSpec Helpers
|
101
101
|
|
102
|
-
|
103
|
-
|
104
|
-
Instead put this in your `spec/spec_helper.rb`:
|
102
|
+
Put this in your `spec/spec_helper.rb`:
|
105
103
|
|
106
104
|
require 'stepford/factory_girl_rspec_helpers'
|
107
105
|
|
@@ -117,30 +115,38 @@ Then you can just use `cache_create`, `cache_create_list`, `cache_build`, `cache
|
|
117
115
|
|
118
116
|
cache_create(:foo)
|
119
117
|
|
120
|
-
####
|
118
|
+
#### CLI
|
121
119
|
|
122
|
-
|
120
|
+
Stepford has a CLI to automatically create your factories file(s).
|
123
121
|
|
124
|
-
|
122
|
+
##### Creating Factories
|
125
123
|
|
126
|
-
|
124
|
+
Autogenerate `test/factories.rb` from all model files in `app/models`:
|
127
125
|
|
128
|
-
|
126
|
+
bundle exec stepford factories
|
129
127
|
|
130
|
-
|
128
|
+
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:
|
131
129
|
|
132
|
-
|
130
|
+
bundle exec stepford factories --path spec/factories --multiple
|
133
131
|
|
134
|
-
|
132
|
+
##### RSpec
|
135
133
|
|
136
134
|
To put all of your factories into `spec/factories.rb`:
|
137
135
|
|
138
|
-
bundle exec stepford factories --
|
136
|
+
bundle exec stepford factories --path spec
|
139
137
|
|
140
|
-
|
138
|
+
This also works:
|
141
139
|
|
142
140
|
bundle exec stepford factories --path spec/support/factories.rb
|
143
141
|
|
142
|
+
##### Specifying Models
|
143
|
+
|
144
|
+
By default, Stepford processes all models found in `app/models`.
|
145
|
+
|
146
|
+
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:
|
147
|
+
|
148
|
+
bundle exec stepford factories --path spec/support/put_into_factories.rb --models foo,bar,foo_bar
|
149
|
+
|
144
150
|
##### Traits
|
145
151
|
|
146
152
|
To generate traits for each attribute that would be included with `--attributes`, but isn't because `--attributes` is not specified:
|
@@ -153,27 +159,29 @@ To generate traits for each association that would be included with `--associati
|
|
153
159
|
|
154
160
|
##### Associations
|
155
161
|
|
162
|
+
Associations in FactoryGirl aren't that great. There are factory interdependence issues (one factory requires another that requires it, etc.- that doesn't work that well) when you don't use `after(:create)` or `after(:build)`, and those don't work if you presence validate the associations or their foreign_keys have NOT NULL.
|
163
|
+
|
164
|
+
However, if you don't have anything that complex or don't mind hand-editing the factories to try to fix issues, go for it. Here are the related methods.
|
165
|
+
|
166
|
+
###### Include All Associations
|
167
|
+
|
156
168
|
To include all associations even if they aren't deemed to be required by not null ActiveRecord constraints defined in the model:
|
157
169
|
|
158
170
|
bundle exec stepford factories --associations
|
159
171
|
|
160
|
-
|
172
|
+
###### Stepford Checks Model Associations
|
161
173
|
|
162
174
|
If `--associations` or `--validate-associations` is specified, Stepford first loads Rails and attempts to check your models for broken associations.
|
163
175
|
|
164
176
|
If associations are deemed broken, it will output proposed changes.
|
165
177
|
|
166
|
-
|
167
|
-
|
168
|
-
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.
|
169
|
-
|
170
|
-
##### Ignored Required Assocations
|
178
|
+
###### Include Required Assocations
|
171
179
|
|
172
|
-
With `--
|
180
|
+
With `--include-required-associations` it will include NOT NULL foreign key associations or presence validated associations.
|
173
181
|
|
174
|
-
|
182
|
+
###### Cache Associations
|
175
183
|
|
176
|
-
Use `--cache-associations`
|
184
|
+
Use `--cache-associations` will use the .
|
177
185
|
|
178
186
|
This uses the [factory_girl-cache][factory_girl-cache] gem in the autogenerated factories, so you will need to include it also in your Gemfile:
|
179
187
|
|
@@ -183,11 +191,17 @@ and
|
|
183
191
|
|
184
192
|
bundle install
|
185
193
|
|
186
|
-
#####
|
194
|
+
##### No IDs
|
187
195
|
|
188
|
-
|
196
|
+
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.
|
189
197
|
|
190
|
-
|
198
|
+
##### How NOT NULL, and Other Database Constraints and Active Record Validations Are Handled
|
199
|
+
|
200
|
+
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.
|
201
|
+
|
202
|
+
Currently uniqueness constraints are ignored and must be handled by FactoryGirl sequence or similar if not automatically populated by your model or the database, e.g. in your factory, if username uniqueness is enforced by a unique constraint on the database-side, you'll need to do something like this manually in the factory:
|
203
|
+
|
204
|
+
sequence(:username) {|n| "user#{n}" }
|
191
205
|
|
192
206
|
##### Testing Factories
|
193
207
|
|
data/lib/stepford/cli.rb
CHANGED
@@ -3,7 +3,7 @@ require 'thor'
|
|
3
3
|
module Stepford
|
4
4
|
class CLI < Thor
|
5
5
|
desc "factories", "create FactoryGirl factories"
|
6
|
-
method_option :
|
6
|
+
method_option :multiple, :desc => "Put each model's factory 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
8
|
method_option :associations, :desc => "Include all associations in factories, not just those that are required due to ActiveRecord presence validation or column not null restriction", :type => :boolean
|
9
9
|
method_option :validate_associations, :desc => "Validate associations in factories even if not including associations", :type => :boolean
|
@@ -13,7 +13,7 @@ module Stepford
|
|
13
13
|
method_option :attribute_traits, :desc => "Include traits for attributes that would be output with --attributes that wouldn't be otherwise when --attributes is not specified", :type => :boolean
|
14
14
|
method_option :association_traits, :desc => "Include traits for associations that would be output with --associations that wouldn't be otherwise when --associations is not specified", :type => :boolean
|
15
15
|
method_option :cache_associations, :desc => "Use singleton values to avoid 'stack level too deep' circular reference(s)", :type => :boolean
|
16
|
-
method_option :
|
16
|
+
method_option :include_required_associations, :desc => "Include NOT NULL foreign key associations or presence validated associations by default", :type => :boolean
|
17
17
|
def factories()
|
18
18
|
# load Rails environment
|
19
19
|
require './config/environment'
|
@@ -22,8 +22,8 @@ module Stepford
|
|
22
22
|
# if has a foreign key, then if NOT NULL or is a presence validate, the association is required and should be output. unfortunately this could mean a circular reference that will have to be manually fixed
|
23
23
|
has_presence_validator = model_class.validators_on(assc_sym).collect{|v|v.class}.include?(ActiveModel::Validations::PresenceValidator)
|
24
24
|
required = reflection.foreign_key ? (has_presence_validator || model_class.columns.any?{|c| !c.null && c.name.to_sym == reflection.foreign_key.to_sym}) : false
|
25
|
-
should_be_trait = !(options[:associations] || (
|
26
|
-
if options[:associations] || (
|
25
|
+
should_be_trait = !(options[:associations] || (options[:include_required_associations] && required)) && options[:association_traits]
|
26
|
+
if options[:associations] || (options[:include_required_associations] && required) || should_be_trait
|
27
27
|
if options[:cache_associations]
|
28
28
|
if reflection.macro == :has_many
|
29
29
|
is_default_plural_association_name = (clas_sym.to_s.pluralize.to_sym == assc_sym)
|
@@ -133,10 +133,10 @@ module Stepford
|
|
133
133
|
if options[:path].end_with?('.rb')
|
134
134
|
path = options[:path]
|
135
135
|
else
|
136
|
-
if options[:
|
137
|
-
path = File.join(options[:path],'factories.rb')
|
138
|
-
else
|
136
|
+
if options[:multiple]
|
139
137
|
path = options[:path]
|
138
|
+
else
|
139
|
+
path = File.join(options[:path],'factories.rb')
|
140
140
|
end
|
141
141
|
end
|
142
142
|
end
|
data/lib/stepford/version.rb
CHANGED