class Rspec::Generators::ScaffoldGenerator

@private

Attributes

generator_args[R]

Public Class Methods

new(*args, &blk) click to toggle source
Calls superclass method
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 22
def initialize(*args, &blk)
  @generator_args = args.first
  super(*args, &blk)
end

Public Instance Methods

generate_controller_spec() click to toggle source
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 27
def generate_controller_spec
  return unless options[:controller_specs]

  template_file = File.join(
    'spec/controllers',
    controller_class_path,
    "#{controller_file_name}_controller_spec.rb"
  )
  if options[:api]
    template 'api_controller_spec.rb', template_file
  else
    template 'controller_spec.rb', template_file
  end
end
generate_routing_spec() click to toggle source
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 52
def generate_routing_spec
  return unless options[:routing_specs]

  template_file = File.join(
    'spec/routing',
    controller_class_path,
    "#{controller_file_name}_routing_spec.rb"
  )
  template 'routing_spec.rb', template_file
end
generate_view_specs() click to toggle source
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 42
def generate_view_specs
  return if options[:api]
  return unless options[:view_specs] && options[:template_engine]

  copy_view :edit
  copy_view :index unless options[:singleton]
  copy_view :new
  copy_view :show
end

Protected Instance Methods

banner() click to toggle source
copy_view(view) click to toggle source
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 69
def copy_view(view)
  template "#{view}_spec.rb",
           File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
end
ns_file_name() click to toggle source

support for namespaced-resources

# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 75
def ns_file_name
  ns_parts.empty? ? file_name : "#{ns_parts[0].underscore}_#{ns_parts[1].singularize.underscore}"
end
ns_parts() click to toggle source
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 84
def ns_parts
  @ns_parts ||= begin
                  matches = generator_args[0].to_s.match(/\A(\w+)(?:\/|::)(\w+)/)
                  matches ? [matches[1], matches[2]] : []
                end
end
ns_table_name() click to toggle source

support for namespaced-resources

# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 80
def ns_table_name
  ns_parts.empty? ? table_name : "#{ns_parts[0].underscore}/#{ns_parts[1].tableize}"
end
raw_value_for(attribute) click to toggle source
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 95
def raw_value_for(attribute)
  case attribute.type
  when :string
    attribute.name.titleize
  when :integer, :float
    @attribute_id_map ||= {}
    @attribute_id_map[attribute] ||= @attribute_id_map.keys.size.next + attribute.default
  else
    attribute.default
  end
end
value_for(attribute) click to toggle source
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 91
def value_for(attribute)
  raw_value_for(attribute).inspect
end