Sha256: 52701af779fa12264e9c270a3e194f286ce5a9ad438c69af8ffd6f60170c0c6b

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

class Reform::Form
  module MultiParameterAttributes
    class DateParamsFilter
      def call(params)
        date_attributes = {}

        params.each do |attribute, value|
          if value.is_a?(Hash)
            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)")
            )
          end
        end
        params.merge!(date_attributes)
      end

    private
      def params_to_date(year, month, day)
        day ||= 1 # FIXME: is that really what we want? test.
        Date.new(year.to_i, month.to_i, day.to_i) # TODO: test fails.
      end
    end

    def validate(params)
      # TODO: make it cleaner to hook into essential reform steps.
      DateParamsFilter.new.call(params)

      super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reform-0.2.2 lib/reform/form/multi_parameter_attributes.rb