Sha256: 34fe7d718f13cbc9a95b29495c6f09ee83ffd66c49d651b6b55f82c045d2f09c

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require "rails/generators"
require "rails/generators/active_record"

module PaperTrailAssociationTracking
  # Installs PaperTrail in a rails app.
  class InstallGenerator < ::Rails::Generators::Base
    include ::Rails::Generators::Migration

    # Class names of MySQL adapters.
    # - `MysqlAdapter` - Used by gems: `mysql`, `activerecord-jdbcmysql-adapter`.
    # - `Mysql2Adapter` - Used by `mysql2` gem.
    MYSQL_ADAPTERS = [
      "ActiveRecord::ConnectionAdapters::MysqlAdapter",
      "ActiveRecord::ConnectionAdapters::Mysql2Adapter"
    ].freeze

    source_root File.expand_path("../templates", __FILE__)

    desc "Generates (but does not run) a migration to add a versions table."

    def create_migrations
      add_paper_trail_migration("create_version_associations")
      add_paper_trail_migration("add_transaction_id_column_to_versions")
    end

    def create_initializer
      create_file(
        "config/initializers/paper_trail.rb",
        "PaperTrail.config.track_associations = #{!!options.with_associations?}\n",
        "PaperTrail.config.association_reify_error_behaviour = :error"
      )
    end

    def self.next_migration_number(dirname)
      ::ActiveRecord::Generators::Base.next_migration_number(dirname)
    end

    protected

    def add_paper_trail_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",
          migration_version: migration_version
        )
      end
    end

    private

    def migration_version
      major = ActiveRecord::VERSION::MAJOR
      if major >= 5
        "[#{major}.#{ActiveRecord::VERSION::MINOR}]"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
paper_trail-association_tracking-2.0.0 lib/generators/paper_trail_association_tracking/install_generator.rb
paper_trail-association_tracking-1.1.1 lib/generators/paper_trail_association_tracking/install_generator.rb
paper_trail-association_tracking-1.1.0 lib/generators/paper_trail_association_tracking/install_generator.rb
paper_trail-association_tracking-1.0.0 lib/generators/paper_trail_association_tracking/install_generator.rb
paper_trail-association_tracking-0.0.1 lib/generators/paper_trail_association_tracking/install_generator.rb