Class: Decidim::SearchResourceFieldsMapper

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
decidim-core/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

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 setted in the model, if any.

  • participatory_space: The field where the ParticipatorySpace is setted 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}

Parameters:

  • declared_fields:

    A Hash with the mappings between a SearchableResource attributes and any given model. Mapped fields are:


28
29
30
31
# File 'decidim-core/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.

Returns:

  • (Boolean)

40
41
42
43
44
45
46
# File 'decidim-core/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.

Returns:

  • (Boolean)

49
50
51
52
53
54
55
# File 'decidim-core/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 'decidim-core/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 'decidim-core/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

Parameters:

  • action:

    currently supports :create, :update

  • condition:

    a boolean or a Proc that will receive the Searchable and will return a boolean.


35
36
37
# File 'decidim-core/lib/decidim/search_resource_fields_mapper.rb', line 35

def set_index_condition(action, condition)
  @conditions[action] = condition
end