Sha256: c5853ec098bb1955e17a6700a174e752c2cb25d33703512267c5e659232b9ddd

Contents?: true

Size: 834 Bytes

Versions: 1

Compression:

Stored size: 834 Bytes

Contents

# frozen_string_literal: true

module PagesCore
  module PageModel
    class InvalidRedirectUrl < StandardError; end

    module Redirectable
      extend ActiveSupport::Concern

      included do
        validates(:redirect_to,
                  format: { with: %r{\A(/|https?://).+\z}, allow_blank: true })
      end

      # Returns boolean true if page has a valid redirect
      def redirects?
        redirect_to?
      end

      def redirect_path(params = {})
        path = redirect_to.dup
        if path.start_with? "/"
          params.each do |key, value|
            unless value.is_a?(String) || value.is_a?(Symbol)
              raise InvalidRedirectUrl, "must be a string or a symbol"
            end

            path.gsub!("/:#{key}", "/#{value}")
          end
        end
        path
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.15.5 app/models/concerns/pages_core/page_model/redirectable.rb