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.15.4 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.15.3 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.15.2 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.15.1 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.14.0 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.13.0 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.12.7 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.12.6 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.12.5 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.12.4 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.12.3 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.12.2 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.12.1 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.12.0 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.11.3 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.11.2 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.11.1 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.11.0 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.10.2 app/models/concerns/pages_core/page_model/redirectable.rb
pages_core-3.10.1 app/models/concerns/pages_core/page_model/redirectable.rb