2014-06-02 15:47:29 +02:00
|
|
|
# workaround for https://github.com/einzige/date_time_attribute/issues/14
|
|
|
|
require 'date_time_attribute'
|
|
|
|
|
|
|
|
module DateTimeAttributeValidate
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include DateTimeAttribute
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def date_time_attribute(*attributes)
|
|
|
|
super
|
|
|
|
|
|
|
|
attributes.each do |attribute|
|
2023-05-12 13:01:12 +02:00
|
|
|
validate -> { send("#{attribute}_datetime_value_valid") }
|
2014-06-02 15:47:29 +02:00
|
|
|
|
|
|
|
# allow resetting the field to nil
|
|
|
|
before_validation do
|
2023-05-12 13:01:12 +02:00
|
|
|
if instance_variable_get("@#{attribute}_is_set")
|
|
|
|
date = instance_variable_get("@#{attribute}_date_value")
|
|
|
|
time = instance_variable_get("@#{attribute}_time_value")
|
|
|
|
send("#{attribute}=", nil) if date.blank? && time.blank?
|
2014-06-02 15:47:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# remember old date and time values
|
|
|
|
define_method("#{attribute}_date_value=") do |val|
|
2023-05-12 13:01:12 +02:00
|
|
|
instance_variable_set("@#{attribute}_is_set", true)
|
|
|
|
instance_variable_set("@#{attribute}_date_value", val)
|
2023-01-06 16:27:41 +01:00
|
|
|
begin
|
2023-05-12 13:01:12 +02:00
|
|
|
send("#{attribute}_date=", val)
|
|
|
|
rescue StandardError
|
2023-01-06 16:27:41 +01:00
|
|
|
nil
|
|
|
|
end
|
2014-06-02 15:47:29 +02:00
|
|
|
end
|
|
|
|
define_method("#{attribute}_time_value=") do |val|
|
2023-05-12 13:01:12 +02:00
|
|
|
instance_variable_set("@#{attribute}_is_set", true)
|
|
|
|
instance_variable_set("@#{attribute}_time_value", val)
|
2023-01-06 16:27:41 +01:00
|
|
|
begin
|
2023-05-12 13:01:12 +02:00
|
|
|
send("#{attribute}_time=", val)
|
|
|
|
rescue StandardError
|
2023-01-06 16:27:41 +01:00
|
|
|
nil
|
|
|
|
end
|
2014-06-02 15:47:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# fallback to field when values are not set
|
|
|
|
define_method("#{attribute}_date_value") do
|
2023-05-12 13:01:12 +02:00
|
|
|
instance_variable_get("@#{attribute}_date_value") || send("#{attribute}_date").try do |e|
|
|
|
|
e.strftime('%Y-%m-%d')
|
|
|
|
end
|
2014-06-02 15:47:29 +02:00
|
|
|
end
|
|
|
|
define_method("#{attribute}_time_value") do
|
2023-05-12 13:01:12 +02:00
|
|
|
instance_variable_get("@#{attribute}_time_value") || send("#{attribute}_time").try do |e|
|
|
|
|
e.strftime('%H:%M')
|
|
|
|
end
|
2014-06-02 15:47:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# validate date and time
|
|
|
|
define_method("#{attribute}_datetime_value_valid") do
|
2023-05-12 13:01:12 +02:00
|
|
|
date = instance_variable_get("@#{attribute}_date_value")
|
2023-01-06 16:27:41 +01:00
|
|
|
unless date.blank? || begin
|
|
|
|
Date.parse(date)
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError
|
2023-01-06 16:27:41 +01:00
|
|
|
nil
|
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
errors.add(attribute, 'is not a valid date') # @todo I18n
|
2014-06-02 15:47:29 +02:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
time = instance_variable_get("@#{attribute}_time_value")
|
2023-01-06 16:27:41 +01:00
|
|
|
unless time.blank? || begin
|
|
|
|
Time.parse(time)
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError
|
2023-01-06 16:27:41 +01:00
|
|
|
nil
|
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
errors.add(attribute, 'is not a valid time') # @todo I18n
|
2014-06-02 15:47:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|