Skip to content

Instantly share code, notes, and snippets.

@durran
Created February 26, 2010 00:14
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save durran/315227 to your computer and use it in GitHub Desktop.
Save durran/315227 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
@ljkbennett
Copy link

Thanks for that, I have put a modified version up at https://gist.github.com/littleowllabs/5495596 which removes the multi parameter attributes and to set the overall attribute to the time, this allows it to be passed directly in to update_attributes once the filtering has been done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment