File: lib/generators/extension_model/extension_model_generator.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: ExtensionModelGenerator#4
inherits from
  ModelGenerator   
has properties
attribute: extension_name [RW] #6
method: initialize / 2 #9
method: manifest #15
method: rspec_manifest #23
method: banner #46
method: extension_path #50
method: destination_root #54
method: extension_uses_rspec? #58
method: add_options! / 1 #62

Class Hierarchy

Code

   1  require 'rails_generator/base'
   2  require 'rails_generator/generators/components/model/model_generator'
   3 
   4  class ExtensionModelGenerator < ModelGenerator
   5    
   6    attr_accessor :extension_name
   7    default_options :with_test_unit => false
   8    
   9    def initialize(runtime_args, runtime_options = {})
  10      runtime_args = runtime_args.dup
  11      @extension_name = runtime_args.shift
  12      super(runtime_args, runtime_options)
  13    end
  14    
  15    def manifest
  16      if extension_uses_rspec?
  17        rspec_manifest
  18      else
  19        super
  20      end
  21    end
  22    
  23    def rspec_manifest
  24      record do |m|
  25        # Check for class naming collisions.
  26        m.class_collisions class_path, class_name
  27 
  28        # Model, spec, and fixture directories.
  29        m.directory File.join('app/models', class_path)
  30        m.directory File.join('spec/models', class_path)
  31        # m.directory File.join('spec/fixtures', class_path)
  32 
  33        # Model class, spec and fixtures.
  34        m.template 'model:model.rb',      File.join('app/models', class_path, "#{file_name}.rb")
  35        # m.template 'model:fixtures.yml',  File.join('spec/fixtures', class_path, "#{table_name}.yml")
  36        m.template 'model_spec.rb',       File.join('spec/models', class_path, "#{file_name}_spec.rb")
  37 
  38        unless options[:skip_migration]
  39          m.migration_template 'model:migration.rb', 'db/migrate', :assigns => {
  40            :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
  41          }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
  42        end
  43      end
  44    end
  45    
  46    def banner
  47      "Usage: #{$0} extension_model ExtensionName ModelName [field:type, field:type]"
  48    end
  49    
  50    def extension_path
  51      File.join('vendor', 'extensions', @extension_name.underscore)
  52    end
  53    
  54    def destination_root
  55      File.join(RAILS_ROOT, extension_path)
  56    end
  57    
  58    def extension_uses_rspec?
  59      File.exists?(File.join(destination_root, 'spec')) && !options[:with_test_unit]
  60    end
  61    
  62    def add_options!(opt)
  63      opt.separator ''
  64      opt.separator 'Options:'
  65      opt.on("--with-test-unit", 
  66             "Use Test::Unit tests instead sof RSpec.") { |v| options[:with_test_unit] = v }
  67    end
  68  end