Class: Rspec::Generators::ScaffoldGenerator

### Home Classes Methods

### In Files

- lib/generators/rspec/scaffold/scaffold_generator.rb

### Parent

Rspec::Generators::Base

### Methods

- #banner
- #copy_view
- #example_invalid_attributes
- #example_params_for_update
- #example_valid_attributes
- #formatted_hash
- #generate_controller_spec
- #generate_routing_spec
- #generate_view_specs
- #mock_file_name
- #ns_file_name
- #ns_parts
- #ns_table_name
- #should_receive
- #stub
- #stub_or_should_chain
- #value_for
- #webrat?

### Included Modules

- ::Rails::Generators::ResourceHelpers

### Files

- fixture

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Rails
- RSpec::Rails::AssertionDelegator
- RSpec::Rails::ControllerExampleGroup
- RSpec::Rails::ControllerExampleGroup::BypassRescue
- RSpec::Rails::ControllerExampleGroup::ClassMethods
- RSpec::Rails::Extensions
- RSpec::Rails::Extensions::ActiveRecord
- RSpec::Rails::FeatureExampleGroup
- RSpec::Rails::FixtureSupport
- RSpec::Rails::HelperExampleGroup
- RSpec::Rails::HelperExampleGroup::ClassMethods
- RSpec::Rails::IllegalDataAccessException
- RSpec::Rails::MailerExampleGroup
- RSpec::Rails::MailerExampleGroup::ClassMethods
- RSpec::Rails::Matchers
- RSpec::Rails::Matchers::BeANew
- RSpec::Rails::Matchers::BeANewRecord
- RSpec::Rails::Matchers::BeValid
- RSpec::Rails::Matchers::HaveExtensions
- RSpec::Rails::Matchers::RedirectTo
- RSpec::Rails::Matchers::RedirectTo::RedirectTo
- RSpec::Rails::Matchers::RenderTemplate
- RSpec::Rails::Matchers::RenderTemplate::RenderTemplateMatcher
- RSpec::Rails::Matchers::RoutingMatchers
- RSpec::Rails::Matchers::RoutingMatchers::BeRoutableMatcher
- RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers
- RSpec::Rails::Matchers::RoutingMatchers::RouteToMatcher
- RSpec::Rails::MiniTestLifecycleAdapter
- RSpec::Rails::MinitestCounters
- RSpec::Rails::Mocks
- RSpec::Rails::Mocks::ActiveModelInstanceMethods
- RSpec::Rails::Mocks::ActiveModelStubExtensions
- RSpec::Rails::Mocks::ActiveRecordInstanceMethods
- RSpec::Rails::Mocks::ActiveRecordStubExtensions
- RSpec::Rails::ModelExampleGroup
- RSpec::Rails::ModuleInclusion
- RSpec::Rails::RailsExampleGroup
- RSpec::Rails::Railtie
- RSpec::Rails::RequestExampleGroup
- RSpec::Rails::RoutingExampleGroup
- RSpec::Rails::RoutingExampleGroup::ClassMethods
- RSpec::Rails::SetupAndTeardownAdapter
- RSpec::Rails::SetupAndTeardownAdapter::ClassMethods
- RSpec::Rails::TestUnitAssertionAdapter
- RSpec::Rails::TestUnitAssertionAdapter::AssertionDelegator
- RSpec::Rails::TestUnitAssertionAdapter::ClassMethods
- RSpec::Rails::Version
- RSpec::Rails::ViewAssigns
- RSpec::Rails::ViewExampleGroup
- RSpec::Rails::ViewExampleGroup::ClassMethods
- RSpec::Rails::ViewExampleGroup::ExampleMethods
- RSpec::Rails::ViewRendering
- RSpec::Rails::ViewRendering::ClassMethods
- RSpec::Rails::ViewRendering::EmptyTemplateHandler
- RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator
- RSpec::Rails::ViewRendering::EmptyTemplates
- Rspec
- Rspec::Generators
- Rspec::Generators::Base
- Rspec::Generators::ControllerGenerator
- Rspec::Generators::HelperGenerator
- Rspec::Generators::InstallGenerator
- Rspec::Generators::IntegrationGenerator
- Rspec::Generators::MailerGenerator
- Rspec::Generators::ModelGenerator
- Rspec::Generators::ObserverGenerator
- Rspec::Generators::ScaffoldGenerator
- Rspec::Generators::ViewGenerator
- Rails
- Rails::Generators
- Rails::Generators::GeneratedAttribute
- Test
- Test::Unit
- Test::Unit::AssertionFailedError
- ActiveModel
- ActiveModel::Validations
- ActiveRecord
- ActiveRecord::Base
- Autotest
- Autotest::RailsRspec2
- Object
- RequestInstanceMethods

