Skip to content

Instantly share code, notes, and snippets.

@oddlyzen
Created February 10, 2009 03:17
Show Gist options
  • Save oddlyzen/61210 to your computer and use it in GitHub Desktop.
Save oddlyzen/61210 to your computer and use it in GitHub Desktop.
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