Skip to content

Commit

Permalink
add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Oct 6, 2010
1 parent 87e5cb7 commit 0e7e7d8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
Empty file removed README
Empty file.
45 changes: 45 additions & 0 deletions README.textile
@@ -0,0 +1,45 @@
h1. SimpleSlugs

simple_slugs aims to be an as-simple-as-possible implementation of slugging/permalink functionality for ActiveRecord 3, but still be I18n-ready by providing transliteration support.

h2. Usage

simple_slugs adds an act_macro to activate slugging support for an ActiveRecord model:

<pre>
class Post
has_slug
end
</pre>

simple_slugs has the following assumptions/defaults:

* The model has a column named "slug" which is used for the slug.
* The model has a column named "title" or "name" which is used as a source for the slug.
* There's no scope to be taken into account when checking for uniqueness of slugs.
* The slug only needs to be updated if the slug column is blank.

You can overwrite these defaults as follows:

<pre>
class Post
has_slug :slug_name => :permalink, # use the permalink column for storing the slug
:source => :heading, # use the heading column as a source
:on_blank => false, # always update the slug
:scope => :blog_id # scope uniqueness of slugs to the current blog_id
end
</pre>

h2. Slugging

simple_slugs performs the following operations on the source value (e.g. post.title):

<pre>
transliterate! # using the current locale, e.g. German "Ä" => "Ae"
spacify! # replace everything except word chars with spaces
join_spaces! # replace duplicate spaces with single spaces
strip! # strip leading and tailing spaces
downcase! # downcase the string
dasherize! # replace spaces with dashes
</pre>

24 changes: 14 additions & 10 deletions lib/simple_slugs/slug.rb
Expand Up @@ -4,25 +4,29 @@ def initialize(string)
super
normalize
end

def normalize
transliterate
spacify
transliterate!
spacify!
join_spaces!
strip!
downcase!
dasherize
dasherize!
end
def transliterate

def transliterate!
replace(I18n.transliterate(self))
end
def spacify

def spacify!
gsub!(/[\W_]/, ' ')
end

def join_spaces!
gsub!(/\s+/, ' ')
end
def dasherize

def dasherize!
gsub!(' ', '-')
end
end
Expand Down

0 comments on commit 0e7e7d8

Please sign in to comment.