Class: Decidim::SearchResourceFieldsMapper
- Inherits:
-
Object
- Object
- Decidim::SearchResourceFieldsMapper
- Includes:
- ActionView::Helpers::SanitizeHelper
- Defined in:
- lib/decidim/search_resource_fields_mapper.rb
Overview
A class with the responsibility to map fields between a Searchable and a SearchableResource.
Instance Method Summary collapse
-
#index_on_create?(searchable) ⇒ Boolean
Checks for the current searchable if it must be indexed when it is created or not.
-
#index_on_update?(searchable) ⇒ Boolean
Checks for the current searchable if it must be indexed when it is updated or not.
-
#initialize(declared_fields) ⇒ SearchResourceFieldsMapper
constructor
Declared fields may be of types: - Hash for deep associations.
- #mapped(resource) ⇒ Object
- #retrieve_organization(resource) ⇒ Object
- #set_index_condition(action, condition) ⇒ Object
Constructor Details
#initialize(declared_fields) ⇒ SearchResourceFieldsMapper
Declared fields may be of types:
-
Hash for deep associations.
-
Array each element should be a text field symbol, all values will be concatenated.
-
Symbol when mapping is direct.
-
scope_id: The field where the scope is set in the model, if any.
-
participatory_space: The field where the ParticipatorySpace is set in the model.
-
datetime: The field that describes where in time the model is placed.
-
A, B, C, D: Weighted text fields.
Example value for declared_fields param: {scope_id: :decidim_scope_id, participatory_space: { component: :participatory_space }, A: :title, D: [:description, :address], datetime: :start_time}
28 29 30 31 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 28 def initialize(declared_fields) @declared_fields = declared_fields.with_indifferent_access @conditions = { create: true, update: true } end |
Instance Method Details
#index_on_create?(searchable) ⇒ Boolean
Checks for the current searchable if it must be indexed when it is created or not.
40 41 42 43 44 45 46 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 40 def index_on_create?(searchable) if @conditions[:create].is_a?(Proc) @conditions[:create].call(searchable) else @conditions[:create] end end |
#index_on_update?(searchable) ⇒ Boolean
Checks for the current searchable if it must be indexed when it is updated or not.
49 50 51 52 53 54 55 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 49 def index_on_update?(searchable) if @conditions[:update].is_a?(Proc) @conditions[:update].call(searchable) else @conditions[:update] end end |
#mapped(resource) ⇒ Object
57 58 59 60 61 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 57 def mapped(resource) fields = map_common_fields(resource) fields[:i18n] = map_i18n_fields(resource) fields end |
#retrieve_organization(resource) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 63 def retrieve_organization(resource) if @declared_fields[:organization_id].present? organization_id = read_field(resource, @declared_fields, :organization_id) Decidim::Organization.find_by(id: organization_id) else participatory_space(resource)&.organization end end |
#set_index_condition(action, condition) ⇒ Object
35 36 37 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 35 def set_index_condition(action, condition) @conditions[action] = condition end |