Class: Decidim::SearchResourceFieldsMapper
- Inherits:
-
Object
- Object
- Decidim::SearchResourceFieldsMapper
- Defined in:
- lib/decidim/search_resource_fields_mapper.rb
Overview
A class with the responsibility to mapp 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 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}
26 27 28 29 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 26 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.
38 39 40 41 42 43 44 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 38 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.
47 48 49 50 51 52 53 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 47 def index_on_update?(searchable) if @conditions[:update].is_a?(Proc) @conditions[:update].call(searchable) else @conditions[:update] end end |
#mapped(resource) ⇒ Object
55 56 57 58 59 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 55 def mapped(resource) fields = map_common_fields(resource) fields[:i18n] = map_i18n_fields(resource) fields end |
#retrieve_organization(resource) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 61 def retrieve_organization(resource) if @declared_fields[:organization_id].present? organization_id = read_field(resource, @declared_fields, :organization_id) Decidim::Organization.find(organization_id) else participatory_space(resource).organization end end |
#set_index_condition(action, condition) ⇒ Object
33 34 35 |
# File 'lib/decidim/search_resource_fields_mapper.rb', line 33 def set_index_condition(action, condition) @conditions[action] = condition end |