From 59e8bd2eb3c9fd52cba9f8c061ee3a4aa59c52d1 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 26 Oct 2012 08:40:27 -0400 Subject: [PATCH] activerecord support --- ...formtastic-separate_html5_date_and_time.rb | 4 +++ .../active_record.rb | 15 ++++++++++ .../mongoid.rb | 21 ++------------ .../processor.rb | 29 +++++++++++++++++++ 4 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 lib/formtastic-separate_html5_date_and_time/active_record.rb create mode 100644 lib/formtastic-separate_html5_date_and_time/processor.rb diff --git a/lib/formtastic-separate_html5_date_and_time.rb b/lib/formtastic-separate_html5_date_and_time.rb index 797b6f6..ef5c258 100644 --- a/lib/formtastic-separate_html5_date_and_time.rb +++ b/lib/formtastic-separate_html5_date_and_time.rb @@ -7,6 +7,10 @@ module Formtastic require 'formtastic' require 'formtastic-separate_html5_date_and_time/formtastic' + if defined?(ActiveRecord::Base) + require 'formtastic-separate_html5_date_and_time/active_record' + end + if defined?(Mongoid::Document) require 'formtastic-separate_html5_date_and_time/mongoid' end diff --git a/lib/formtastic-separate_html5_date_and_time/active_record.rb b/lib/formtastic-separate_html5_date_and_time/active_record.rb new file mode 100644 index 0000000..443a10e --- /dev/null +++ b/lib/formtastic-separate_html5_date_and_time/active_record.rb @@ -0,0 +1,15 @@ +require 'formtastic-separate_html5_date_and_time/processor' + +module ActiveRecord + module ProcessSeparateDateAndTimePickerInputs + def assign_multiparameter_attributes(attrs) + Formtastic::SeparateDateAndTimePickerInput::Processor.process(attrs).each do |attribute, value| + public_send("#{attribute}=", value) + end + end + + def update_attributes(attrs = nil) + super Formtastic::SeparateDateAndTimePickerInput::Processor.process(attrs) + end + end +end diff --git a/lib/formtastic-separate_html5_date_and_time/mongoid.rb b/lib/formtastic-separate_html5_date_and_time/mongoid.rb index 1033271..08aa556 100644 --- a/lib/formtastic-separate_html5_date_and_time/mongoid.rb +++ b/lib/formtastic-separate_html5_date_and_time/mongoid.rb @@ -1,27 +1,10 @@ require 'mongoid' +require 'formtastic-separate_html5_date_and_time/processor' module Mongoid module ProcessSeparateDateAndTimePickerInputs def process_attributes(attrs = nil, role = :default, guard_protected_attributes = true) - if attrs - datetime_attrs = {} - - attrs.each do |key, value| - if attr = key[/\A(.*)\((date|time)\)\Z/, 1] - datetime_attrs[attr] ||= {} - datetime_attrs[attr][key] = value - end - end - - datetime_attrs.each do |attr, values| - attrs[attr] = values["#{attr}(date)"] + ' ' + values["#{attr}(time)"] - - attrs.delete("#{attr}(date)") - attrs.delete("#{attr}(time)") - end - end - - super(attrs, role, guard_protected_attributes) + super Formtastic::SeparateDateAndTimePickerInput::Processor.process(attrs), role, guard_protected_attributes end end end diff --git a/lib/formtastic-separate_html5_date_and_time/processor.rb b/lib/formtastic-separate_html5_date_and_time/processor.rb new file mode 100644 index 0000000..1a39680 --- /dev/null +++ b/lib/formtastic-separate_html5_date_and_time/processor.rb @@ -0,0 +1,29 @@ +module Formtastic + module SeparateDateAndTimePickerInput + class Processor + def self.process(attrs) + if attrs + attrs = Hash[attrs.dup] + + datetime_attrs = {} + + attrs.each do |key, value| + if attr = key[/\A(.*)\((date|time)\)\Z/, 1] + datetime_attrs[attr] ||= {} + datetime_attrs[attr][key] = value + end + end + + datetime_attrs.each do |attr, values| + attrs[attr] = values["#{attr}(date)"] + ' ' + values["#{attr}(time)"] + + attrs.delete("#{attr}(date)") + attrs.delete("#{attr}(time)") + end + end + + attrs + end + end + end +end