class FlattenRecord::Generators::MigrationGenerator

Migration generator that creates migration file from template

Public Class Methods

source_root() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 7
def self.source_root
  @source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

Public Instance Methods

generate_files() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 13
def generate_files
  return unless valid? 

  @table_name = klass.table_name
  if klass.table_exists?
    puts "Table already exists: #{@table_name}"
    diff_and_generate 
  else
    @table_columns = denormalized_columns
    migration_template('migration.erb', "db/migrate/create_table_#{@table_name}.rb")    
  end
end

Private Instance Methods

add_columns() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 87
def add_columns
  @add_columns ||= 
    denormalized_columns.inject([]) do |cols, col|
      if !column_names.include?(col.name)
        cols << col
      end
      cols
    end
end
add_columns_names() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 67
def add_columns_names
  add_columns.collect(&:name).join(', ')
end
any_diff?() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 63
def any_diff?
  !add_columns.empty? 
end
blue(text) click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 123
def blue(text); colorize(text, 34); end
colorize(text, color_code) click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 116
def colorize(text, color_code)
  "\e[#{color_code}m#{text}\e[0m"
end
column_names() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 83
def column_names
  columns.map(&:name)
end
columns() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 75
def columns
  klass.columns
end
denormalized_column_names() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 79
def denormalized_column_names
  denormalized_columns.map(&:name)
end
denormalized_columns() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 31
def denormalized_columns
  meta.all_columns
end
diff_and_generate() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 51
def diff_and_generate
  if any_diff?
    puts "Generating migration based on the difference.."
    puts "  #{yellow('Other columns(not defined):')} #{extra_columns_names}" unless extra_columns.empty?
    puts "  #{green('Add columns:')} #{add_columns_names}" unless add_columns.empty?

    @migration = "add_#{add_columns.first.name}_and_columns_to"
    
    migration_template('update.erb', "db/migrate/#{@migration}_#{@table_name}.rb")
  end
end
extra_columns() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 97
def extra_columns
  @extra_columns ||= 
    columns.inject([]) do |cols, col|
      if col.name != 'id' && !denormalized_column_names.include?(col.name)
        cols << col
      end
      cols
    end
end
extra_columns_names() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 71
def extra_columns_names
  extra_columns.collect(&:name).join(', ')
end
flatten_klass_names() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 47
def flatten_klass_names
  FlattenRecord::Config.included_models
end
green(text) click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 121
def green(text); colorize(text, 32); end
klass() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 39
def klass
  klass_name.constantize
end
klass_name() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 35
def klass_name
  name.camelize
end
meta() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 27
def meta
  klass.flattener_meta
end
red(text) click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 120
def red(text); colorize(text, 31); end
table_name() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 43
def table_name
  klass.table_name
end
valid?() click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 107
def valid? 
  begin 
    klass && meta
  rescue Exception => e
    puts red(e.message)
    false
  end
end
yellow(text) click to toggle source
# File lib/generators/flatten_record/migration/migration_generator.rb, line 122
def yellow(text); colorize(text, 33); end