Skip to content

Instantly share code, notes, and snippets.

@slawosz
Created June 9, 2010 09:04
Show Gist options
  • Save slawosz/431249 to your computer and use it in GitHub Desktop.
Save slawosz/431249 to your computer and use it in GitHub Desktop.
class SimpleFactoryBuilder
def self.build_factories( klass, field_for_factory_name, *excluded )
attributes = klass.new.attributes
excluded.each { |e| attributes.symbolize_keys!.delete(e.to_sym) }
klass.all.each do |row|
attributes_string = ""
attributes.each_key do |key|
attributes_string << factory_attribute_row(row, key)
end
factory = <<-STRING
Factory.define #{':' + (klass.to_s.underscore + '_' + row[field_for_factory_name.to_s.to_sym].to_s).to_s.downcase}, :class => #{klass} do |f|
#{attributes_string}end
STRING
print factory
end
end
private
def self.factory_attribute_row(row, key)
value = row[key.to_sym]
if value.is_a? String
value = '"' + value + '"'
end
return " f.#{key.to_s} #{value}\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment