Sha256: cd60142a016ec7a41b30e142dd72550c26b21c265aec790908671d8426890a93

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

# A Slug is a unique, human-friendly identifier for an ActiveRecord.
class Slug < ::ActiveRecord::Base

  table_name = "slugs"
  belongs_to :sluggable, :polymorphic => true
  before_save :enable_name_reversion, :set_sequence
  validate :validate_name
  send FriendlyId::AcktiveRecord::Compat.scope_method, :similar_to, lambda {|slug| {:conditions => {
        :name           => slug.name,
        :scope          => slug.scope,
        :sluggable_type => slug.sluggable_type
      },
      :order => "sequence ASC"
    }
  }

  # Whether this slug is the most recent of its owner's slugs.
  def current?
    sluggable.slug == self
  end

  def outdated?
    !current?
  end

  def to_friendly_id
    sequence > 1 ? friendly_id_with_sequence : name
  end

  # Raise a FriendlyId::SlugGenerationError if the slug name is blank.
  def validate_name
    if name.blank?
      raise FriendlyId::BlankError.new("slug.name can not be blank.")
    end
  end

  private

  # If we're renaming back to a previously used friendly_id, delete the
  # slug so that we can recycle the name without having to use a sequence.
  def enable_name_reversion
    sluggable.slugs.find_all_by_name_and_scope(name, scope).each { |slug| slug.destroy }
  end

  def friendly_id_with_sequence
    "#{name}#{separator}#{sequence}"
  end

  def similar_to_other_slugs?
    !similar_slugs.empty?
  end

  def similar_slugs
    self.class.similar_to(self)
  end

  def separator
    sluggable.friendly_id_config.sequence_separator
  end

  def set_sequence
    return unless new_record?
    self.sequence = similar_slugs.last.sequence.succ if similar_to_other_slugs?
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
friendly_id-3.0.0.beta3 lib/friendly_id/acktive_record/slug.rb
friendly_id-3.0.0.beta2 lib/friendly_id/acktive_record/slug.rb
friendly_id-3.0.0.beta1 lib/friendly_id/acktive_record/slug.rb