<repo_name>Maudulus/car_listing<file_sep>/app/models/car.rb
class Car < ActiveRecord::Base
  belongs_to :manufacturer

  validates :color, presence: true
  validates :year, presence: true,
    numericality: { greater_than_or_equal_to: 1979, less_than_or_equal_to: Date.current.year }
  validates :mileage, presence: true, numericality: true
end
<file_sep>/spec/models/manufacturer_spec.rb
require 'spec_helper'

describe Manufacturer do
  it { should have_valid(:name).when('Toyota') }
  it { should_not have_valid(:name).when(nil, '') }

  it { should have_valid(:country).when('Africa') }
  it { should_not have_valid(:country).when(nil, '') }

  it { should have_many(:cars).dependent(:nullify) }
end
<file_sep>/spec/support/factories.rb
FactoryGirl.define do

  factory :car do
    color 'green'
    year '1980'
    mileage '200000'
    description 'Not only does this car look sporty and powerful, it also has aerodynamics equivalent to a garden shed, enabling it to reach speeds well beyond 100,000 metres per hour. In fact, with a tail wind these things can go even faster, but a cross wind presents problems, especially after the car ends up on its roof.'
  end

  factory :manufacturer do
    name '<NAME>'
  end
end
<file_sep>/spec/features/user_adds_manufacturer_spec.rb
require 'spec_helper'

feature 'used car salesperson specifies manufacturer (optionally)', %q{
  As a car salesperson
  I want to record a car manufacturer
  So that I can keep track of the types of cars found in the lot
  } do

    scenario 'salesperson adds a car with valid attributes' do
      manufacturer = FactoryGirl.build(:manufacturer)
      count = Manufacturer.count

      visit new_manufacturer_path
      new_manufacturer_helper(manufacturer)
      select('United Kingdom', from: 'manufacturer_country')
      click_on 'Add Manufacturer'

      expect(Manufacturer.count).to eq(count+1)
      expect(page).to have_content("Manufacturer Successfully Added")
      expect(current_path).to eq new_manufacturer_path
    end

    scenario 'salesperson adds a car with invalid attributes' do
      manufacturer = FactoryGirl.build(:manufacturer)
      count = Manufacturer.count

      visit new_manufacturer_path
      new_manufacturer_helper(manufacturer)
      click_on 'Add Manufacturer'

      expect(Manufacturer.count).to eq(count)
      expect(page).to have_content("Manufacturer Not Added")
      expect(current_path).to eq manufacturers_path
    end
end
<file_sep>/spec/features/user_adds_car_spec.rb
require 'spec_helper'

feature 'used car salesperson adds car', %q{
  As a car salesperson
  I want to record a newly acquired car
  So that I can list it in my lot
  } do

  scenario 'salesperson adds a car with valid attributes' do
    car = FactoryGirl.build(:car)
    count = Car.count

    visit new_car_path
    new_car_helper(car)
    click_on 'Add Car'

    expect(Car.count).to eq(count+1)
    expect(page).to have_content("Car Successfully Added")
    expect(current_path).to eq new_car_path
  end

  scenario 'salesperson adds a car with invalid attributes' do
    car = FactoryGirl.build(:car)
    count = Car.count

    visit new_car_path
    new_car_helper(car)
    fill_in "Color", with: ""
    click_on 'Add Car'

    expect(Car.count).to eq(count)
    expect(page).to have_content("Car Not Added")
    expect(current_path).to eq cars_path
  end

end
<file_sep>/spec/models/car_spec.rb
require 'spec_helper'

describe Car do
  it { should have_valid(:color).when('turquoise') }
  it { should_not have_valid(:color).when(nil, '') }

  it { should have_valid(:year).when('1980', '2014') }
  it { should_not have_valid(:year).when(nil, '', 'Nineteen Eighty-Four', 1977, 2015) }

  it { should have_valid(:mileage).when('200000') }
  it { should_not have_valid(:mileage).when(nil, '', "asdf") }

  it { should belong_to :manufacturer }
end
<file_sep>/spec/support/form_input_helper.rb
module FormHelper

  def new_car_helper(valid_attrs)
    fill_in 'Color', with: valid_attrs.color
    fill_in 'Year', with: valid_attrs.year
    fill_in 'Mileage', with: valid_attrs.mileage
    fill_in 'Description', with: valid_attrs.description
  end

  def new_manufacturer_helper(valid_attrs)
    fill_in 'Name', with: valid_attrs.name
  end
end
