Sha256: bee826332334713133b5a4b697f95e2ac8abe9251c7a0c66acc093a8a55054ac

Contents?: true

Size: 1.74 KB

Versions: 33

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require "rails/generators/active_record"

module ActiveRecord
  module Generators # :nodoc:
    class ModelGenerator < Base # :nodoc:
      argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"

      check_class_collision

      class_option :migration, type: :boolean
      class_option :timestamps, type: :boolean
      class_option :parent, type: :string, desc: "The parent class for the generated model"
      class_option :indexes, type: :boolean, default: true, desc: "Add indexes for references and belongs_to columns"
      class_option :primary_key_type, type: :string, desc: "The type for primary key"

      # creates the migration file for the model.
      def create_migration_file
        return unless options[:migration] && options[:parent].nil?
        attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false
        migration_template "../../migration/templates/create_table_migration.rb", File.join(db_migrate_path, "create_#{table_name}.rb")
      end

      def create_model_file
        template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
      end

      def create_module_file
        return if regular_class_path.empty?
        template "module.rb", File.join("app/models", "#{class_path.join('/')}.rb") if behavior == :invoke
      end

      hook_for :test_framework

      private

        def attributes_with_index
          attributes.select { |a| !a.reference? && a.has_index? }
        end

        # Used by the migration template to determine the parent name of the model
        def parent_class_name
          options[:parent] || "ApplicationRecord"
        end
    end
  end
end

Version data entries

33 entries across 33 versions & 3 rubygems

Version Path
activerecord-5.2.8.1 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.8 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.7.1 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.7 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.6.3 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.6.2 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.6.1 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.6 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.4.6 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.5 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.4.5 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.4.4 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.4.3 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.4.2 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.4.1 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.4 lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.4.rc1 lib/rails/generators/active_record/model/model_generator.rb
spiral_form-0.1.1 vendor/bundle/gems/activerecord-5.2.3/lib/rails/generators/active_record/model/model_generator.rb
spiral_form-0.1.0 vendor/bundle/gems/activerecord-5.2.3/lib/rails/generators/active_record/model/model_generator.rb
activerecord-5.2.3 lib/rails/generators/active_record/model/model_generator.rb