Sha256: 6fb70b143563e7f637bdaf7aa6dd2a3c4074278bcfe97abf4e340a036a67b07b

Contents?: true

Size: 1.45 KB

Versions: 62

Compression:

Stored size: 1.45 KB

Contents

module Workarea
  module Navigation
    class Redirect
      include ApplicationDocument

      field :path, type: String
      field :destination, type: String

      index({ path: 1 }, { unique: true })
      index({ destination: 1 })

      validates :path, presence: true, uniqueness: true
      validates :destination, presence: true

      before_validation :sanitize_path

      def self.find_by_path(path)
        find_by(path: sanitize_path(path)) rescue nil
      end

      def self.find_destination(path)
        find_by_path(path).try(:destination)
      end

      def self.search(query)
        return all unless query.present?

        regex = /^\/?#{::Regexp.quote(query)}/
        any_of(
          { path:  regex },
          { destination: regex }
        )
      end

      def self.sanitize_path(path)
        encoded_path = if path =~ URI::ESCAPED
                         path
                       else
                         URI.encode(path)
                       end
        uri = URI.parse(encoded_path)

        result = uri.path
        result = "/#{result}" unless result.starts_with?('/')
        result = result[0..-2] if result.ends_with?('/')
        result = "#{result}?#{uri.query}" if uri.query.present?
        result
      end

      def self.sorts
        [Sort.newest, Sort.path, Sort.destination, Sort.modified]
      end

      private

      def sanitize_path
        self.path = self.class.sanitize_path(path)
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.26 app/models/workarea/navigation/redirect.rb
workarea-core-3.4.45 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.25 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.23 app/models/workarea/navigation/redirect.rb
workarea-core-3.4.44 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.22 app/models/workarea/navigation/redirect.rb
workarea-core-3.4.43 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.21 app/models/workarea/navigation/redirect.rb
workarea-core-3.4.42 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.20 app/models/workarea/navigation/redirect.rb
workarea-core-3.4.41 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.19 app/models/workarea/navigation/redirect.rb
workarea-core-3.4.40 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.18 app/models/workarea/navigation/redirect.rb
workarea-core-3.4.39 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.17 app/models/workarea/navigation/redirect.rb
workarea-core-3.4.38 app/models/workarea/navigation/redirect.rb
workarea-core-3.5.16 app/models/workarea/navigation/redirect.rb
workarea-core-3.4.37 app/models/workarea/navigation/redirect.rb