Class: ExtensionControllerGenerator

Inherits:
ControllerGenerator
  • Object
show all
Defined in:
lib/generators/extension_controller/extension_controller_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime_args, runtime_options = {}) ⇒ ExtensionControllerGenerator

Returns a new instance of ExtensionControllerGenerator.



9
10
11
12
13
# File 'lib/generators/extension_controller/extension_controller_generator.rb', line 9

def initialize(runtime_args, runtime_options = {})
  runtime_args = runtime_args.dup
  @extension_name = runtime_args.shift
  super(runtime_args, runtime_options)
end

Instance Attribute Details

#extension_nameObject

Returns the value of attribute extension_name.



6
7
8
# File 'lib/generators/extension_controller/extension_controller_generator.rb', line 6

def extension_name
  @extension_name
end

Instance Method Details

#add_options!(opt) ⇒ Object



78
79
80
81
82
83
# File 'lib/generators/extension_controller/extension_controller_generator.rb', line 78

def add_options!(opt)
  opt.separator ''
  opt.separator 'Options:'
  opt.on("--with-test-unit", 
         "Use Test::Unit tests instead sof RSpec.") { |v| options[:with_test_unit] = v }
end


62
63
64
# File 'lib/generators/extension_controller/extension_controller_generator.rb', line 62

def banner
  "Usage: #{$0} #{spec.name} ExtensionName #{spec.name.camelize}Name [options]"
end

#destination_rootObject



70
71
72
# File 'lib/generators/extension_controller/extension_controller_generator.rb', line 70

def destination_root
  File.join(RAILS_ROOT, extension_path)
end

#extension_pathObject



66
67
68
# File 'lib/generators/extension_controller/extension_controller_generator.rb', line 66

def extension_path
  File.join('vendor', 'extensions', @extension_name.underscore)
end

#extension_uses_rspec?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/generators/extension_controller/extension_controller_generator.rb', line 74

def extension_uses_rspec?
  File.exists?(File.join(destination_root, 'spec')) && !options[:with_test_unit]
end

#manifestObject



15
16
17
18
19
20
21
# File 'lib/generators/extension_controller/extension_controller_generator.rb', line 15

def manifest
  if extension_uses_rspec?
    rspec_manifest
  else
    super
  end
end

#rspec_manifestObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/extension_controller/extension_controller_generator.rb', line 23

def rspec_manifest
  record do |m|
    # Check for class naming collisions.
    m.class_collisions class_path, "#{class_name}Controller", "#{class_name}Helper"

    # Controller, helper, views, and spec directories.
    m.directory File.join('app/controllers', class_path)
    m.directory File.join('app/helpers', class_path)
    m.directory File.join('app/views', class_path, file_name)
    m.directory File.join('spec/controllers', class_path)
    m.directory File.join('spec/helpers', class_path)
    m.directory File.join('spec/views', class_path, file_name)

    # Controller spec, class, and helper.
    m.template 'controller_spec.rb',
      File.join('spec/controllers', class_path, "#{file_name}_controller_spec.rb")

    m.template 'helper_spec.rb',
      File.join('spec/helpers', class_path, "#{file_name}_helper_spec.rb")

    m.template 'controller:controller.rb',
      File.join('app/controllers', class_path, "#{file_name}_controller.rb")

    m.template 'controller:helper.rb',
      File.join('app/helpers', class_path, "#{file_name}_helper.rb")

    # Spec and view template for each action.
    actions.each do |action|
      m.template 'view_spec.rb',
        File.join('spec/views', class_path, file_name, "#{action}_view_spec.rb"),
        :assigns => { :action => action, :model => file_name }
      path = File.join('app/views', class_path, file_name, "#{action}.html.erb")
      m.template 'controller:view.html.erb',
        path,
        :assigns => { :action => action, :path => path }
    end
  end
end