Class: Rspec::Generators::ScaffoldGenerator

Inherits:
NamedBase
  • Object
show all
Defined in:
lib/generators/rspec/scaffold/scaffold_generator.rb

Instance Method Summary collapse

Methods inherited from NamedBase

banner, source_root

Constructor Details

#initialize(*args, &block) ⇒ ScaffoldGenerator

Returns a new instance of ScaffoldGenerator.



37
38
39
40
41
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
# File 'lib/generators/rspec/scaffold/scaffold_generator.rb', line 37

def initialize(*args, &block)
  super

  @my_options ||= {}
  @controller_actions = []
  @model_attributes = []

  args_for_c_m.each do |arg|
    if arg == '!'
      @my_options[:invert] = true
    elsif arg.include?(':')
      @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
    else
      @controller_actions << arg
      @controller_actions << 'create' if arg == 'new'
      @controller_actions << 'update' if arg == 'edit'
    end
  end

  @controller_actions.uniq!
  @model_attributes.uniq!

  if options.do_invert? || @controller_actions.empty?
    @controller_actions = all_actions - @controller_actions
  end

  if @model_attributes.empty?
    @my_options[:skip_model] = true # default to skipping model if no attributes passed
    if model_exists?
      model_columns_for_attributes.each do |column|
        @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
      end
    else
      @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
    end
  end
end

Instance Method Details

#create_filesObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/generators/rspec/scaffold/scaffold_generator.rb', line 75

def create_files      
  # Controller, helper, views, and spec directories.
    
  # empty_directory 'spec/controllers'
  # empty_directory 'spec/routing'
  # empty_directory 'spec/models'
  # empty_directory 'spec/helpers'
  # empty_directory 'spec/fixtures'
  # empty_directory 'spec/views'
  # empty_directory 'spec/integration'          
  
  # Controller spec, class, and helper.

  # template 'controller_spec.rb',
  #   File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
  # 
  # 
  # template 'helper_spec.erb',
  #   File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb")
  # 
  # # View specs
  # template "edit_erb_spec.rb",
  #   File.join('spec/views', controller_class_path, controller_file_name, "edit.#{default_file_extension}_spec.rb")
  # template "index_erb_spec.rb",
  #   File.join('spec/views', controller_class_path, controller_file_name, "index.#{default_file_extension}_spec.rb")
  # template "new_erb_spec.rb",
  #   File.join('spec/views', controller_class_path, controller_file_name, "new.#{default_file_extension}_spec.rb")
  # template "show_erb_spec.rb",
  #   File.join('spec/views', controller_class_path, controller_file_name, "show.#{default_file_extension}_spec.rb")

  no_fixture = options[:skip_fixture] ? '--skip-fixture' : ''
  skip_migration = options[:skip_migration] ? '--skip-migration' : ''
  flags = [no_fixture, skip_migration].join(' ')
  
  actions_str = controller_actions.join(' ')        
  generate "rspec:controller #{controller_file_name} #{actions_str}"
  
  attribs = model_attributes.map{|a| a.to_s}.join(' ')        
  generate "rspec:model #{controller_file_name} #{attribs} #{flags}"  
  
  empty_directory 'spec/routing'
  template 'routing_spec.rb', File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb")                      
end