class DutyFree::InstallGenerator

Auto-generates an IMPORT_TEMPLATE entry for one or more models

Public Class Methods

next_migration_number(dirname) click to toggle source
# File lib/generators/duty_free/install_generator.rb, line 27
def self.next_migration_number(dirname)
  ::ActiveRecord::Generators::Base.next_migration_number(dirname)
end

Public Instance Methods

create_migration_file() click to toggle source
# File lib/generators/duty_free/install_generator.rb, line 22
def create_migration_file
  add_duty_free_migration('create_versions')
  add_duty_free_migration('add_object_changes_to_versions') if options.with_changes?
end

Protected Instance Methods

add_duty_free_migration(template) click to toggle source
# File lib/generators/duty_free/install_generator.rb, line 33
def add_duty_free_migration(template)
  migration_dir = File.expand_path('db/migrate')
  if self.class.migration_exists?(migration_dir, template)
    ::Kernel.warn "Migration already exists: #{template}"
  else
    migration_template(
      "#{template}.rb.erb",
      "db/migrate/#{template}.rb",
      item_type_options: item_type_options,
      migration_version: migration_version,
      versions_table_options: versions_table_options
    )
  end
end

Private Instance Methods

item_type_options() click to toggle source

MySQL 5.6 utf8mb4 limit is 191 chars for keys used in indexes.

# File lib/generators/duty_free/install_generator.rb, line 51
def item_type_options
  opt = { null: false }
  opt[:limit] = 191 if mysql?
  ", #{opt}"
end
migration_version() click to toggle source
# File lib/generators/duty_free/install_generator.rb, line 57
def migration_version
  return unless (major = ActiveRecord::VERSION::MAJOR) >= 5

  "[#{major}.#{ActiveRecord::VERSION::MINOR}]"
end
mysql?() click to toggle source

Class names of MySQL adapters.

  • `MysqlAdapter` - Used by gems: `mysql`, `activerecord-jdbcmysql-adapter`.

  • `Mysql2Adapter` - Used by `mysql2` gem.

# File lib/generators/duty_free/install_generator.rb, line 66
def mysql?
  [
    'ActiveRecord::ConnectionAdapters::MysqlAdapter',
    'ActiveRecord::ConnectionAdapters::Mysql2Adapter'
  ].freeze.include?(ActiveRecord::Base.connection.class.name)
end
versions_table_options() click to toggle source

Even modern versions of MySQL still use `latin1` as the default character encoding. Many users are not aware of this, and run into trouble when they try to use DutyFree in apps that otherwise tend to use UTF-8. Postgres, by comparison, uses UTF-8 except in the unusual case where the OS is configured with a custom locale.

Furthermore, MySQL's original implementation of UTF-8 was flawed, and had to be fixed later by introducing a new charset, `utf8mb4`.

# File lib/generators/duty_free/install_generator.rb, line 88
def versions_table_options
  if mysql?
    ', { options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }'
  else
    ''
  end
end