File: lib/generators/extension_controller/extension_controller_generator.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: ExtensionControllerGenerator#4
inherits from
  ControllerGenerator   
has properties
attribute: extension_name [RW] #6
method: initialize / 2 #9
method: manifest #15
method: rspec_manifest #23
method: banner #62
method: extension_path #66
method: destination_root #70
method: extension_uses_rspec? #74
method: add_options! / 1 #78

Code

   1  require 'rails_generator/base'
   2  require 'rails_generator/generators/components/controller/controller_generator'
   3 
   4  class ExtensionControllerGenerator < ControllerGenerator
   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}Controller", "#{class_name}Helper"
  27 
  28        # Controller, helper, views, and spec directories.
  29        m.directory File.join('app/controllers', class_path)
  30        m.directory File.join('app/helpers', class_path)
  31        m.directory File.join('app/views', class_path, file_name)
  32        m.directory File.join('spec/controllers', class_path)
  33        m.directory File.join('spec/helpers', class_path)
  34        m.directory File.join('spec/views', class_path, file_name)
  35 
  36        # Controller spec, class, and helper.
  37        m.template 'controller_spec.rb',
  38          File.join('spec/controllers', class_path, "#{file_name}_controller_spec.rb")
  39 
  40        m.template 'helper_spec.rb',
  41          File.join('spec/helpers', class_path, "#{file_name}_helper_spec.rb")
  42 
  43        m.template 'controller:controller.rb',
  44          File.join('app/controllers', class_path, "#{file_name}_controller.rb")
  45 
  46        m.template 'controller:helper.rb',
  47          File.join('app/helpers', class_path, "#{file_name}_helper.rb")
  48 
  49        # Spec and view template for each action.
  50        actions.each do |action|
  51          m.template 'view_spec.rb',
  52            File.join('spec/views', class_path, file_name, "#{action}_view_spec.rb"),
  53            :assigns => { :action => action, :model => file_name }
  54          path = File.join('app/views', class_path, file_name, "#{action}.html.erb")
  55          m.template 'controller:view.html.erb',
  56            path,
  57            :assigns => { :action => action, :path => path }
  58        end
  59      end
  60    end
  61    
  62    def banner
  63      "Usage: #{$0} #{spec.name} ExtensionName #{spec.name.camelize}Name [options]"
  64    end
  65    
  66    def extension_path
  67      File.join('vendor', 'extensions', @extension_name.underscore)
  68    end
  69    
  70    def destination_root
  71      File.join(RAILS_ROOT, extension_path)
  72    end
  73    
  74    def extension_uses_rspec?
  75      File.exists?(File.join(destination_root, 'spec')) && !options[:with_test_unit]
  76    end
  77    
  78    def add_options!(opt)
  79      opt.separator ''
  80      opt.separator 'Options:'
  81      opt.on("--with-test-unit", 
  82             "Use Test::Unit tests instead sof RSpec.") { |v| options[:with_test_unit] = v }
  83    end
  84  en