Class: Decidim::ResourceManifest

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

Overview

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

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

Example:

feature.register_resource do |resource|
  resource.model_class = Decidim::MyEngine::MyModel
  resource.template    = "decidim/myengine/myengine/linked_models"
end

Instance Method Summary collapse

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.



56
57
58
# File 'lib/decidim/resource_manifest.rb', line 56

def model_class
  model_class_name.constantize
end

#nameObject

The name of the resource we are exposing.

Returns a String.



63
64
65
# File 'lib/decidim/resource_manifest.rb', line 63

def name
  super || model_class_name.demodulize.underscore.pluralize.to_sym
end

#resource_scope(feature) ⇒ Object

Finds an ActiveRecord::Relation of the resource ‘model_class`, scoped to the given feature. This way you can find resources from another engine without actually coupling both engines.

feature - a Decidim::Feature

Returns an ActiveRecord::Relation.



45
46
47
48
49
50
# File 'lib/decidim/resource_manifest.rb', line 45

def resource_scope(feature)
  feature_ids = Decidim::Feature.where(participatory_space: feature.participatory_space, manifest_name: feature_manifest.name).pluck(:id)
  return model_class.none if feature_ids.empty?

  model_class.where(feature: feature_ids)
end

#route_nameObject

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

Returns a String.



70
71
72
# File 'lib/decidim/resource_manifest.rb', line 70

def route_name
  super || model_class_name.demodulize.underscore
end