Sha256: 7b8c74bb56d18e5d6228341c455dfdf35f3e02e4566f78a3211547593d5e4e9d

Contents?: true

Size: 1.44 KB

Versions: 108

Compression:

Stored size: 1.44 KB

Contents

require 'rails/generators/active_record'

module ActiveRecord
  module Generators
    class ModelGenerator < Base
      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?
        migration_template "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.has_index? || (a.reference? && options[:indexes]) }
      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

108 entries across 85 versions & 13 rubygems

Version Path
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/rails/generators/active_record/model/model_generator.rb
activerecord-3.2.13 lib/rails/generators/active_record/model/model_generator.rb
activerecord-3.2.13.rc2 lib/rails/generators/active_record/model/model_generator.rb
activerecord-3.2.13.rc1 lib/rails/generators/active_record/model/model_generator.rb
active_mailer-0.0.5 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/rails/generators/active_record/model/model_generator.rb
active_mailer-0.0.4 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/rails/generators/active_record/model/model_generator.rb
active_mailer-0.0.3 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/rails/generators/active_record/model/model_generator.rb
activerecord-3.2.12 lib/rails/generators/active_record/model/model_generator.rb
challah-0.9.1.beta.3 vendor/bundle/gems/activerecord-3.2.11/lib/rails/generators/active_record/model/model_generator.rb
devise_sociable-0.1.0 vendor/bundle/gems/activerecord-3.2.11/lib/rails/generators/active_record/model/model_generator.rb
challah-0.9.1.beta vendor/bundle/gems/activerecord-3.2.11/lib/rails/generators/active_record/model/model_generator.rb
activerecord-3.2.11 lib/rails/generators/active_record/model/model_generator.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.10/lib/rails/generators/active_record/model/model_generator.rb
challah-0.9.0 vendor/bundle/gems/activerecord-3.2.9/lib/rails/generators/active_record/model/model_generator.rb
activerecord-3.2.10 lib/rails/generators/active_record/model/model_generator.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/activerecord-3.2.8/lib/rails/generators/active_record/model/model_generator.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/activerecord-3.2.7/lib/rails/generators/active_record/model/model_generator.rb
challah-rolls-0.2.0 vendor/bundle/gems/activerecord-3.2.9/lib/rails/generators/active_record/model/model_generator.rb
challah-rolls-0.2.0 vendor/bundle/gems/activerecord-3.2.8/lib/rails/generators/active_record/model/model_generator.rb
challah-rolls-0.2.0 vendor/bundle/gems/activerecord-3.2.7/lib/rails/generators/active_record/model/model_generator.rb