Skip to content

Instantly share code, notes, and snippets.

@foysavas
Created May 25, 2010 16:14
Show Gist options
  • Save foysavas/413336 to your computer and use it in GitHub Desktop.
Save foysavas/413336 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
before_filter :compound_date_values
protected
def compound_date_values(h=params,prefix=[])
to_fix = {}
h.each do |k, v|
if v.is_a? Hash
compound_date_values(v,prefix+[k])
end
if m = k.match(/^([a-zA-Z_]+?)\(([12345])i\)$/)
if !prefix.empty?
params.send_chain(prefix.map{|a| [:[],a]}).delete(k)
else
params.delete(k)
end
to_fix[m[1]] = {} unless to_fix[m[1]]
to_fix[m[1]][m[2].to_i] = v.to_i
end
end
to_fix.each do |k,v|
if v.keys.size == 3
d = Date.new(v[1], v[2], v[3])
elsif v.keys.size == 5
d = DateTime.new(v[1], v[2], v[3], v[4], v[5])
end
if !prefix.empty?
params.send_chain(prefix.map{|a| [:[],a]})[k] = d
else
params[k] = d
end
end
end
end
class Object
def send_chain(v)
this = self
v.each do |k|
return nil if this.nil?
this = this.send(*k)
end
this
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment