Sha256: 1147aab5c0b69a25c329ea71815787ec5ea34d97bddf2389c36d97c3fe26ddd9

Contents?: true

Size: 1.33 KB

Versions: 10

Compression:

Stored size: 1.33 KB

Contents

require 'closure_tree/active_record_support'
require 'forwardable'
require 'rails/generators/active_record'
require 'rails/generators/named_base'

module ClosureTree
  module Generators # :nodoc:
    class MigrationGenerator < Rails::Generators::NamedBase # :nodoc:
      include Rails::Generators::Migration
      include ClosureTree::ActiveRecordSupport
      extend Forwardable
      def_delegators :ct, :hierarchy_table_name, :primary_key_type

      def self.default_generator_root
        File.dirname(__FILE__)
      end

      def create_migration_file
        migration_template 'create_hierarchies_table.rb.erb', "db/migrate/create_#{migration_name}.rb"
      end

      private

      def migration_name
        remove_prefix_and_suffix(ct.hierarchy_table_name)
      end

      def migration_class_name
        "Create#{migration_name.camelize}"
      end

      def target_class
        @target_class ||= class_name.constantize
      end

      def ct
        @ct ||= if target_class.respond_to?(:_ct)
          target_class._ct
        else
          fail "Please RTFM and add the `has_closure_tree` (or `acts_as_tree`) annotation to #{class_name} before creating the migration."
        end
      end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
closure_tree-6.6.0 lib/generators/closure_tree/migration_generator.rb
closure_tree-6.5.0 lib/generators/closure_tree/migration_generator.rb
closure_tree-6.4.0 lib/generators/closure_tree/migration_generator.rb
closure_tree-6.3.0 lib/generators/closure_tree/migration_generator.rb
closure_tree-6.2.0 lib/generators/closure_tree/migration_generator.rb
closure_tree-6.1.0 lib/generators/closure_tree/migration_generator.rb
closure_tree-6.0.0 lib/generators/closure_tree/migration_generator.rb
closure_tree-6.0.0.gamma lib/generators/closure_tree/migration_generator.rb
closure_tree-6.0.0.alpha lib/generators/closure_tree/migration_generator.rb
closure_tree-5.2.0 lib/generators/closure_tree/migration_generator.rb