From d2de7bc6f244d7130a765ede039f6dd184e28ba7 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Fri, 14 Dec 2012 23:13:46 +0000 Subject: [PATCH] Add slugs to updates. - add column to Update table - add a slug to all old updates - use slugs in URLs - make update slugs unique (we'll need them for delete URLs) --- app/models/update.rb | 2 ++ db/migrate/20121214224227_add_slug_to_updates.rb | 6 ++++++ db/migrate/20121214230336_give_old_updates_new_slugs.rb | 9 +++++++++ db/schema.rb | 6 ++++-- spec/models/update_spec.rb | 6 ++++++ 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20121214224227_add_slug_to_updates.rb create mode 100644 db/migrate/20121214230336_give_old_updates_new_slugs.rb diff --git a/app/models/update.rb b/app/models/update.rb index b39fe9d02..347b39101 100644 --- a/app/models/update.rb +++ b/app/models/update.rb @@ -1,4 +1,6 @@ class Update < ActiveRecord::Base + extend FriendlyId + friendly_id :subject, use: :slugged attr_accessible :body, :subject, :user_id belongs_to :user default_scope order("created_at desc") diff --git a/db/migrate/20121214224227_add_slug_to_updates.rb b/db/migrate/20121214224227_add_slug_to_updates.rb new file mode 100644 index 000000000..85a0510d5 --- /dev/null +++ b/db/migrate/20121214224227_add_slug_to_updates.rb @@ -0,0 +1,6 @@ +class AddSlugToUpdates < ActiveRecord::Migration + def change + add_column :updates, :slug, :string + add_index :updates, :slug, unique: true + end +end diff --git a/db/migrate/20121214230336_give_old_updates_new_slugs.rb b/db/migrate/20121214230336_give_old_updates_new_slugs.rb new file mode 100644 index 000000000..1ea1a3451 --- /dev/null +++ b/db/migrate/20121214230336_give_old_updates_new_slugs.rb @@ -0,0 +1,9 @@ +class GiveOldUpdatesNewSlugs < ActiveRecord::Migration + def up + Update.find_each(&:save) + end + + # note: this is basically impossible to reverse without removing + # friendly_id, because friendly_id automatically updates the slug when + # you save the object. +end diff --git a/db/schema.rb b/db/schema.rb index e7e80de28..1ce6d5599 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20121203034745) do +ActiveRecord::Schema.define(:version => 20121214230336) do create_table "crops", :force => true do |t| t.string "system_name", :null => false @@ -26,7 +26,7 @@ ActiveRecord::Schema.define(:version => 20121203034745) do create_table "gardens", :force => true do |t| t.string "name", :null => false - t.integer "user_id" + t.integer "user_id", :null => false t.string "slug", :null => false t.datetime "created_at", :null => false t.datetime "updated_at", :null => false @@ -48,9 +48,11 @@ ActiveRecord::Schema.define(:version => 20121203034745) do t.text "body", :null => false t.datetime "created_at", :null => false t.datetime "updated_at", :null => false + t.string "slug" end add_index "updates", ["created_at", "user_id"], :name => "index_updates_on_created_at_and_user_id" + add_index "updates", ["slug"], :name => "index_updates_on_slug", :unique => true create_table "users", :force => true do |t| t.string "email", :default => "", :null => false diff --git a/spec/models/update_spec.rb b/spec/models/update_spec.rb index 7704bed71..962571dfc 100644 --- a/spec/models/update_spec.rb +++ b/spec/models/update_spec.rb @@ -6,4 +6,10 @@ describe Update do Update.create! :subject => "second entry", :body => "blah", :user_id => 1 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" + end end