Class: Decidim::SearchResourceFieldsMapper

Inherits:
Object
  • Object
show all
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

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}

Parameters:

  • declared_fields (Hash)
    • A Hash with the mappings between a SearchableResource attributes and

    any given model. Mapped fields are:



27
28
29
30
# File 'lib/decidim/search_resource_fields_mapper.rb', line 27

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)


39
40
41
42
43
44
45
# File 'lib/decidim/search_resource_fields_mapper.rb', line 39

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)


48
49
50
51
52
53
54
# File 'lib/decidim/search_resource_fields_mapper.rb', line 48

def index_on_update?(searchable)
  if @conditions[:update].is_a?(Proc)
    @conditions[:update].call(searchable)
  else
    @conditions[:update]
  end
end

#mapped(resource) ⇒ Object



56
57
58
59
60
# File 'lib/decidim/search_resource_fields_mapper.rb', line 56

def mapped(resource)
  fields = map_common_fields(resource)
  fields[:i18n] = map_i18n_fields(resource)
  fields
end

#retrieve_organization(resource) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/decidim/search_resource_fields_mapper.rb', line 62

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 (Symbol)

    currently supports :create, :update

  • condition (TrueClass, FalseClass)

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



34
35
36
# File 'lib/decidim/search_resource_fields_mapper.rb', line 34

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