Sha256: 40661585ce6c2a9f870081b9fa4a87fd4aabfbe68dc6a479700bdc95bc5eda95

Contents?: true

Size: 869 Bytes

Versions: 22

Compression:

Stored size: 869 Bytes

Contents

# frozen_string_literal: true

class UrlRewrite < ApplicationRecord
  StatusCode = Struct.new(:code, :label)

  STATUS_CODES = [
    StatusCode.new(303, "See other"),
    StatusCode.new(307, "Temporary redirect"),
    StatusCode.new(308, "Permanent redirect"),
  ].freeze

  enum status_code: STATUS_CODES.map(&:code).index_by(&:itself)

  attribute :status_code, :integer, default: 303

  validates :from, :to, presence: true
  validates :from, format: { with: %r{\A/.*\z}, message: "should start with /" } # rubocop:disable Rails/I18nLocaleTexts

  scope :active, -> { where(active: true) }
  scope :alphabetical, -> { order(from: :asc) }

  def to_s
    "#{from} -> #{to}"
  end

  scope :admin_search, ->(query) do
    where(arel_table[:from].matches("%#{query}%").or(arel_table[:to].matches("%#{query}%")))
  end

  def title
    from.delete_prefix("/")
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
katalyst-koi-4.12.6 app/models/url_rewrite.rb
katalyst-koi-4.12.5 app/models/url_rewrite.rb
katalyst-koi-4.12.4 app/models/url_rewrite.rb
katalyst-koi-4.12.3 app/models/url_rewrite.rb
katalyst-koi-4.12.2 app/models/url_rewrite.rb
katalyst-koi-4.12.1 app/models/url_rewrite.rb
katalyst-koi-4.12.0 app/models/url_rewrite.rb
katalyst-koi-4.11.2 app/models/url_rewrite.rb
katalyst-koi-4.11.1 app/models/url_rewrite.rb
katalyst-koi-4.11.0 app/models/url_rewrite.rb
katalyst-koi-4.10.3 app/models/url_rewrite.rb
katalyst-koi-4.10.2 app/models/url_rewrite.rb
katalyst-koi-4.10.1 app/models/url_rewrite.rb
katalyst-koi-4.10.0 app/models/url_rewrite.rb
katalyst-koi-4.9.5 app/models/url_rewrite.rb
katalyst-koi-4.9.4 app/models/url_rewrite.rb
katalyst-koi-4.9.3 app/models/url_rewrite.rb
katalyst-koi-4.9.2 app/models/url_rewrite.rb
katalyst-koi-4.9.1 app/models/url_rewrite.rb
katalyst-koi-4.9.0 app/models/url_rewrite.rb