-
-
Save snusnu/413838 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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