Sha256: 4f1248c69344d40c86fcd4c98ae6fe981e345bcc6ad819ffb27801494e4ca447

Contents?: true

Size: 849 Bytes

Versions: 3

Compression:

Stored size: 849 Bytes

Contents

class Permalink < ActiveRecord::Base
  
  belongs_to :content, :polymorphic => true
  before_save :validate_content
  
  validates_presence_of   :slug
  validates_uniqueness_of :slug
  validate :not_system_slug
  
  def to_param
    slug
  end
  
private

  # Validates that the slug is not routable to a controller other than PermalinksController
  def not_system_slug
    begin
      route = Rails.application.routes.recognize_path "/#{slug}"
      errors.add :slug, "is a reserved system route" unless route[:controller] == "permalinks"
    rescue ActionController::RoutingError
      # No route matches, so that's probably good
    end
  end

  def validate_content
    errors.add :content_id, "can't be blank" unless content_id.present?
    errors.add :content_type, "can't be blank" unless content_type.present?
    errors.empty?
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slug-engine-0.1.0 app/models/permalink.rb
slug-engine-0.0.3 app/models/permalink.rb
slug-engine-0.0.2 app/models/permalink.rb