Sha256: a56c69488275f1039d48992f3069236a141490acca54e1cbc154f82280e3fd8b

Contents?: true

Size: 862 Bytes

Versions: 28

Compression:

Stored size: 862 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

28 entries across 28 versions & 1 rubygems

Version Path
pages_core-3.9.2 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.9.1 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.9.0 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.8.3 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.8.2 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.8.1 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.8.0 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.7.0 app/models/concerns/pages_core/page_model/redirectable.rb