Skip to content

Instantly share code, notes, and snippets.

@Digital-Learner
Created March 20, 2013 12:43
Show Gist options
  • Save Digital-Learner/5204353 to your computer and use it in GitHub Desktop.
Save Digital-Learner/5204353 to your computer and use it in GitHub Desktop.
Attempt to generate a slug for use in first pairing individual project at Makers Academy
class Boat < ActiveRecord::Base
attr_accessible :name
before_save :generate_slug
# validates_presence_of :name
# validates_presence_of :slug
private
def generate_slug
self.slug = name.downcase.gsub(/'/, '').gsub(/[^a-z0-9]/i, '-').squeeze('-')
end
end
require 'test_helper'
class BoatTest < ActiveSupport::TestCase
test "Boat name is saved as slug with as alphanumeric for routing" do
@boat = Boat.create(name: "Old Stinky")
assert_equal "old-stinky", @boat.slug
end
test "Find boat with specified id" do
@boat = Boat.create(name: "Old Stinky")
@boat.update_column(:slug, nil)
assert_nil @boat.slug
@boat = Boat.find_by_id(@boat.id)
assert @boat
assert_equal "Old Stinky", @boat.name
end
test "Find boat with specified slug" do
@boat0 = Boat.create(name: "Old Stinky")
@boat1 = Boat.create(name: "John's Boat")
@boat2 = Boat.create(name: "Lady Marmaduke")
assert_equal 3, Boat.count
assert "old-stinky", @boat0.slug
assert "johns-boat", @boat1.slug
assert "lady-marmaduke", @boat2.slug
@boat0.update_column(:slug, nil)
assert_nil @boat0.slug
@found_boat2 = Boat.find_by_slug("lady-marmaduke")
assert_equal @boat2, @found_boat2
@found_boat1 = Boat.find_by_slug("johns-boat")
assert_equal @boat1, @found_boat1
@found_boat0 = Boat.find_by_slug("old-stinky")
refute @found_boat0
end
test "Fail when non-existant boat requested" do
@boat_in_existance = Boat.create(name: "Old Stinky")
@boat_in_existance.update_column(:slug, nil)
@non_existant_boat = Boat.find_by_id("fine-and-dandy")
assert @non_existant_boat
refute @boat_in_existance
end
end
class BoatsController < ApplicationController
def index
@boats = Boat.all
end
def show
@boat = Boat.find_by_id(params[:id]) || Boat.find_by_slug(params[:slug])
raise ActiveRecord::RecordNotFound if @boat.nil?
end
def new
@boat = Boat.new
end
def create
@boat = Boat.new(params[:boat])
if @boat.save
redirect_to @boat
else
render :new
end
end
def edit
@boat = Boat.find(params[:id])
end
def update
@boat = Boat.find(params[:id])
if @boat.update_attributes(params[:boat])
redirect_to @boat
else
render :edit
end
end
def destroy
@boat = Boat.find(params[:id]).delete
redirect_to boats_path
end
end
ThamesCommute::Application.routes.draw do
get '/boats/:id' => 'boats#show', :constraints => { :id => /\d+/ }
get '/boats/:slug' => 'boats#show', :constraints => { :slug => /(?!new).+/}
resources :boats
get '/help', :to => 'static_pages#help'
get '/about', :to => 'static_pages#about'
get '/contact', :to => 'static_pages#contact'
root :to => 'static_pages#home'
end
require 'test_helper'
class RoutingTest < ActionDispatch::IntegrationTest
# test "boats route with :id request should route to boats show" do
# assert_generates "/boats/1", { :controller => "boats", :action => "show", :id => 1 }
# end
test "boats route with :slug request should route to boats show" do
assert_generates "/boats/bobs-jane", { :controller => "boats", :action => "show", :id => "bobs-jane" }
assert_generates "/boats/bobs-jane", { :controller => "boats", :action => "show", :slug => "bobs-jane" }
assert_generates "/boats/new-jane", { :controller => "boats", :action => "show", :slug => "new-jane" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment