activerecord support

This commit is contained in:
John Bintz 2012-10-26 08:40:27 -04:00
parent 2a180fa433
commit 59e8bd2eb3
4 changed files with 50 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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