module Decidim::Resourceable

A concern with the components needed when you want a model to be able to create links from it to another resource.

Public Instance Methods

allow_resource_permissions?() click to toggle source

Public: Whether the permissions for this object actions can be set at resource level.

# File lib/decidim/resourceable.rb, line 113
def allow_resource_permissions?
  false
end
linked_classes_for(component) click to toggle source

Finds the name of the classes that have been linked to this model for the given `component`.

component - a Decidim::Component instance where the links will be scoped to.

Returns an Array of Strings.

# File lib/decidim/resourceable.rb, line 142
def linked_classes_for(component)
  scope = where(component: component)

  from = scope
         .joins(:resource_links_from)
         .where(decidim_resource_links: { from_type: name })

  to = scope
       .joins(:resource_links_to)
       .where(decidim_resource_links: { to_type: name })

  ResourceLink
    .where(from: from)
    .or(ResourceLink.where(to: to))
    .pluck(:from_type, :to_type)
    .flatten
    .uniq
    .reject { |k| k == name }
end
linked_resources(resource_name, link_name) click to toggle source

Finds all the linked resources to or from this model for a given resource name and link name.

resource_name - The String name of the resource manifest exposed by a component. link_name - The String name of the link between this model and the target resource.

Returns an ActiveRecord::Relation.

# File lib/decidim/resourceable.rb, line 28
def linked_resources(resource_name, link_name)
  scope = sibling_scope(resource_name)

  from = scope
         .joins(:resource_links_from)
         .where(decidim_resource_links: { name: link_name, to_id: id, to_type: self.class.name })

  to = scope
       .joins(:resource_links_to)
       .where(decidim_resource_links: { name: link_name, from_id: id, from_type: self.class.name })

  scope.where(id: from).or(scope.where(id: to))
end
permissions() click to toggle source

Public: Returns permissions for this object actions if they can be set at resource level.

# File lib/decidim/resourceable.rb, line 118
def permissions
  resource_permission&.permissions if allow_resource_permissions?
end
resource_description() click to toggle source

Public: This method will be used to represent this resource in other contexts, like cards or search results.

# File lib/decidim/resourceable.rb, line 130
def resource_description
  try(:description) || try(:body) || try(:content)
end
resource_manifest() click to toggle source

Finds the resource manifest for the model.

Returns a Decidim::ResourceManifest

# File lib/decidim/resourceable.rb, line 165
def resource_manifest
  Decidim.find_resource_manifest(self)
end
resource_title() click to toggle source

Public: This method will be used to represent this resource in other contexts, like cards or search results.

# File lib/decidim/resourceable.rb, line 124
def resource_title
  try(:title) || try(:name)
end
resource_visible?() click to toggle source

Check only the resource visibility not its hierarchy. This method is intended to be overriden by classes that include this module and have the need to impose its own visibility rules.

@return If the resource is also Publicable checks if the resource is published, otherwise returns true by default.

# File lib/decidim/resourceable.rb, line 104
def resource_visible?
  return !hidden? && published? if respond_to?(:hidden?) && respond_to?(:published?)
  return published? if respond_to?(:published?)
  return !hidden? if respond_to?(:hidden?)

  true
end
sibling_scope(resource_name) click to toggle source

Builds an ActiveRecord::Relation in order to load all the resources that are in the same parent as this model.

resource_name - The String name of the resource manifest exposed by a component.

Returns an ActiveRecord::Relation.

# File lib/decidim/resourceable.rb, line 48
def sibling_scope(resource_name)
  manifest = Decidim.find_resource_manifest(resource_name)
  return self.class.none unless manifest

  scope = manifest.resource_scope(component)
  scope = scope.where("#{self.class.table_name}.id != ?", id) if manifest.model_class == self.class
  scope.includes(:component).where.not(decidim_components: { published_at: nil })
end
visible?() click to toggle source

Checks throughout all its parent hierarchy if this Resource should be visible in the public views. i.e. checks

  • the visibility of its parent Component

  • the visibility of its participatory space.

  • the visibility of the resource itself.

# File lib/decidim/resourceable.rb, line 95
def visible?
  component.participatory_space.try(:visible?) && component.published? && resource_visible?
end