Class: Rspec::Generators::ControllerGenerator

Inherits:
NamedBase
  • Object
show all
Defined in:
lib/generators/rspec/controller/controller_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NamedBase

banner

Constructor Details

#initialize(*args, &block) ⇒ ControllerGenerator

Returns a new instance of ControllerGenerator.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/rspec/controller/controller_generator.rb', line 25

def initialize(*args, &block)
  super
  @actions = []

  controller_actions.each do |arg|
    @actions << arg
    @actions << 'create' if arg == 'new'
    @actions << 'update' if arg == 'edit'
  end

  @actions.uniq!

  if @actions.empty?
    @actions = all_actions - @actions
  end
end

Class Method Details

.source_rootObject



21
22
23
# File 'lib/generators/rspec/controller/controller_generator.rb', line 21

def self.source_root
  @source_root ||= File.expand_path('../templates', __FILE__)
end

Instance Method Details

#create_filesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/generators/rspec/controller/controller_generator.rb', line 42

def create_files
  # Check for class naming collisions.
  class_collisions class_path, "#{class_name}Controller", "#{class_name}Helper"

  @default_file_extension = "erb"

  # Controller, helper, views, and spec directories.
  inside 'app' do
    empty_directory 'controllers'
    empty_directory 'helpers'
    empty_directory 'views'
  end
  
  # use hooks instead!
  
  # template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb")
  # template 'helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb")

  html_actions = actions.reject{|a| %w{create update}.include?(a)}
  # 
  # html_actions.each do |action|
  #   @action = action          
  #   template "views/#{@default_file_extension}/#{action}.html.#{@default_file_extension}", "app/views/#{action}.html.#{@default_file_extension}"
  # 
  #   # add '_form' view for new or update action
  #   if %w{edit new}.include?(action)
  #     template "views/#{@default_file_extension}/_form.html.#{@default_file_extension}", "app/views/_form.html.#{@default_file_extension}"
  #   end
  # end
    
  # spec  
  inside 'spec' do
    empty_directory 'controllers'
    empty_directory 'helpers'
    empty_directory 'views'
  end
    
  # Controller spec, class, and helper.
  template 'controller_spec.rb', File.join('spec/controllers', class_path, "#{file_name}_controller_spec.rb")        
  template 'helper_spec.erb', File.join('spec/helpers', class_path, "#{file_name}_helper_spec.rb")
  
  # Spec and view template for each action.
  html_actions.each do |action|
    @action = action
    target_file = "spec/views/#{file_name}/actions/#{action}_html_#{@default_file_extension}_spec.rb"
    puts target_file
    template 'view_spec.rb', target_file
  end        
  
  actions_str = actions.join(' ')
  generate "controller #{file_name} #{actions_str}"        
end