Permalink
Browse files

version 0.3.0. support for excluding all ids and specifying model names

  • Loading branch information...
1 parent 9b6dfaa commit f542e85d2cd7aafe73359a904dc7ef8c8a070474 @garysweaver committed Nov 2, 2012
Showing with 27 additions and 12 deletions.
  1. +14 −4 README.md
  2. +5 −3 lib/stepford/cli.rb
  3. +7 −4 lib/stepford/factory_girl.rb
  4. +1 −1 lib/stepford/version.rb
View
@@ -37,7 +37,7 @@ Then run:
#### Factory Girl
-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 non-primary key, non-foreign key attributes only (no associations):
+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:
bundle exec stepford factories
@@ -57,10 +57,20 @@ To include associations:
### Stepford Checks Model Associations
-If `--associations` or `--validate_associations` is specified, Stepford first loads Rails and attempts to check your models for broken associations.
+If `--associations` or `--validate-associations` is specified, Stepford first loads Rails and attempts to check your models for broken associations.
If associations are deemed broken, it will output proposed changes.
+### No IDs
+
+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.
+
+### Specifying Models
+
+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:
+
+ bundle exec stepford factories --path spec/support/put_into_factories.rb --models foo,bar,foo_bar
+
### Troubleshooting
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 `
Tested with postgreSQL 9.x only.
-If you use stepford to create factories for existing tests and the tests fail with:
+If you use Stepford to create factories for existing tests and the tests fail with:
ActiveRecord::StatementInvalid:
PG::Error: ERROR: null value in column "something_id" violates not-null constraint
@@ -81,7 +91,7 @@ or maybe:
ActiveRecord::RecordInvalid:
Validation failed: Item The item is required., Pricer The pricer is required., Purchased by A purchaser is required.
-you might either need to modify those factories to set associations that are required or specify `--associations` in stepford to attempt generate them.
+you might either need to modify those factories to set associations that are required or specify `--associations` in Stepford to attempt generate them.
If you specify `--associations`, you might get circular associations and could easily end up with:
View
@@ -3,10 +3,12 @@
module Stepford
class CLI < Thor
desc "factories", "create FactoryGirl factories"
- method_option :single, :desc => "Put all factories into a single file"
+ method_option :single, :desc => "Put all factories into a single file", :type => :boolean
method_option :path, :desc => "Pathname of file to contain factories or path of directory to contain factory/factories"
- method_option :associations, :desc => "Include associations in factories"
- method_option :validate_associations, :desc => "Validate associations in factories even if not including associations"
+ method_option :associations, :desc => "Include associations in factories", :type => :boolean
+ method_option :validate_associations, :desc => "Validate associations in factories even if not including associations", :type => :boolean
+ method_option :exclude_all_ids, :desc => "Exclude attributes with names ending in _id even if they aren't foreign or primary keys", :type => :boolean
+ method_option :models, :desc => "A comma delimited list of only the models you want to include"
def factories()
# load Rails environment
require './config/environment'
@@ -6,17 +6,18 @@ def self.generate_factories(options={})
# guard against circular references
factories = {}
expected = {}
+ included_models = options[:models] ? options[:models].split(',').collect{|s|s.strip}.compact : nil
Dir[File.join('app','models','*.rb').to_s].each do |filename|
model_name = File.basename(filename).sub(/.rb$/, '')
+ next if included_models && !included_models.include?(model_name)
load File.join('app','models',"#{model_name}.rb")
model_class = model_name.camelize.constantize
next unless model_class.ancestors.include?(ActiveRecord::Base)
factory = (factories[model_name.to_sym] ||= [])
- primary_keys = Array.wrap(model_class.primary_key).collect{|pk|pk.to_sym}
- foreign_keys = []
+ excluded_attributes = Array.wrap(model_class.primary_key).collect{|pk|pk.to_sym} + [:updated_at, :created_at]
association_lines = model_class.reflections.collect {|association_name, reflection|
(expected[reflection.class_name.underscore.to_sym] ||= []) << model_name
- foreign_keys << reflection.foreign_key.to_sym
+ excluded_attributes << reflection.foreign_key.to_sym
assc_sym = reflection.name.to_sym
clas_sym = reflection.class_name.underscore.to_sym
# we have to do the part above to not set arbitrary values in foreign key attributes
@@ -32,7 +33,9 @@ def self.generate_factories(options={})
nil
end
}.compact.sort.each {|l|factory << l}
- model_class.columns.collect {|c| "#{c.name.to_sym} #{Stepford::Common.value_for(c)}" unless foreign_keys.include?(c.name.to_sym) || primary_keys.include?(c.name.to_sym)}.compact.sort.each {|l|factory << l}
+ model_class.columns.collect {|c|
+ "#{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])))
+ }.compact.sort.each {|l|factory << l}
end
if options[:associations] || options[:validate_associations]
View
@@ -1,3 +1,3 @@
module Stepford
- VERSION = '0.2.3'
+ VERSION = '0.3.0'
end

0 comments on commit f542e85

Please sign in to comment.