Class: Blacklight::SearchParamsYamlCoder

Inherits:
Object
  • Object
show all
Defined in:
app/services/blacklight/search_params_yaml_coder.rb

Overview

This is a custom YAML coder for (de)serializing blacklight search parameters that supports deserializing HashWithIndifferentAccess parameters (as was historically done by Blacklight).

Class Method Summary collapse

Class Method Details

.dump(obj) ⇒ Object

Serializes an attribute value to a string that will be stored in the database.



8
9
10
11
12
13
# File 'app/services/blacklight/search_params_yaml_coder.rb', line 8

def self.dump(obj)
  # Convert HWIA to an ordinary hash so we have some hope of using the regular YAML encoder in the future
  obj = obj.to_hash if obj.is_a?(ActiveSupport::HashWithIndifferentAccess)

  YAML.dump(obj)
end

.load(yaml) ⇒ Object

Deserializes a string from the database to an attribute value.



16
17
18
19
20
21
22
# File 'app/services/blacklight/search_params_yaml_coder.rb', line 16

def self.load(yaml)
  return yaml unless yaml.is_a?(String) && yaml.start_with?("---")

  params = yaml_load(yaml)

  params.with_indifferent_access
end

.yaml_load(payload) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'app/services/blacklight/search_params_yaml_coder.rb', line 26

def self.yaml_load(payload)
  if ActiveRecord.try(:use_yaml_unsafe_load) || ActiveRecord::Base.try(:use_yaml_unsafe_load)
    YAML.unsafe_load(payload)
  else
    permitted_classes = (ActiveRecord.try(:yaml_column_permitted_classes) || ActiveRecord::Base.try(:yaml_column_permitted_classes) || []) +
                        Blacklight::Engine.config.blacklight.search_params_permitted_classes
    YAML.safe_load(payload, permitted_classes: permitted_classes, aliases: true)
  end
end