chore: rubocop
chore: fix api test conventions chore: rubocop -A spec/ chore: more rubocop -A fix failing test rubocop fixes removes helper methods that are in my opinion dead code more rubocop fixes rubocop -a --auto-gen-config
This commit is contained in:
parent
f6fb804bbe
commit
fb2b4d8a8a
331 changed files with 4263 additions and 4507 deletions
|
|
@ -10,66 +10,68 @@ module DateTimeAttributeValidate
|
|||
super
|
||||
|
||||
attributes.each do |attribute|
|
||||
validate -> { self.send("#{attribute}_datetime_value_valid") }
|
||||
validate -> { send("#{attribute}_datetime_value_valid") }
|
||||
|
||||
# allow resetting the field to nil
|
||||
before_validation do
|
||||
if self.instance_variable_get("@#{attribute}_is_set")
|
||||
date = self.instance_variable_get("@#{attribute}_date_value")
|
||||
time = self.instance_variable_get("@#{attribute}_time_value")
|
||||
if date.blank? && time.blank?
|
||||
self.send("#{attribute}=", nil)
|
||||
end
|
||||
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?
|
||||
end
|
||||
end
|
||||
|
||||
# remember old date and time values
|
||||
define_method("#{attribute}_date_value=") do |val|
|
||||
self.instance_variable_set("@#{attribute}_is_set", true)
|
||||
self.instance_variable_set("@#{attribute}_date_value", val)
|
||||
instance_variable_set("@#{attribute}_is_set", true)
|
||||
instance_variable_set("@#{attribute}_date_value", val)
|
||||
begin
|
||||
self.send("#{attribute}_date=", val)
|
||||
rescue
|
||||
send("#{attribute}_date=", val)
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
end
|
||||
define_method("#{attribute}_time_value=") do |val|
|
||||
self.instance_variable_set("@#{attribute}_is_set", true)
|
||||
self.instance_variable_set("@#{attribute}_time_value", val)
|
||||
instance_variable_set("@#{attribute}_is_set", true)
|
||||
instance_variable_set("@#{attribute}_time_value", val)
|
||||
begin
|
||||
self.send("#{attribute}_time=", val)
|
||||
rescue
|
||||
send("#{attribute}_time=", val)
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
# fallback to field when values are not set
|
||||
define_method("#{attribute}_date_value") do
|
||||
self.instance_variable_get("@#{attribute}_date_value") || self.send("#{attribute}_date").try { |e| e.strftime('%Y-%m-%d') }
|
||||
instance_variable_get("@#{attribute}_date_value") || send("#{attribute}_date").try do |e|
|
||||
e.strftime('%Y-%m-%d')
|
||||
end
|
||||
end
|
||||
define_method("#{attribute}_time_value") do
|
||||
self.instance_variable_get("@#{attribute}_time_value") || self.send("#{attribute}_time").try { |e| e.strftime('%H:%M') }
|
||||
instance_variable_get("@#{attribute}_time_value") || send("#{attribute}_time").try do |e|
|
||||
e.strftime('%H:%M')
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# validate date and time
|
||||
define_method("#{attribute}_datetime_value_valid") do
|
||||
date = self.instance_variable_get("@#{attribute}_date_value")
|
||||
date = instance_variable_get("@#{attribute}_date_value")
|
||||
unless date.blank? || begin
|
||||
Date.parse(date)
|
||||
rescue
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
errors.add(attribute, "is not a valid date") # @todo I18n
|
||||
errors.add(attribute, 'is not a valid date') # @todo I18n
|
||||
end
|
||||
time = self.instance_variable_get("@#{attribute}_time_value")
|
||||
time = instance_variable_get("@#{attribute}_time_value")
|
||||
unless time.blank? || begin
|
||||
Time.parse(time)
|
||||
rescue
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
errors.add(attribute, "is not a valid time") # @todo I18n
|
||||
errors.add(attribute, 'is not a valid time') # @todo I18n
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue