class PaperTrail::InstallGenerator

Installs PaperTrail in a rails app.

Constants

MYSQL_ADAPTERS

Class names of MySQL adapters.

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

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

Public Instance Methods

create_migration_file() click to toggle source
# File lib/generators/paper_trail/install/install_generator.rb, line 33
def create_migration_file
  add_paper_trail_migration(
    "create_versions",
    item_type_options: item_type_options,
    versions_table_options: versions_table_options,
    item_id_type_options: item_id_type_options,
    version_table_primary_key_type: version_table_primary_key_type
  )
  if options.with_changes?
    add_paper_trail_migration("add_object_changes_to_versions")
  end
end

Private Instance Methods

item_id_type_options() click to toggle source

To use uuid instead of integer for primary key

# File lib/generators/paper_trail/install/install_generator.rb, line 49
def item_id_type_options
  options.uuid? ? "string" : "bigint"
end
item_type_options() click to toggle source

MySQL 5.6 utf8mb4 limit is 191 chars for keys used in indexes. See github.com/paper-trail-gem/paper_trail/issues/651

# File lib/generators/paper_trail/install/install_generator.rb, line 64
def item_type_options
  if mysql?
    ", null: false, limit: 191"
  else
    ", null: false"
  end
end
mysql?() click to toggle source
# File lib/generators/paper_trail/install/install_generator.rb, line 72
def mysql?
  MYSQL_ADAPTERS.include?(ActiveRecord::Base.connection.class.name)
end
version_table_primary_key_type() click to toggle source

To use uuid for version table primary key

# File lib/generators/paper_trail/install/install_generator.rb, line 54
def version_table_primary_key_type
  if options.uuid?
    ", id: :uuid"
  else
    ""
  end
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 PaperTrail 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/paper_trail/install/install_generator.rb, line 91
def versions_table_options
  if mysql?
    ', options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci"'
  else
    ""
  end
end