Last active
August 29, 2015 14:10
-
-
Save JacobKorn/69b981164da8b49e3fe4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe Car, :type => :model do | |
it { should belong_to :shape } | |
it { should belong_to :colour } | |
it { should belong_to :wheel_style } | |
it { should have_many :car_decals } | |
it { should have_many :decals } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe Shape, :type => :model do | |
it { should have_many :cars } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CarsController < ApplicationController | |
before_action :authenticate_user! | |
def index | |
@cars = Car.all | |
respond_to do |format| | |
format.html | |
format.json { render json: @cars} | |
end | |
end | |
def new | |
@car = Car.new | |
@select_options = CarOptions.new | |
end | |
def show | |
@car = Car.find(params[:id]) | |
respond_to do |format| | |
format.json { render json: @car} | |
end | |
end | |
def create | |
@car = Car.new(car_params) | |
@car.user = current_user | |
if @car.save | |
redirect_to(cars_url, notice: "new car '#{@car.name}' created") | |
end | |
end | |
def update | |
@car = Car.find(params[:id]) | |
respond_to do |format| | |
if @car.update(car_params) | |
format.json { render json: @car, status: :ok } | |
end | |
end | |
end | |
private | |
def car_params | |
params.require(:car).permit(:id, :name, :shape_id, :colour_id, :wheel_style_id) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CarOptions | |
def shape | |
@shape ||= Shape.pluck(:name, :id) | |
end | |
def colour | |
@colour ||= Colour.pluck(:name, :id) | |
end | |
def wheel_style | |
@wheel_style ||= WheelStyle.pluck(:name, :id) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe CarOptions do | |
describe "#shape" do | |
let(:shapes) {[double(:shape)]} | |
it "returns an array " do | |
allow(Shape).to receive(:pluck).and_return(shapes) | |
options = CarOptions.new | |
expect(options.shape).to eq(shapes) | |
end | |
it "calls pluck with :name and :id" do | |
expect(Shape).to receive(:pluck).with(:name, :id).and_return(shapes) | |
options = CarOptions.new | |
options.shape | |
end | |
end | |
describe "#colour" do | |
let(:colours) {[double(:colour)]} | |
it "returns an array " do | |
allow(Colour).to receive(:pluck).and_return(colours) | |
options = CarOptions.new | |
expect(options.colour).to eq(colours) | |
end | |
it "calls pluck with :name and :id" do | |
expect(Colour).to receive(:pluck).with(:name, :id).and_return(colours) | |
options = CarOptions.new | |
options.colour | |
end | |
end | |
describe "#wheel_style" do | |
let(:wheel_styles) {[double(:wheel_style)]} | |
it "returns an array " do | |
allow(WheelStyle).to receive(:pluck).and_return(wheel_styles) | |
options = CarOptions.new | |
expect(options.wheel_style).to eq(wheel_styles) | |
end | |
it "calls pluck with :name and :id" do | |
expect(WheelStyle).to receive(:pluck).with(:name, :id).and_return(wheel_styles) | |
options = CarOptions.new | |
options.wheel_style | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment