Skip to content

Commit

Permalink
Added upgrade migration. Tweaked README.
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Clarke committed Dec 19, 2008
1 parent 893d08d commit a77ee14
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 11 deletions.
1 change: 1 addition & 0 deletions History.txt
@@ -1,6 +1,7 @@
== RELEASE DATE COMING SOON

* FriendlyId 2.0 Released

* Support for scoped slugs (Norman Clarke)
* Support for UTF-8 friendly_ids (Norman Clarke)
* Can now be installed via Ruby Gems, or as a Rails plugin (Norman Clarke)
Expand Down
2 changes: 2 additions & 0 deletions Manifest.txt
Expand Up @@ -16,6 +16,8 @@ coverage/rails-init_rb.html
friendly_id.gemspec
generators/friendly_id/friendly_id_generator.rb
generators/friendly_id/templates/create_slugs.rb
generators/friendly_id_20_upgrade/friendly_id_20_upgrade_generator.rb
generators/friendly_id_20_upgrade/templates/upgrade_friendly_id_to_20.rb
init.rb
lib/friendly_id.rb
lib/friendly_id/non_sluggable_class_methods.rb
Expand Down
65 changes: 54 additions & 11 deletions README.rdoc
Expand Up @@ -86,15 +86,27 @@ numeric id, or with an outdated slug.

=== Slug Versioning

FriendlyId will record changes to slugs so that you can tell when the model
is found with an older slug. This can be useful if you want to do a 301
redirect to your updated URL.
FriendlyId will record changes to slugs so that you can tell when the model is
found with an older slug, or by the numeric id. This can be useful if you want
to do a 301 redirect to your updated URL.

class PostsController < ApplicationController

before_filter ensure_current_post_url, :only => :show

...

def ensure_current_post_url
redirect_to @post, :status => :moved_permanently if @post.has_better_id?
end

def find_record_using_friendly_id
@post = Post.find(params[:id])
redirect_to @post, :status => :moved_permanently if @post.has_better_id?
end

This is particularly useful when implementing FrindlyId on an existing
website that already has many URL's with the old numeric id listed on search
engines. When the search engine spiders crawl your site, they will
eventually pick up the new, more SEO-friendly URL's.

=== Non-unique Slug Names

FriendlyId will append a arbitrary number to the end of the id to keep it
Expand All @@ -112,9 +124,6 @@ rest of the slug. This is important to enable having slugs like:
/cars/peugeot-206
/cars/peugeot-206--2

This is somewhat ugly, but you can change the "--" for another character or
sequence of characters if you wish.

=== Reserved Names

You can mark off some strings as reserved so that, for example, you don't end
Expand Down Expand Up @@ -204,7 +213,7 @@ that uses a non-Roman writing system, your feedback would be most welcome.

FriendlyId is installed as a Ruby Gem:

gem install friendly-id
gem install friendly_id

Alternatively, you can install it as a Rails plugin, though this is
discouraged:
Expand All @@ -217,7 +226,7 @@ FriendlyId currently works with Rails 2.0.0 and higher. Here's how to set it up.

1) Install the Gem:

sudo gem install friendly-id
sudo gem install friendly_id
cd my_app
script/generate friendly_id
rake db:migrate
Expand Down Expand Up @@ -250,6 +259,39 @@ The default is to remove dead slugs older than 45 days, but is configurable:

rake:friendly_id:remove_old_slugs MODEL=MyModelName DAYS=60

== Upgrading from an older version

If you installed an older version of FriendlyId and want to upgrade to 2.0,
follow these steps:

==== Install the friendly_id Gem:

sudo gem install friendly_id

==== Add FriendlyId to environment.rb:

===== For Rails 2.1 and higher:

config.gem "friendly_id", :version => ">= 2.0.0"

===== For Rails 2.0:

Add this to the bottom of environment.rb:

require 'friendly_id'

==== Remove the older version of FriendlyId:

git rm -rf vendor/plugins/friendly_id
svn delete vendor/plugins/friendly_id
# or whatever

==== Generate the upgrade migration and run it

./script generate friendly_id_20_upgrade
rake db:migrate

That's it!

== Hacking FriendlyId:

Expand All @@ -260,6 +302,7 @@ we love pull requests. :-)

Please report them on Lighthouse[http://randomba.lighthouseapp.com/projects/14675-friendly_id].


== Credits:

FriendlyId was created by {Norman Clarke}[mailto:norman@randomba.org],
Expand Down
@@ -0,0 +1,11 @@
class FriendlyId20UpgradeGenerator < Rails::Generator::Base
def manifest
record do |m|
unless options[:skip_migration]
m.migration_template(
'upgrade_friendly_id_to_20.rb', 'db/migrate', :migration_file_name => 'upgrade_friendly_id_to_20'
)
end
end
end
end
@@ -0,0 +1,19 @@
class UpgradeFriendlyIdTo20 < ActiveRecord::Migration

def self.up
remove_column :slugs, :updated_at
remove_index :slugs, :column => [:name, :sluggable_type]
add_column :slugs, :sequence, :integer, :null => false, :default => 1
add_column :slugs, :scope, :string, :limit => 40
add_index :slugs, [:name, :sluggable_type, :scope, :sequence], :unique => true, :name => "index_slugs_on_n_s_s_and_s"
end

def self.down
remove_index :slugs, :name => "index_slugs_on_n_s_s_and_s"
remove_column :slugs, :scope
remove_column :slugs, :sequence
add_column :slugs, :updated_at, :datetime
add_index :slugs, [:name, :sluggable_type], :unique => true
end

end

0 comments on commit a77ee14

Please sign in to comment.