Sha256: 51eabdea577319094c4ccc889bae54e9e014e23c46a89e1e4a9a4cfe6295e8d2

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

$:.unshift(File.dirname(__FILE__)) unless
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

module SluggableFinder
  
  @@not_found_exception = nil
  
  def self.not_found_exception=(ex)
    @@not_found_exception = ex
  end
  
  def self.not_found_exception
    @@not_found_exception || ActiveRecord::RecordNotFound
  end
  
  class << self
    
    def enable_activerecord
      ActiveRecord::Base.extend SluggableFinder::Orm::ClassMethods
      # support for associations
      a = ActiveRecord::Associations
      Array(ActiveRecord::Associations::AssociationCollection.tap { |classes|
        # detect http://dev.rubyonrails.org/changeset/9230
        unless a::HasManyThroughAssociation.superclass == a::HasManyAssociation
          classes << a::HasManyThroughAssociation
        end
      }).each do |klass|
        klass.send :include, SluggableFinder::Finder
        klass.send :include, SluggableFinder::AssociationProxyFinder
        klass.alias_method_chain :find, :slug
      end
      
    end
    
  end
  
  def self.encode(str)
    if defined?(ActiveSupport::Inflector.parameterize)
      ActiveSupport::Inflector.parameterize(str).to_s
    else
      ActiveSupport::Multibyte::Handlers::UTF8Handler.
      		normalize(str,:d).split(//u).reject { |e| e.length > 1 }.join.strip.gsub(/[^a-z0-9]+/i, '-').downcase.gsub(/-+$/, '') 
    end
  end
  
  def self.random_slug_for(klass)
    Digest::SHA1.hexdigest( klass.name.to_s + Time.now.to_s )
  end
  
end

require 'rubygems'
require 'active_record'
require 'digest/sha1'

Dir.glob(File.dirname(__FILE__)+'/sluggable_finder/*.rb').each do |file|
  require file
end

SluggableFinder.enable_activerecord if (defined?(ActiveRecord) && !ActiveRecord::Base.respond_to?(:sluggable_finder))

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sluggable_finder-2.3.5 lib/sluggable_finder.rb
sluggable_finder-2.3.4 lib/sluggable_finder.rb
sluggable_finder-2.3.3 lib/sluggable_finder.rb
sluggable_finder-2.3.2 lib/sluggable_finder.rb