Module: ForestLiana::DecorationHelper

Defined in:
app/helpers/forest_liana/decoration_helper.rb

Class Method Summary collapse

Class Method Details

.decorate_for_search(records_serialized, field_names, search_value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/forest_liana/decoration_helper.rb', line 14

def self.decorate_for_search(records_serialized, field_names, search_value)
  match_fields = {}
  records_serialized['data'].each_with_index do |record, index|
    field_names.each do |field_name|
      value = record['attributes'][field_name]
      if value
        detect_match_and_decorate(record, index, field_name, value, search_value, match_fields)
      end
    end

    detect_match_and_decorate(record, index, 'id', record['id'], search_value, match_fields)
  end
  match_fields.empty? ? nil : match_fields
end

.detect_match_and_decorate(record, index, field_name, value, search_value, match_fields) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'app/helpers/forest_liana/decoration_helper.rb', line 3

def self.detect_match_and_decorate record, index, field_name, value, search_value, match_fields
  begin
    match = value.match(/#{search_value}/i)
    if match
      match_fields[index] = { id: record['id'], search: [] } if match_fields[index].nil?
      match_fields[index][:search] << field_name
    end
  rescue
  end
end