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)
This commit is contained in:
Miles Gould committed 2012-12-15 00:18:46 +00:00
1 parent 69d943e721
commit d2de7bc6f2
5 files changed
+27 -2

No files matched your search

+2
View File
@@ -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")
@@ -0,0 +1,6 @@
class AddSlugToUpdates < ActiveRecord::Migration
def change
add_column :updates, :slug, :string
add_index :updates, :slug, unique: true
end
end
@@ -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
+4 -2
View File
@@ -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
+6
View File
@@ -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