Class: Wallaby::ActiveRecord::ModelServiceProvider::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb

Overview

Normalize the values for a model

Instance Method Summary collapse

Constructor Details

#initialize(model_decorator) ⇒ Normalizer

Returns a new instance of Normalizer.

Parameters:

  • model_decorator (Wallaby::ModelDecorator)


9
10
11
# File 'lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb', line 9

def initialize(model_decorator)
  @model_decorator = model_decorator
end

Instance Method Details

#normalize(params) ⇒ Object

Parameters:

  • params (ActionController::Parameters)


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb', line 14

def normalize(params)
  params.each do |field_name, values|
     =
      @model_decorator.(field_name).presence ||
      @model_decorator.(field_name)
    type = [:type].try(:[], /range|point|binary/)
    next unless type

    try "normalize_#{type}_values", params, field_name, values
  end
  params
end

#normalize_binary_values(params, field_name, values) ⇒ Object

Turn values into binary

Parameters:

  • params (ActionController::Parameters)
  • field_name (String)
  • values (Object)


49
50
51
# File 'lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb', line 49

def normalize_binary_values(params, field_name, values)
  params[field_name] = (values.is_a?(::ActionDispatch::Http::UploadedFile) && values.read) || nil
end

#normalize_point_values(params, field_name, values) ⇒ Object

Turn values into points

Parameters:

  • params (ActionController::Parameters)
  • field_name (String)
  • values (Array)


40
41
42
43
# File 'lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb', line 40

def normalize_point_values(params, field_name, values)
  normalized = Array(values).map(&:presence).compact
  params[field_name] = (normalized.present? && values.map(&:to_f)) || nil
end

#normalize_range_values(params, field_name, values) ⇒ Object

Turn values into range

Parameters:

  • params (ActionController::Parameters)
  • field_name (String)
  • values (Array)


31
32
33
34
# File 'lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb', line 31

def normalize_range_values(params, field_name, values)
  normalized = Array(values).map(&:presence).compact
  params[field_name] = ((normalized.present? && values.length == 2) && (values.first...values.last)) || nil
end