activerecord support
This commit is contained in:
parent
2a180fa433
commit
59e8bd2eb3
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue