Sha256: e2f589ba41f83d819a9d77d68666b28a73864e2006bc1ca3bc31c760e0c8130b

Contents?: true

Size: 1.95 KB

Versions: 10

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module Decidim
  # A presenter to get the url or path from a resource.
  class ResourceLocatorPresenter
    def initialize(resource)
      @resource = resource
    end

    attr_reader :resource

    # Builds the path to the resource. Useful when linking to a resource from
    # another engine.
    #
    # options - An optional hash of options to pass to the Rails router
    #
    # Returns a String.
    def path(options = {})
      member_route("path", options)
    end

    # Builds the url to the resource. Useful when linking to a resource from
    # another engine.
    #
    # options - An optional hash of options to pass to the Rails router
    #
    # Returns a String.
    def url(options = {})
      member_route("url", options.merge(host: resource.organization.host))
    end

    # Builds the index path to the associated collection of resources.
    #
    # options - An optional hash of options to pass to the Rails router
    #
    # Returns a String.
    def index(options = {})
      collection_route("path", options)
    end

    private

    # Private: Build the route to the resource.
    #
    # Returns a String.
    def member_route(route_type, options)
      route_proxy.send("#{member_route_name}_#{route_type}", resource, options)
    end

    # Private: Build the route to the associated collection of resources.
    #
    # Returns a String.
    def collection_route(route_type, options)
      route_proxy.send("#{collection_route_name}_#{route_type}", options)
    end

    def manifest
      resource.class.try(:resource_manifest) ||
        resource.class.try(:participatory_space_manifest)
    end

    def component
      resource.component if resource.respond_to?(:component)
    end

    def member_route_name
      manifest.route_name
    end

    def collection_route_name
      member_route_name.pluralize
    end

    def route_proxy
      @route_proxy ||= EngineRouter.main_proxy(component || resource)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
decidim-core-0.13.1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.12.2 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.13.0 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.12.1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.13.0.pre1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.12.0 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.11.2 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.12.0.pre app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.11.1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.11.0.pre1 app/presenters/decidim/resource_locator_presenter.rb