Sha256: c9110d95f3004f75957561e519fbb9d1869c48f71b76443a9d5fe0f888002060

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

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

    # Builds the path to a 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 = {})
      _route(@resource, "path", options)
    end

    # Builds the url to a 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 = {})
      _route(@resource, "url", options.merge(host: @resource.organization.host))
    end

    private

    # Private: Build the route to a given resource.
    #
    # Returns a String.
    def _route(resource, route_type, options)
      manifest = resource.class.resource_manifest
      engine = manifest.feature_manifest.engine

      url_params = {
        id: resource.id,
        feature_id: resource.feature.id,
        participatory_process_id: resource.feature.participatory_process.id
      }

      engine.routes.url_helpers.send("#{manifest.route_name}_#{route_type}", url_params.merge(options))
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-core-0.4.2 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.4.1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.4.3 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.4.0 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.3.2 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.3.1 app/presenters/decidim/resource_locator_presenter.rb
decidim-core-0.3.0 app/presenters/decidim/resource_locator_presenter.rb