Use username-yyyymmdd-subject-line slugs.

This commit is contained in:
Miles Gould
2012-12-17 12:50:51 +00:00
parent b02eced9b7
commit febd78d77a
4 changed files with 19 additions and 20 deletions

View File

@@ -1,11 +1,13 @@
class Update < ActiveRecord::Base
extend FriendlyId
friendly_id :subject, use: :slugged
friendly_id :user_date_subject, use: :slugged
attr_accessible :body, :subject, :user_id
belongs_to :user
default_scope order("created_at desc")
def to_param
"#{created_at.year}/#{created_at.month}/#{created_at.day}/#{slug}"
def user_date_subject
# slugs are created before created_at is set
time = created_at || Time.now
"#{user.username} #{time.strftime("%Y%m%d")} #{subject}"
end
end

View File

@@ -6,9 +6,6 @@ Growstuff::Application.routes.draw do
resources :members
devise_for :users
match "/updates/:year/:month/:day/:id", :to => "updates#show"
match "/updates/:id", :to => "updates#show"
get "home/index"
# The priority is based upon order of creation:

View File

@@ -66,16 +66,6 @@ describe UpdatesController do
end
end
describe "GET show" do
it "assigns the requested update as @update fetching with dates" do
update = Update.create! valid_attributes
date = update.created_at
get :show, {:year => date.year, :month => date.month, :day => date.day,
:id => update.slug}
assigns(:update).should eq(update)
end
end
describe "GET new" do
it "assigns a new update as @update" do
get :new, {}

View File

@@ -1,15 +1,25 @@
require 'spec_helper'
describe Update do
before(:each) do
user = User.create! :username => "test", :password => "password",
:email => "test@example.com", :tos_agreement => true
user.confirm!
@id = user.id
end
it "should be sorted in reverse order" do
Update.create! :subject => "first entry", :body => "blah", :user_id => 1
Update.create! :subject => "second entry", :body => "blah", :user_id => 1
Update.create! :subject => "first entry", :body => "blah", :user_id => @id
Update.create! :subject => "second entry", :body => "blah", :user_id => @id
Update.first.subject.should == "second entry"
end
it "should have a slug" do
update = Update.create! :subject => "slugs are nasty",
:body => "They leave slime everywhere", :user_id => 1
update.slug.should == "slugs-are-nasty"
:body => "They leave slime everywhere", :user_id => @id
time = update.created_at
datestr = time.strftime("%Y%m%d")
datestr.length.should == 8
update.slug.should == "test-#{datestr}-slugs-are-nasty"
end
end