class Rspec::Generators::ScaffoldGenerator
def banner
def banner self.class.banner end
def copy_view(view)
def copy_view(view) template "#{view}_spec.rb", File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb") end
def example_invalid_attributes
def example_invalid_attributes @example_invalid_attributes ||= if attributes.any? { attributes.first.name => "invalid value" } else { } end end
def example_params_for_update
def example_params_for_update @example_params_for_update ||= if example_valid_attributes.any? example_valid_attributes else { "these" => "params" } end end
def example_valid_attributes
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
def formatted_hash(hash)
def formatted_hash(hash) formatted = hash.inspect formatted.gsub!("{", "{ ") formatted.gsub!("}", " }") formatted.gsub!("=>", " => ") formatted end
def generate_controller_spec
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
def generate_routing_spec
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
def generate_view_specs
def generate_view_specs return unless options[:view_specs] copy_view :edit copy_view :index unless options[:singleton] copy_view :new copy_view :show end
def mock_file_name(hash=nil)
called, it will be the one used.
If another ORM is being used and another method instead of save is
#=> mock_user(:save => true)
mock_file_name(:save => true)
value as response. So, for ActiveRecord and file name "User":
If a hash is given, it uses the hash key as the ORM method and the
it returns mock_user.
Returns the name of the mock. For example, if the file name is user,
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
def ns_file_name
def ns_file_name ns_parts.empty? ? file_name : "#{ns_parts[0].underscore}_#{ns_parts[1].singularize.underscore}" end
def ns_parts
def ns_parts @ns_parts ||= begin matches = ARGV[0].to_s.match(/\A(\w+)(?:\/|::)(\w+)/) matches ? [matches[1], matches[2]] : [] end end
def ns_table_name
def ns_table_name ns_parts.empty? ? table_name : "#{ns_parts[0].underscore}/#{ns_parts[1].tableize}" end
def should_receive(chain)
#=> User.should_receive(:get).with(37)
should! orm_class.find(User, "37")
For Datamapper:
#=> User.should_receive(:find).with(37)
should! orm_class.find(User, "37")
Receives the ORM chain and convert to expects. For ActiveRecord:
def should_receive(chain) stub_or_should_chain(:should_receive, chain) end
def stub(chain)
#=> User.stub(:get).with(37)
stub orm_class.find(User, "37")
For Datamapper:
#=> User.stub(:find).with(37)
stub orm_class.find(User, "37")
Receives the ORM chain and convert to stub. For ActiveRecord:
def stub(chain) stub_or_should_chain(:stub, chain) end
def stub_or_should_chain(mode, chain)
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
def value_for(attribute)
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
def webrat?
- Use `--webrat` instead.
def webrat? RSpec.deprecate("--webrat-matchers", "--webrat") if options[:webrat_matchers] options[:webrat] || options[:webrat_matchers] end