Skip to content

Instantly share code, notes, and snippets.

@warlley
Forked from durran/gist:315227
Created September 24, 2010 18:54
Show Gist options
  • Save warlley/595843 to your computer and use it in GitHub Desktop.
Save warlley/595843 to your computer and use it in GitHub Desktop.
module MultiParameterAttributes
def filter_time(attributes, name)
attrs = attributes.collect do |key, value|
if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
[$1.to_i, value.send("to_#$2")]
end
end.compact.sort_by(&:first).map(&:last)
Time.zone.local(*attrs) unless attrs.empty?
end
end
class ApplicationController < ActionController::Base
include MultiParameterAttributes
end
class YourController < ApplicationController
def your_action
time = filter_time(params, :my_time_attribute_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment