Sha256: 1ccc4cec7734f77ab9a28a4ed26c59b700dad78d0e5eed3852367b55e72efbab

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

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

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

    source_root File.expand_path("../templates", __FILE__)
    class_option(
      :with_changes,
      type: :boolean,
      default: false,
      desc: "Store changeset (diff) with each version"
    )
    class_option(
      :with_associations,
      type: :boolean,
      default: false,
      desc: "Store transactional IDs to support association restoration"
    )

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

    def create_migration_file
      add_paper_trail_migration("create_versions")
      add_paper_trail_migration("add_object_changes_to_versions") if options.with_changes?
      if options.with_associations?
        add_paper_trail_migration("create_version_associations")
        add_paper_trail_migration("add_transaction_id_column_to_versions")
      end
    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", "db/migrate/#{template}.rb"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
paper_trail-5.2.0 lib/generators/paper_trail/install_generator.rb
paper_trail-5.1.1 lib/generators/paper_trail/install_generator.rb
paper_trail-5.1.0 lib/generators/paper_trail/install_generator.rb
paper_trail-5.0.1 lib/generators/paper_trail/install_generator.rb