Permalink
Browse files

Added slug to Post

  • Loading branch information...
1 parent 10c1532 commit 04a1d2738712205c7347a7bd93bb220bf313274f @jhbabon committed Apr 21, 2012
View
@@ -56,3 +56,6 @@ gem 'mini_magick', '~> 3.4'
gem 'carrierwave', '~> 0.6.2'
# better forms
gem 'simple_form', '~> 2.0.1'
+
+# better urls
+gem 'friendly_id', '~> 4.0.4'
@@ -1,11 +1,24 @@
class PostsController < ApplicationController
before_filter :find_post
+ before_filter :post_moved_permanently, only: [:show]
def find_post
@post = Post.find params[:id]
end
protected :find_post
+ def post_moved_permanently
+ # from the friendly_id documentation:
+ #
+ # If an old id or a numeric id was used to find the record, then
+ # the request path will not match the post_path, and we should do
+ # a 301 redirect that uses the current friendly id.
+ if request.path != post_path(@post)
+ redirect_to @post, status: :moved_permanently
+ end
+ end
+ protected :post_moved_permanently
+
# GET /posts/1
# GET /posts/1.json
def show
View
@@ -1,7 +1,8 @@
# -*- encoding: utf-8 -*-
-# TODO: tests!
class Post < ActiveRecord::Base
+ extend FriendlyId
+ friendly_id :title, use: [:slugged, :history]
validates :title, :category, presence: true
@@ -0,0 +1,6 @@
+class AddSlugToPosts < ActiveRecord::Migration
+ def change
+ add_column :posts, :slug, :string
+ add_index :posts, :slug
+ end
+end
@@ -0,0 +1,18 @@
+class CreateFriendlyIdSlugs < ActiveRecord::Migration
+
+ def self.up
+ create_table :friendly_id_slugs do |t|
+ t.string :slug, :null => false
+ t.integer :sluggable_id, :null => false
+ t.string :sluggable_type, :limit => 40
+ t.datetime :created_at
+ end
+ add_index :friendly_id_slugs, :sluggable_id
+ add_index :friendly_id_slugs, [:slug, :sluggable_type], :unique => true
+ add_index :friendly_id_slugs, :sluggable_type
+ end
+
+ def self.down
+ drop_table :friendly_id_slugs
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120420184536) do
+ActiveRecord::Schema.define(:version => 20120421110715) do
create_table "categories", :force => true do |t|
t.string "name"
@@ -22,6 +22,17 @@
add_index "categories", ["key"], :name => "index_categories_on_key"
+ create_table "friendly_id_slugs", :force => true do |t|
+ t.string "slug", :null => false
+ t.integer "sluggable_id", :null => false
+ t.string "sluggable_type", :limit => 40
+ t.datetime "created_at"
+ end
+
+ add_index "friendly_id_slugs", ["slug", "sluggable_type"], :name => "index_friendly_id_slugs_on_slug_and_sluggable_type", :unique => true
+ add_index "friendly_id_slugs", ["sluggable_id"], :name => "index_friendly_id_slugs_on_sluggable_id"
+ add_index "friendly_id_slugs", ["sluggable_type"], :name => "index_friendly_id_slugs_on_sluggable_type"
+
create_table "images", :force => true do |t|
t.string "title"
t.string "resource"
@@ -39,8 +50,11 @@
t.datetime "updated_at", :null => false
t.string "excerpt"
t.integer "category_id"
+ t.string "slug"
end
+ add_index "posts", ["slug"], :name => "index_posts_on_slug"
+
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
@@ -7,6 +7,7 @@ class Backend::PostsControllerTest < ActionController::TestCase
include BackendTestCase
setup { @post = FactoryGirl.create(:post) }
+ teardown { Post.delete_all }
test "should get index" do
get :index
@@ -1,7 +1,26 @@
+# -*- encoding: utf-8 -*-
+
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
- # test "the truth" do
- # assert true
- # end
+
+ setup { @post = FactoryGirl.create(:post) }
+ teardown { Post.delete_all }
+
+ test "should show post" do
+ get :show, id: @post
+
+ assert_response :success
+ end
+
+ test "should redirect to new url if the post's slug has changed" do
+ old_slug = @post.slug
+ @post.title = "#{@post.title} #{Faker::Lorem.word}"
+ @post.save
+
+ get :show, id: old_slug
+
+ assert_response :moved_permanently
+ assert_redirected_to @post
+ end
end

0 comments on commit 04a1d27

Please sign in to comment.