Sha256: f024b8b13cd5499a91598097a0358c91a147646b422578ba66be32992a1be40f

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

module Slugs
  module ActiveRecord
    module Base
      extend ActiveSupport::Concern

      def to_param
        if self.class.sluggable?
          if slug_changed?
            slug_was
          else
            slug
          end
        else
          super
        end
      end

      protected

      def generate_slug
        unless slug_changed?
          options = self.class.slug
          case options
          when Symbol
            value = send(options)
          when Array
            value = options.each.map{ |p| send(p) }.join(' ')
          else
            value = options.call(self) if options.respond_to? :call
          end
          value = value.to_s.parameterize
          if value.present? and value != slug
            previous_value = self.class.unscoped.find_previous_slug(value)
            if previous_value.present?
              index = previous_value.match(/#{value}-([0-9]+)$/)
              if index.present?
                value << "-#{index[1].to_i + 1}"
              else
                value << '-1'
              end
            end
            self.slug = value
          end
        end
      end

      module ClassMethods

        attr_accessor :slug

        def inherited(subclass)
          subclass.slug = slug
          super
        end

        def sluggable?
          slug.present?
        end

        def has_slug(*args, &block)
          unless sluggable?
            if try(:translatable?)
              include Slugs::ActiveRecord::Translatable
              attr_translatable :slug
              before_validation :generate_slugs
            else
              include Slugs::ActiveRecord::NonTranslatable
              before_validation :generate_slug
            end
            include Slugs::ActiveRecord::Finders
            if block_given?
              self.slug = block
            else
              self.slug = args.size == 1 ? args[0] : args
            end
          end
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slugs-1.3.2 lib/slugs/active_record/base.rb
slugs-1.3.1 lib/slugs/active_record/base.rb
slugs-1.3.0 lib/slugs/active_record/base.rb