Sha256: 5cdd18b7174fcae0606c915cf7d18d89f1558c1d68e7a707e11db74a8bcaef5f

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module Motr
  module Model
    
    module Sluggable #:nodoc:
      extend ActiveSupport::Concern
      
      included do
        callback_opt = (self.sluggable_on || "save")
        send(:"before_#{callback_opt.to_s}", :make_sluggable_fields)
      end
      
      # @private
      def make_sluggable_fields #:nodoc:
        [self.class.sluggable_fields].flatten.each do |atr|
          orig = self.send(:"#{atr}")
          self.send(:"#{atr}=", orig.to_slug)
        end
      end
      
      # @private
      module ClassMethods #:nodoc:
        Motr::Model.config(self, :sluggable, :fields, :on)
      end
      
    end
    
  end
end

##
# Original implementation by Ludo van den Boom's to_slug plugin 
# @see to_slug (https://github.com/ludo/to_slug)
#
module ToSlug #:nodoc:
  def to_slug
    value = self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').to_s
    value.gsub!(/[']+/, '')
    value.gsub!(/\W+/, ' ')
    value.strip!
    value.downcase!
    value.gsub!(' ', '-')
    value
  end
end
String.send(:include, ToSlug)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
motr-0.0.5 lib/motr/model/sluggable.rb
motr-0.0.4 lib/motr/model/sluggable.rb