Last active
March 2, 2017 03:08
-
-
Save Martin91/5bc1f1df8549279fde083b1bfcc42bd3 to your computer and use it in GitHub Desktop.
FactoryGirl model generator to support automatically add columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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