Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Make your model play nice with SEO

## config/initializers/string_extensions.rb

require 'unicode'

class String
  def to_slug
    str = Unicode.normalize_KD(self).gsub(/[^\x00-\x7F]/n,'')
    str = str.gsub(/\W+/, '-').gsub(/^-+/,'').gsub(/-+$/,'').downcase
  end

  def numeric?
    if self =~ /^\d+$/
      self.to_i
    elsif self =~ /^\d+([,\.]\d+)?$/
      self.tr(',','.').to_f
    else
      false
    end
  end
end


## app/models/article.rb

# string :title
# text   :body
# string :permalink
class Article < ActiveRecord::Base
  before_save :set_permalink

  # article_path(@article)
  # >> /article/5
  # article_path(@article.permalink)
  # /article/pink-ferret
  def self.find(*args)
    if args.first.is_a?(String) and !args.first.numeric?
      find_by_permalink(args.shift,*args) or raise ActiveRecord::RecordNotFound
    else
      super
    end
  end

  # article_path(@article)
  # >> /article/5-pink-ferret
  def to_param
    "#{id}-#{permalink}"
  end

  private
  def set_permalink
    self.permalink = title.to_slug
  end
end

Permalink from post title

Method for permalink creation in ruby, based on drupal code

def create_permalink(string, separator = '-', max_size = 127)

    # words to ignore, usually the same words ignored by search engines
    ignore_words = ['a', 'an', 'the']
    ignore_re    = String.new
    
    # building ignore regexp
    ignore_words.each{ |word|
      ignore_re << word + '\b|\b'
    }
    ignore_re = '\b' + ignore_re + '\b'
    
    # replace apostrophes with separator
    permalink = string.gsub("'", separator)
    
    # delete ignore words
    permalink.gsub!(ignore_re, '')

    # all down
    permalink.downcase!

    # preserve alphanumerics, everything else becomes a separator
    permalink.gsub!(/[^a-z0-9]+/, separator)
    
    # enforce the maximum component length and return it
    permalink = permalink.to(max_size)
    
    # trim any leading or trailing separators
    return permalink.gsub(/^\\#{separator}+|\\#{separator}+$/, '')

end

Nice post slug

This code replaces accents to normal chars(e.g "á" => "a"), everything that isn´t in "a-zA-Z0-9" to "", multiples spaces to one space, and one space to "-".

This is useful to make URLs from titles, like Netscape.com does...

"A new report recommends only work 4 -6 hrs a day" => "A-new-report-recommends-only-work-4-6-hrs-a-day"

"Video: \"Popular Mechanics\" editor debunks 9/11 myths" => "Video-Popular-Mechanics-editor-debunks-911-myths"

etc..
The code is 95% based on http://textsnippets.com/posts/show/451

def self.nice_slug(str)
		
		accents = { 
		  ['á','à','â','ä','ã'] => 'a',
		  ['Ã','Ä','Â','À','�'] => 'A',
		  ['é','è','ê','ë'] => 'e',
		  ['Ë','É','È','Ê'] => 'E',
		  ['í','ì','î','ï'] => 'i',
		  ['�','Î','Ì','�'] => 'I',
		  ['ó','ò','ô','ö','õ'] => 'o',
		  ['Õ','Ö','Ô','Ò','Ó'] => 'O',
		  ['ú','ù','û','ü'] => 'u',
		  ['Ú','Û','Ù','Ü'] => 'U',
		  ['ç'] => 'c', ['Ç'] => 'C',
		  ['ñ'] => 'n', ['Ñ'] => 'N'
		  }
		accents.each do |ac,rep|
		  ac.each do |s|
			str = str.gsub(s, rep)
		  end
		end
		str = str.gsub(/[^a-zA-Z0-9 ]/,"")
		
		str = str.gsub(/[ ]+/," ")
		

		str = str.gsub(/ /,"-")
		
		#str = str.downcase

	end
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS