Sha256: 72adc46245bb3f76131f6d55b88d619d7d98398486c5e6c06e44bd983307b5cf

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

module EditorialLogic
  require 'editorial_logic/railtie.rb' if defined?(Rails)
  require 'editorial_logic/engine.rb' if defined?(Rails)

  def self.setup
    yield self
  end

  def self.filter_time(attributes, name)
     attrs = attributes.collect do |key, value|
       if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
         [$1.to_i, value.send("to_#$2")]
       end
     end.compact.sort_by(&:first).map(&:last)
     Time.zone.local(*attrs) unless attrs.empty?
  end

  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?(:headline)
        text = self.headline.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

5 entries across 5 versions & 1 rubygems

Version Path
editorial_logic-1.3.3 lib/editorial_logic.rb
editorial_logic-1.3.2 lib/editorial_logic.rb
editorial_logic-1.3.1 lib/editorial_logic.rb
editorial_logic-1.3.0 lib/editorial_logic.rb
editorial_logic-1.1.6 lib/editorial_logic.rb