class Generator

Attributes

force[RW]
rails_root[RW]
single[RW]

Public Class Methods

new(rails_root, single, force) click to toggle source
# File lib/generator.rb, line 5
def initialize(rails_root, single, force)
  @rails_root = rails_root
  @single = single
  @force = force
end

Public Instance Methods

create_factories() click to toggle source
# File lib/generator.rb, line 11
def create_factories
  @rails_root = rails_root
  if !@single
    create_multiple
  else
    create_single
  end
end
create_multiple() click to toggle source
# File lib/generator.rb, line 55
def create_multiple
  if !directory?("spec/factories")
    FileUtils.mkdir_p("#{@rails_root}/spec/factories")
  end
  library = Library.new
  ActiveRecord::Base.connection.tables.each do |table_name|
    next if table_name == 'schema_migrations'
    next if file?("spec/factories/#{table_name}.rb") and !@force
    file = File.join(@rails_root, 'spec', 'factories', "#{table_name}.rb")
    File.open(file, 'w') do |f|
      f.puts "require 'factory_girl'"
      f.puts "\n"
      f.puts "FactoryGirl.define do"
      p "Creating factory #{table_name}"
      f.puts "\n"
      f.puts "  factory :#{table_name} do"
      ActiveRecord::Base.connection.columns(table_name).each do |c| 
        f.puts "    #{c.name} #{library.check(c.name, c.type, c.limit)}"
      end
      f.puts "  end"
      f.puts "\n"
      f.puts "end"
    end
  end

end
create_single() click to toggle source
# File lib/generator.rb, line 32
def create_single
  library = Library.new
  return if file?("spec/factories.rb") and !@force
  file = File.join(@rails_root, 'spec', 'factories.rb')
  File.open(file, 'w') do |f|
    f.puts "require 'factory_girl'"
    f.puts "\n"
    f.puts "FactoryGirl.define do"
    ActiveRecord::Base.connection.tables.each do |table_name|
      next if table_name == 'schema_migrations'
      p "Creating factory #{table_name}"
      f.puts "\n"
      f.puts "  factory :#{table_name} do"
      ActiveRecord::Base.connection.columns(table_name).each do |c| 
        f.puts "    #{c.name} #{library.check(c.name, c.type, c.limit)}"
      end
      f.puts "  end"
    end
    f.puts "\n"
    f.puts "end"
  end
end
directory?(path) click to toggle source
# File lib/generator.rb, line 24
def directory?(path)
  File.directory?("#{@rails_root}/#{path}")
end
file?(path) click to toggle source
# File lib/generator.rb, line 20
def file?(path)
  File.exists?("#{@rails_root}/#{path}")
end
to_boolean(str) click to toggle source
# File lib/generator.rb, line 28
def to_boolean(str)
  str == "true"
end