Module: MakePermalink::Base

Defined in:
lib/make_permalink/base.rb

Instance Method Summary collapse

Instance Method Details

#default_optionsObject



24
25
26
27
28
29
# File 'lib/make_permalink/base.rb', line 24

def default_options
  {
    :replace_non_ascii => true,
    :include_id => true
  }
end

Creates a permalink for a given object based on the method passed as parameter:

You can modify the default behavior with this two options

:include_id: This will include the objects id method. Default true. :replace_non_ascii: This will replace non-ascii-chars (like & and $) for their english words (and - dollars). Default true

class Post < ActiveRecord::Base
  make_permalink :title
end

p = Post.create(:title => "Rock & Roll!")
p.permalink   # => "1-rock-and-roll"


47
48
49
50
51
52
53
54
# File 'lib/make_permalink/base.rb', line 47

def make_permalink(method, options = {})
  define_method "permalink" do
    options = self.class.default_options.merge!(options)
    # Remember to remove the trailing dashes! That's the last gsub
    "#{self.create_permalink_prefix(options[:include_id]).to_s + 
      self.create_url_string(method, options[:replace_non_ascii]).to_s}".gsub(/-$/, "")
  end
end