Module: Decidim::ResourceHelper

Defined in:
app/helpers/decidim/resource_helper.rb

Overview

A Helper to render and link to resources.

Instance Method Summary collapse

Instance Method Details

#_decidim_resource_route(resource, route_type) ⇒ Object

Private: Build the route to a given resource.

Returns a String.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/decidim/resource_helper.rb', line 51

def _decidim_resource_route(resource, route_type)
  manifest = resource.class.resource_manifest
  engine = send(manifest.mounted_engine_name)

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

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

#decidim_resource_path(resource) ⇒ Object

Builds the path to a resource. Useful when linking to a resource from another engine.

resource - An object that is a valid resource exposed by some feature.

Returns a String.



11
12
13
# File 'app/helpers/decidim/resource_helper.rb', line 11

def decidim_resource_path(resource)
  _decidim_resource_route(resource, "path")
end

#decidim_resource_url(resource) ⇒ Object

Builds the url to a resource. Useful when linking to a resource from another engine.

resource - An object that is a valid resource exposed by some feature.

Returns a String.



21
22
23
# File 'app/helpers/decidim/resource_helper.rb', line 21

def decidim_resource_url(resource)
  _decidim_resource_route(resource, "url")
end

#linked_resources_for(resource, type, link_name) ⇒ Object

Renders a collection of linked resources for a resource.

resource - The resource to get the links from. type - The String type fo the resources we want to render. link_name - The String name of the link between the resources.

Example to render the proposals in a meeting view:

linked_resources_for(:meeting, :proposals, "proposals_from_meeting")

Returns nothing.



36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/decidim/resource_helper.rb', line 36

def linked_resources_for(resource, type, link_name)
  linked_resources = resource.linked_resources(type, link_name).group_by { |linked_resource| linked_resource.class.name }

  safe_join(linked_resources.map do |klass, resources|
    resource_manifest = klass.constantize.resource_manifest
    (:div, class: "section") do
      (:h3, I18n.t(resource_manifest.name, scope: "decidim.resource_links.#{link_name}"), class: "section-heading") +
        render(partial: resource_manifest.template, locals: { resources: resources })
    end
  end)
end