File: lib/generators/extension/extension_generator.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: ExtensionGenerator#6
inherits from
  NamedBase ( Rails::Generator )
has properties
attribute: extension_path [R] #9
attribute: extension_file_name [R] #9
method: initialize / 2 #11
method: manifest #17
method: class_name #63
method: extension_name #67
method: author_info #71
method: homepage #79
method: author_email #83
method: author_name #87
method: add_options! / 1 #91

Class Hierarchy

Code

   1  begin
   2    require 'git'
   3  rescue LoadError
   4  end
   5 
   6  class ExtensionGenerator < Rails::Generator::NamedBase
   7    default_options :with_test_unit => false
   8    
   9    attr_reader :extension_path, :extension_file_name
  10    
  11    def initialize(runtime_args, runtime_options = {})
  12      super
  13      @extension_file_name = "#{file_name}_extension"
  14      @extension_path = "vendor/extensions/#{file_name}"
  15    end
  16    
  17    def manifest
  18      record do |m|
  19        m.directory "#{extension_path}/app/controllers"
  20        m.directory "#{extension_path}/app/helpers"
  21        m.directory "#{extension_path}/app/models"
  22        m.directory "#{extension_path}/app/views"
  23        m.directory "#{extension_path}/config/locales"
  24        m.directory "#{extension_path}/config/initializers"
  25        m.directory "#{extension_path}/db/migrate"
  26        m.directory "#{extension_path}/lib/tasks"
  27        
  28        m.template 'README.md',           "#{extension_path}/README.md"
  29        m.template 'extension.rb',        "#{extension_path}/#{extension_file_name}.rb"
  30        m.template 'tasks.rake',          "#{extension_path}/lib/tasks/#{extension_file_name}_tasks.rake"
  31        m.template 'en.yml',              "#{extension_path}/config/locales/en.yml"
  32        m.template 'routes.rb',           "#{extension_path}/config/routes.rb"
  33        m.template 'radiant_config.rb',   "#{extension_path}/config/initializers/radiant_config.rb"
  34        m.template 'lib.rb',              "#{extension_path}/lib/radiant-#{file_name}-extension.rb"
  35        m.template 'gemspec.rb',          "#{extension_path}/radiant-#{file_name}-extension.gemspec"
  36        
  37        if options[:with_test_unit]
  38          m.directory "#{extension_path}/test/fixtures"
  39          m.directory "#{extension_path}/test/functional"
  40          m.directory "#{extension_path}/test/unit"
  41 
  42          m.template 'Rakefile',            "#{extension_path}/Rakefile"
  43          m.template 'test_helper.rb',      "#{extension_path}/test/test_helper.rb"
  44          m.template 'functional_test.rb',  "#{extension_path}/test/functional/#{extension_file_name}_test.rb"
  45        else
  46          m.directory "#{extension_path}/spec/controllers"
  47          m.directory "#{extension_path}/spec/models"        
  48          m.directory "#{extension_path}/spec/views"
  49          m.directory "#{extension_path}/spec/helpers"
  50          m.directory "#{extension_path}/features/support"
  51          m.directory "#{extension_path}/features/step_definitions/admin"
  52 
  53          m.template 'RSpecRakefile',       "#{extension_path}/Rakefile"
  54          m.template 'spec_helper.rb',      "#{extension_path}/spec/spec_helper.rb"
  55          m.file     'spec.opts',           "#{extension_path}/spec/spec.opts"
  56          m.file     'cucumber.yml',        "#{extension_path}/cucumber.yml"
  57          m.template 'cucumber_env.rb',     "#{extension_path}/features/support/env.rb"
  58          m.template 'cucumber_paths.rb',   "#{extension_path}/features/support/paths.rb"
  59        end
  60      end
  61    end
  62    
  63    def class_name
  64      super.to_name.gsub(' ', '') + 'Extension'
  65    end
  66    
  67    def extension_name
  68      class_name.to_name('Extension')
  69    end
  70 
  71    def author_info
  72      @author_info ||= begin
  73        Git.global_config
  74      rescue NameError
  75        {}
  76      end
  77    end
  78 
  79    def homepage
  80      author_info['github.user'] ? "http://github.com/#{author_info['github.user']}/radiant-#{file_name}-extension" : "http://example.com/#{file_name}"
  81    end
  82 
  83    def author_email
  84      author_info['user.email'] || 'your email'
  85    end
  86 
  87    def author_name
  88      author_info['user.name'] || 'Your Name'
  89    end
  90    
  91    def add_options!(opt)
  92      opt.separator ''
  93      opt.separator 'Options:'
  94      opt.on("--with-test-unit", 
  95             "Use Test::Unit for this extension instead of RSpec") { |v| options[:with_test_unit] = v }
  96    end
  97  en