Sha256: edbd0c3b96695f3d765cc42306f30924b54833db604f7f8a6b9477e497d68061

Contents?: true

Size: 1.94 KB

Versions: 24

Compression:

Stored size: 1.94 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 feature
      resource.feature if resource.respond_to?(:feature)
    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(feature || resource)
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
decidim-core-0.10.1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.10.0 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.9.3 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.9.2 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.9.1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.9.0 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.8.4 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.8.3 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.8.2 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.8.1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.8.0 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.7.4 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.7.3 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.7.2 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.7.1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.7.0 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.6.8 app/presenters/decidim/resource_locator_presenter.rb
decidim-0.6.8 decidim-core/app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.6.7 app/presenters/decidim/resource_locator_presenter.rb
decidim-0.6.7 decidim-core/app/presenters/decidim/resource_locator_presenter.rb