Created
February 10, 2009 03:17
-
-
Save oddlyzen/61210 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ZepFrog | |
module DasPermalinkor | |
def self.included(base) | |
base.extend Permalinkor | |
end | |
module Permalinkor | |
def das_permalinked(options = {}) | |
options[:name] ||= :permalink | |
options[:source] ||= :title | |
unless included_modules.include? InstanceMethods | |
class_inheritable_accessor :options | |
extend ClassMethods | |
include InstanceMethods | |
end | |
self.options = options | |
end | |
end | |
module ClassMethods | |
def permalinked_find(key, *params) | |
self.send("find_by_#{options[:name].to_s}".to_sym, key, *params) | |
end | |
end | |
module InstanceMethods | |
def to_param | |
if self.send(options[:name]).nil? | |
self.das_permalink = self.send(options[:source]).slugify | |
self.save! | |
end | |
self.send options[:name] | |
end | |
def das_permalink | |
self.send options[:name] | |
end | |
def das_permalink=(val) | |
self[options[:name]] = val | |
end | |
end | |
end | |
end | |
class String | |
def slugify | |
self.gsub(/\'/, '').gsub(/[^a-z0-9]+/i, '-') | |
end | |
def slugify! | |
self.gsub!(/\'/, '').gsub(/[^a-z0-9]+/i, '-') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment