Class: Decidim::ResourceManifest

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, AttributeObject::Model
Defined in:
decidim-core/lib/decidim/resource_manifest.rb

Overview

Components inside a component can expose different Resources, these resources will be used to be linked between each other and other possible components.

This class sets a scheme to expose these resources. It shouldn't be used directly, you should use `register_resource` inside a component.

Example:

component.register_resource(:my_model) do |resource|
  resource.model_class = Decidim::MyEngine::MyModel
  resource.template    = "decidim/myengine/myengine/linked_models"
end

Constant Summary

Constants included from AttributeObject::TypeMap

AttributeObject::TypeMap::Boolean, AttributeObject::TypeMap::Decimal

Instance Method Summary collapse

Methods included from AttributeObject::Model

#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h

Instance Method Details

#model_classObject

Finds the current class with the given `model_class_name` in order to avoid problems with Rails' autoloading.

Returns a class.


83
84
85
# File 'decidim-core/lib/decidim/resource_manifest.rb', line 83

def model_class
  model_class_name.constantize
end

#permissions_classObject

Public: Finds the permission class from its name, using the `permissions_class_name` attribute. If the class does not exist, it raises an exception. If the class name is not set, it returns nil.

Returns a Class.


92
93
94
# File 'decidim-core/lib/decidim/resource_manifest.rb', line 92

def permissions_class
  permissions_class_name&.constantize
end

#resource_scope(component) ⇒ Object

Finds an ActiveRecord::Relation of the resource `model_class`, scoped to the given component. This way you can find resources from another engine without actually coupling both engines. If no `component_manifest` is set for this manifest, it returns an empty collection.

component - a Decidim::Component

Returns an ActiveRecord::Relation.


70
71
72
73
74
75
76
77
# File 'decidim-core/lib/decidim/resource_manifest.rb', line 70

def resource_scope(component)
  return model_class.none unless component_manifest

  component_ids = Decidim::Component.where(participatory_space: component.participatory_space, manifest_name: component_manifest.name).pluck(:id)
  return model_class.none if component_ids.empty?

  model_class.joins(:component).where(decidim_components: { id: component_ids })
end

#route_nameObject

The name of the named Rails route to create the url to the resource.

Returns a String.


99
100
101
# File 'decidim-core/lib/decidim/resource_manifest.rb', line 99

def route_name
  super || model_class_name.demodulize.underscore
end