Sha256: 84c4d5b1ff600e41efc247bdbad4da34c7cd015b5bfe0369b0cfb2b0560d8bdc

Contents?: true

Size: 1.05 KB

Versions: 11

Compression:

Stored size: 1.05 KB

Contents

module SiteLogic

  module Base

    @@sluggable_attribute = nil

    module ClassMethods
      def has_slug(attr)
        self.send(:set_callback, :save, :before, Proc.new{|doc| doc.make_slug})
      end
    end

    def self.included(base)
      base.extend(ClassMethods)
    end

    def make_slug
      if self.desired_slug && ! self.desired_slug.blank?
        text = self.desired_slug
      elsif self.slug
        text = self.slug
      elsif self.respond_to?(:page_title)
        text = self.page_title.to_s.downcase
      elsif self.respond_to?(:name)
        text = self.name.to_s.downcase
      end

      # Translation borrowed from permalink_fu
      text = text.to_s
      text.gsub!(/[^\x00-\x7F]+/, '-')    # Remove anything non-ASCII entirely (e.g. diacritics).
      text.gsub!(/[^\/\w_ \-]+/i,   '-')  # Remove unwanted chars.
      text.gsub!(/[ \-]+/i,      '-')     # No more than one of the separator in a row.
      text.gsub!(/^\-|\-$/i,      '')     # Remove leading/trailing separator.
      text.downcase!
      self.slug = text

    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
site_logic-1.9.2 lib/site_logic/base.rb
site_logic-1.9.1 lib/site_logic/base.rb
site_logic-1.9.0 lib/site_logic/base.rb
site_logic-1.8.0 lib/site_logic/base.rb
site_logic-1.7.1 lib/site_logic/base.rb
site_logic-1.7.0 lib/site_logic/base.rb
site_logic-1.6.10 lib/site_logic/base.rb
site_logic-1.6.9 lib/site_logic/base.rb
site_logic-1.6.8 lib/site_logic/base.rb
site_logic-1.6.7 lib/site_logic/base.rb
site_logic-1.6.6 lib/site_logic/base.rb