From febd78d77a2bef1b14b5c71ae5c8cf4a0989d3d7 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Mon, 17 Dec 2012 12:50:51 +0000 Subject: [PATCH] Use username-yyyymmdd-subject-line slugs. --- app/models/update.rb | 8 +++++--- config/routes.rb | 3 --- spec/controllers/updates_controller_spec.rb | 10 ---------- spec/models/update_spec.rb | 18 ++++++++++++++---- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/app/models/update.rb b/app/models/update.rb index 716d1981a..0ad193708 100644 --- a/app/models/update.rb +++ b/app/models/update.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 9d633663a..94e715271 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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: diff --git a/spec/controllers/updates_controller_spec.rb b/spec/controllers/updates_controller_spec.rb index 31cb6265f..1053e19d3 100644 --- a/spec/controllers/updates_controller_spec.rb +++ b/spec/controllers/updates_controller_spec.rb @@ -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, {} diff --git a/spec/models/update_spec.rb b/spec/models/update_spec.rb index 962571dfc..3966143b5 100644 --- a/spec/models/update_spec.rb +++ b/spec/models/update_spec.rb @@ -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