Skip to content

Instantly share code, notes, and snippets.

@Martin91
Last active March 2, 2017 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Martin91/5bc1f1df8549279fde083b1bfcc42bd3 to your computer and use it in GitHub Desktop.
Save Martin91/5bc1f1df8549279fde083b1bfcc42bd3 to your computer and use it in GitHub Desktop.
FactoryGirl model generator to support automatically add columns
# generate factory with columns for model
#
# $ bundle exec rails g factory_girl:model User
require 'factory_girl'
require 'generators/factory_girl/model/model_generator'
FactoryGirl::Generators::ModelGenerator.class_eval do
alias :command_line_attributes :attributes
def attributes
if command_line_attributes.present?
command_line_attributes
else
# TODO: handle the situation when the model or corresponding table doesn't exist yet
columns = name.constantize.columns
columns.map do |column|
"#{column.name}:#{column.type}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment