Sha256: cef885989992eaca625639356d8c5f8b95426a0ac94ad5f1ac5297a0b15949f0

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

class Wallaby::ActiveRecord::ModelOperator::Normalizer
  def initialize(model_decorator)
    @model_decorator = model_decorator
  end

  def normalize(params)
    params.each do |field_name, values|
      next unless metadata = @model_decorator.fields[field_name]

      case metadata[:type]
      when /range/
        normalize_range_values params, field_name, values
      when /point/
        normalize_point_values params, field_name, values
      when /binary/
        normalize_binary_values params, field_name, values
      end
    end
  end

  def normalize_range_values(params, field_name, values)
    normalized = Array(values).map(&:presence).compact
    if normalized.length > 0 && values.length == 2
      params[field_name] = values.first...values.last
    else
      params[field_name] = nil
    end
  end

  def normalize_point_values(params, field_name, values)
    normalized = Array(values).map(&:presence).compact
    if normalized.length > 0
      params[field_name] = values.map &:to_f
    else
      params[field_name] = nil
    end
  end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wallaby-4.1.6 lib/adaptors/wallaby/active_record/model_operator/normalizer.rb
wallaby-5.0.1 lib/adaptors/wallaby/active_record/model_operator/normalizer.rb