Sha256: d2102e448572b8d0773c8c4ae66f5b3c2fe78e13873cbccb18dc4b6ce5f733be

Contents?: true

Size: 1.59 KB

Versions: 29

Compression:

Stored size: 1.59 KB

Contents

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"

      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", "db/migrate/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

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

      def accessible_attributes
        attributes.reject(&:reference?)
      end

      hook_for :test_framework

      protected

        def parent_class_name
          options[:parent] || "ActiveRecord::Base"
        end

    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
activerecord-4.0.13 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.13.rc1 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.11.1 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.12 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.11 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.10 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.10.rc2 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.10.rc1 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.9 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.8 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.7 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.6 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.6.rc3 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.6.rc2 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.6.rc1 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.5 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.4 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.4.rc1 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.3 lib/rails/generators/active_record/model/model_generator.rb
activerecord-4.0.2 lib/rails/generators/active_record/model/model_generator.rb