Sha256: 1b8d757827d629b1b537efdb4fac801d56cf630a1c814e7e9e31ad7ddd86e890

Contents?: true

Size: 1.81 KB

Versions: 30

Compression:

Stored size: 1.81 KB

Contents

module Scrivito

class Route

  VALID_ROUTE_NAMES = %i(homepage slug_id permalink).freeze

  def self.register(route_set, name)
    assert_valid_route_name(name)
    registry(route_set)[name] = new(name)
  end

  def self.find(given_context, name)
    available_contexts(given_context).each do |context|
      route = registry(context._routes)[name]

      if route && route.defined_in?(context)
        return route.apply_to(context)
      end
    end

    nil
  end

  private_class_method def self.registry(route_set)
    @registry ||= {}
    @registry[route_set] ||= {}
  end

  private_class_method def self.assert_valid_route_name(name)
    unless VALID_ROUTE_NAMES.include?(name)
      valid_values = VALID_ROUTE_NAMES.join(', ')
      raise ScrivitoError,
          %("#{name}" is not a valid scrivito route name. Valid values are: #{valid_values}.)
    end
  end

  private_class_method def self.available_contexts(context)
    [
      context,
      context.try(:main_app)
    ].compact
  end

  def initialize(name)
    @name = name
  end

  def helper_name
    # use a random name to prevent users from getting the idea
    # that the internally unsed routing helpers might be a stable API
    @helper_name ||=
      begin
        random_segment = Digest::SHA1.hexdigest(@name.to_s)[0..15]
        "scrivito_#{random_segment}"
      end
  end

  def defined_in?(context)
    context.respond_to?("#{helper_name}_path")
  end

  def apply_to(context)
    RouteApplication.new(self, context)
  end

  class RouteApplication < Struct.new(:route, :context)
    def generate_path(options = {})
      generate(:path, options)
    end

    def generate_url(options = {})
      generate(:url, options)
    end

    def generate(path_or_url, options = {})
      context.public_send("#{route.helper_name}_#{path_or_url}", options)
    end
  end

end

end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
scrivito_sdk-1.18.0 app/cms/scrivito/route.rb
scrivito_sdk-1.18.0.rc1 app/cms/scrivito/route.rb
scrivito_sdk-1.17.0 app/cms/scrivito/route.rb
scrivito_sdk-1.17.0.rc3 app/cms/scrivito/route.rb
scrivito_sdk-1.17.0.rc2 app/cms/scrivito/route.rb
scrivito_sdk-1.17.0.rc1 app/cms/scrivito/route.rb
scrivito_sdk-1.16.0 app/cms/scrivito/route.rb
scrivito_sdk-1.16.0.rc2 app/cms/scrivito/route.rb
scrivito_sdk-1.16.0.rc1 app/cms/scrivito/route.rb
scrivito_sdk-1.15.0 app/cms/scrivito/route.rb
scrivito_sdk-1.15.0.rc2 app/cms/scrivito/route.rb
scrivito_sdk-1.15.0.rc1 app/cms/scrivito/route.rb
scrivito_sdk-1.14.0 app/cms/scrivito/route.rb
scrivito_sdk-1.14.0.rc3 app/cms/scrivito/route.rb
scrivito_sdk-1.14.0.rc2 app/cms/scrivito/route.rb
scrivito_sdk-1.14.0.rc1 app/cms/scrivito/route.rb
scrivito_sdk-1.13.0 app/cms/scrivito/route.rb
scrivito_sdk-1.13.0.rc2 app/cms/scrivito/route.rb
scrivito_sdk-1.13.0.rc1 app/cms/scrivito/route.rb
scrivito_sdk-1.12.0 app/cms/scrivito/route.rb