Skip to content

Instantly share code, notes, and snippets.

@jamster
Created December 19, 2008 16:42
Show Gist options
  • Save jamster/38039 to your computer and use it in GitHub Desktop.
Save jamster/38039 to your computer and use it in GitHub Desktop.
# This code just takes multiparamter inputs from a form and strings them together to
# create dash separated date string. The purpose behind this is because sometimes if
# a user doesn't fill in all the date drop downs in a form, you might get an invalid date
# The folowing code was repurposed from ActiveRecord::Base from a few of the mutliparamter
# assignment methods
module ConverDateParamsToString
def convert_date_params_to_string!(params, date_attr_name)
date_params = params.reject {| key, value | !(key =~ /^#{date_attr_name}/) }
params = params.reject {| key, value | (key =~ /^#{date_attr_name}/) }
date_attributes=[]
date_params.each do |date_param|
name, value = date_param
date_attributes << [name.scan(/\(([0-9]*).*\)/).first.first, value]
end
params[date_attr_name] = date_attributes.sort_by{|x| x.first}.collect{|x| x[1]}.join("-")
end
end
class ControllerMock
include ConverDateParamsToString
def params
{
"happy" => "joy",
"date(3i)" => "20",
"date(2i)" => "1",
"date(1i)" => "2003"
}
end
end
controller = ControllerMock.new
puts controller.convert_date_params_to_string!(controller.params, "date")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment