Skip to content

Instantly share code, notes, and snippets.

@data-doge
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save data-doge/cd358acb693e3daa5941 to your computer and use it in GitHub Desktop.

Select an option

Save data-doge/cd358acb693e3daa5941 to your computer and use it in GitHub Desktop.
meowtown skeleton
class Cat < ActiveRecord::Base
validates :name, presence: true
validates :life_story, presence: true
validates :image_url, presence: true
def lose_a_life!
if self.lives > 1
self.lives -= 1
self.save
else
self.destroy
end
end
end
require 'rails_helper'
RSpec.describe Cat, type: :model do
let(:healthy_cat) { create(:cat) }
let(:almost_dead_cat) { create(:cat_with_1_life) }
describe "fields" do
it { should have_db_column(:name).of_type(:string) }
it { should have_db_column(:life_story).of_type(:text) }
it { should have_db_column(:image_url).of_type(:string) }
it { should have_db_column(:lives).of_type(:integer).with_options(default: 9) }
end
describe "validations" do
it { should validate_presence_of(:name) }
it { should validate_presence_of(:life_story) }
it { should validate_presence_of(:image_url) }
end
describe "methods" do
describe "#lose_a_life!" do
context "if cat has more than 1 life remaining" do
it "decrements the cat's lives by 1" do
healthy_cat.lose_a_life!
expect(healthy_cat.lives).to eq(8)
end
end
context "if cat has 1 life remaining" do
it "removes the cat from the database" do
almost_dead_cat.lose_a_life!
expect(Cat.find_by_name(almost_dead_cat.name)).to be_nil
end
end
end
end
end
class CatsController < ApplicationController
def index
@cats = Cat.all
end
def show
@cat = Cat.find(params[:id])
@cat.lose_a_life!
end
def new
@cat = Cat.new
end
def create
@cat = Cat.new(cat_params)
if @cat.save
redirect_to @cat
else
render 'new', status: 400
end
end
def edit
@cat = Cat.find(params[:id])
end
def update
@cat = Cat.find(params[:id])
if @cat.update(cat_params)
redirect_to @cat
else
render 'edit', status: 400
end
end
private
def cat_params
params.require(:cat).permit(:name, :life_story, :image_url)
end
end
require 'rails_helper'
RSpec.describe CatsController, type: :controller do
describe "#index" do
before do
5.times { Cat.create(attributes_for(:cat)) }
get :index
end
it { should respond_with(200) }
it { should render_template(:index) }
it "should assign @cats to all Cats in DB" do
expect(assigns(:cats)).to eq(Cat.all)
end
end
describe "#show" do
before do
@cat = Cat.create(attributes_for(:cat))
allow(@cat).to receive(:lose_a_life!)
get :show, id: @cat.id
end
it { should respond_with(200) }
it { should render_template(:show) }
it "should assign cat with specified id to @cat" do
expect(assigns(:cat)).to eq(@cat)
end
it "should call cat's #lose_a_life! method" do
@cat.reload
expect(@cat.lives).to eq(8)
end
end
describe "#new" do
before do
get :new
end
it { should respond_with(200) }
it { should render_template(:new) }
it "should assign an instance of Cat to @cat" do
expect(assigns(:cat)).to be_a(Cat)
end
end
describe "#create" do
context "if valid params" do
before do
@cat_params = attributes_for(:cat)
post :create, { cat: @cat_params }
end
it { should respond_with(302) }
it "should redirect to the new cat's page" do
cat = Cat.find_by(@cat_params)
expect(response).to redirect_to("/cats/#{cat.id}")
end
it "creates a new cat with specified params" do
expect(Cat.find_by(@valid_params)).to be_truthy
end
end
context "if invalid params" do
before do
post :create, { cat: {name: "test"} }
end
it { should respond_with(400) }
it { should render_template(:new) }
it "should not create a new cat" do
expect(Cat.find_by_name("test")).to be_nil
end
end
end
describe "#edit" do
before do
@cat = create(:cat)
get :edit, id: @cat.id
end
it { should respond_with(200) }
it { should render_template(:edit) }
it "should assign cat with specified id to @cat" do
expect(assigns(:cat)).to eq(@cat)
end
end
describe "#update" do
context "with valid params" do
before do
@cat = create(:cat)
@cat_params = attributes_for(:cat)
patch :update, { id: @cat.id, cat: @cat_params }
end
it { should respond_with(302) }
it { should redirect_to("/cats/#{@cat.id}")}
it "should update the attributes for cat" do
expect(Cat.find_by(@cat_params)).to be_truthy
end
end
context "with invalid params" do
before do
@cat = create(:cat)
@invalid_cat_params = { name: "", life_story: "", image_url: "" }
patch :update, { id: @cat.id, cat: @invalid_cat_params }
end
it { should respond_with(400) }
it { should render_template(:edit) }
it "should not update the attributes for cat" do
expect(Cat.find_by(@invalid_cat_params)).to be_nil
end
end
end
end
<h1>edit a cat</h1>
<% if @cat.errors.any? %>
<ul>
<% @cat.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= form_for :cat, url: cat_path(@cat), method: :patch do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :life_story %><br>
<%= f.text_area :life_story %>
</p>
<p>
<%= f.label :image_url %><br>
<%= f.text_field :image_url %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to('back', cats_path) %>
# spec/factories/cat.rb
FactoryGirl.define do
factory :cat do
name Faker::Name.name
life_story Faker::Lorem.paragraph
image_url Faker::Avatar.image
factory :cat_with_1_life do
lives 1
end
end
end
<h1>meowtown</h1>
<%= link_to('make a new cat', new_cat_path) %>
<ul>
<% @cats.each do |cat| %>
<li>
<h2><%= cat.name %></h2>
<img src='<%=cat.image_url%>' />
<p>lives left: <%= cat.lives %></p>
<%= link_to('show this cat', cat_path(cat)) %>
</li>
<% end %>
</ul>
<h1>create a cat</h1>
<% if @cat.errors.any? %>
<ul>
<% @cat.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= form_for :cat, url: cats_path do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :life_story %><br>
<%= f.text_area :life_story %>
</p>
<p>
<%= f.label :image_url %><br>
<%= f.text_field :image_url %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to('back', cats_path) %>
<h1><%= @cat.name %></h1>
<img src='<%= @cat.image_url %>' />
<h2>Life Story:</h2>
<p><%= @cat.life_story %></p>
<%= link_to('edit this cat', edit_cat_path(@cat)) %>
<%= link_to('back', cats_path) %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment