class Form::MultiParameterAttributes::DateTimeParamsFilter

TODO: implement this with parse_filter, so we don't have to manually walk through the hash, etc.

Public Instance Methods

call(params) click to toggle source
# File lib/reform/form/multi_parameter_attributes.rb, line 4
def call(params)
  params = params.dup # DISCUSS: not sure if that slows down form processing?
  date_attributes = {}

  params.each do |attribute, value|
    if value.is_a?(Hash)
      params[attribute] = call(value) # TODO: #validate should only handle local form params.
    elsif matches = attribute.match(/^(\w+)\(.i\)$/)
      date_attribute = matches[1]
      date_attributes[date_attribute] = params_to_date(
        params.delete("#{date_attribute}(1i)"),
        params.delete("#{date_attribute}(2i)"),
        params.delete("#{date_attribute}(3i)"),
        params.delete("#{date_attribute}(4i)"),
        params.delete("#{date_attribute}(5i)")
      )
    end
  end

  date_attributes.each do |attribute, date|
    params[attribute] = date
  end
  params
end

Private Instance Methods

params_to_date(year, month, day, hour, minute) click to toggle source
# File lib/reform/form/multi_parameter_attributes.rb, line 30
def params_to_date(year, month, day, hour, minute)
  date_fields = [year, month, day].map!(&:to_i)
  time_fields = [hour, minute].map!(&:to_i)

  if date_fields.any?(&:zero?) || !Date.valid_date?(*date_fields)
    return nil
  end

  if hour.blank? && minute.blank?
    Date.new(*date_fields)
  else
    args = date_fields + time_fields
    Time.zone ? Time.zone.local(*args) :
      Time.new(*args)
  end
end