No matching classes.

# Rspec::Generators::ScaffoldGenerator

### Public Instance Methods

generate_controller_spec() click to toggle source

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 22
    def generate_controller_spec
      return unless options[:controller_specs]

      template 'controller_spec.rb',
               File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
    end

generate_routing_spec() click to toggle source

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 38
    def generate_routing_spec
      return unless options[:routing_specs]

      template 'routing_spec.rb',
        File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb")
    end

generate_view_specs() click to toggle source

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 29
    def generate_view_specs
      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

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 186
    def banner
      self.class.banner
    end

copy_view(view) click to toggle source

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

example_invalid_attributes() click to toggle source

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 72
    def example_invalid_attributes
      @example_invalid_attributes ||=
        if attributes.any?
          { attributes.first.name => "invalid value" }
        else
          { }
        end
    end

example_params_for_update() click to toggle source

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 81
    def example_params_for_update
      @example_params_for_update ||=
        if example_valid_attributes.any?
          example_valid_attributes
        else
          { "these" => "params" }
        end
    end

example_valid_attributes() click to toggle source

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 60
    def example_valid_attributes
      # Only take the first attribute so this hash does not become unweildy and large in the
      # generated controller spec. It is the responsibility of the user to keep the the valid
      # attributes method up-to-date as they add validations.
      @example_valid_attributes ||=
        if attributes.any?
          { attributes.first.name => attributes.first.default.to_s }
        else
          { }
        end
    end

formatted_hash(hash) click to toggle source

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 90
    def formatted_hash(hash)
      formatted = hash.inspect
      formatted.gsub!("{", "{ ")
      formatted.gsub!("}", " }")
      formatted.gsub!("=>", " => ")
      formatted
    end

mock_file_name(hash=nil) click to toggle source

Returns the name of the mock. For example, if the file name is user, it returns mock_user.

If a hash is given, it uses the hash key as the ORM method and the value as response. So, for ActiveRecord and file name "User":

    mock_file_name(:save => true)
    #=> mock_user(:save => true)

If another ORM is being used and another method instead of save is called, it will be the one used.

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 127
    def mock_file_name(hash=nil)
      if hash
        method, and_return = hash.to_a.first
        method = orm_instance.send(method).split('.').last.gsub(/\(.*?\)/, '')
        "mock_#{ns_file_name}(:#{method} => #{and_return})"
      else
        "mock_#{ns_file_name}"
      end
    end

ns_file_name() click to toggle source

support for namespaced-resources

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 99
    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 108
    def ns_parts
      @ns_parts ||= begin
                      matches = ARGV[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 104
    def ns_table_name
      ns_parts.empty? ? table_name : "#{ns_parts[0].underscore}/#{ns_parts[1].tableize}"
    end

should_receive(chain) click to toggle source

Receives the ORM chain and convert to expects. For ActiveRecord:

    should! orm_class.find(User, "37")
    #=> User.should_receive(:find).with(37)

For Datamapper:

    should! orm_class.find(User, "37")
    #=> User.should_receive(:get).with(37)

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 147
    def should_receive(chain)
      stub_or_should_chain(:should_receive, chain)
    end

stub(chain) click to toggle source

Receives the ORM chain and convert to stub. For ActiveRecord:

    stub orm_class.find(User, "37")
    #=> User.stub(:find).with(37)

For Datamapper:

    stub orm_class.find(User, "37")
    #=> User.stub(:get).with(37)

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 161
    def stub(chain)
      stub_or_should_chain(:stub, chain)
    end

stub_or_should_chain(mode, chain) click to toggle source

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 165
    def stub_or_should_chain(mode, chain)
      receiver, method = chain.split(".")
      method.gsub!(/\((.*?)\)/, '')

      response = "#{receiver}.#{mode}(:#{method})"
      response << ".with(#{$1})" unless $1.blank?
      response
    end

value_for(attribute) click to toggle source

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 174
    def value_for(attribute)
      case attribute.type
      when :string
        "#{attribute.name.titleize}".inspect
      when :integer
        @attribute_id_map ||= {}
        @attribute_id_map[attribute] ||= @attribute_id_map.keys.size.next.to_s
      else
        attribute.default.inspect
      end
    end

webrat?() click to toggle source

@deprecated Use `--webrat` instead.

    # File lib/generators/rspec/scaffold/scaffold_generator.rb, line 50
    def webrat?
      RSpec.deprecate("--webrat-matchers", :replacement => "--webrat") if options[:webrat_matchers]
      options[:webrat] || options[:webrat_matchers]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.