2019-12-06 19:50:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryTimeValue < RepositoryDateTimeValueBase
|
2022-04-12 21:56:20 +08:00
|
|
|
SORTABLE_COLUMN_NAME = 'repository_date_time_values.data::time'
|
|
|
|
|
2022-02-21 19:08:35 +08:00
|
|
|
def data_different?(new_data)
|
2019-12-10 20:39:08 +08:00
|
|
|
new_time = Time.zone.parse(new_data)
|
|
|
|
new_time.min != data.min || new_time.hour != data.hour
|
2019-12-08 23:53:59 +08:00
|
|
|
end
|
|
|
|
|
2019-12-10 20:39:08 +08:00
|
|
|
def formatted
|
|
|
|
super(:time)
|
2019-12-08 23:53:59 +08:00
|
|
|
end
|
2019-12-09 20:48:24 +08:00
|
|
|
|
2022-01-31 18:10:39 +08:00
|
|
|
def self.add_filter_condition(repository_rows, join_alias, filter_element)
|
2021-12-21 19:38:52 +08:00
|
|
|
parameters = filter_element.parameters
|
|
|
|
case filter_element.operator
|
|
|
|
when 'equal_to'
|
2022-03-10 20:49:09 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::time = (?)::time", Time.zone.parse(parameters['time']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'unequal_to'
|
2022-03-10 20:49:09 +08:00
|
|
|
repository_rows.where.not("#{join_alias}.data::time = (?)::time", Time.zone.parse(parameters['time']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'greater_than'
|
2022-03-10 20:49:09 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::time > (?)::time", Time.zone.parse(parameters['time']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'greater_than_or_equal_to'
|
2022-03-10 20:49:09 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::time >= (?)::time", Time.zone.parse(parameters['time']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'less_than'
|
2022-03-10 20:49:09 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::time < (?)::time", Time.zone.parse(parameters['time']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'less_than_or_equal_to'
|
2022-03-10 20:49:09 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::time <= (?)::time", Time.zone.parse(parameters['time']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'between'
|
2022-03-10 20:49:09 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::time > (?)::time AND #{join_alias}.data::time < (?)::time",
|
2022-02-04 17:30:11 +08:00
|
|
|
Time.zone.parse(parameters['start_time']), Time.zone.parse(parameters['end_time']))
|
2021-12-21 19:38:52 +08:00
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryTimeValue!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-09 20:48:24 +08:00
|
|
|
def self.new_with_payload(payload, attributes)
|
|
|
|
value = new(attributes)
|
|
|
|
value.data = Time.zone.parse(payload)
|
|
|
|
value
|
|
|
|
end
|
2020-01-10 16:29:20 +08:00
|
|
|
|
2020-03-18 17:35:31 +08:00
|
|
|
def self.import_from_text(text, attributes, _options = {})
|
2020-03-18 16:53:47 +08:00
|
|
|
time_format = '%H:%M'
|
|
|
|
new(attributes.merge(data: DateTime.strptime(text, time_format).strftime(time_format)))
|
2020-03-18 04:20:50 +08:00
|
|
|
rescue ArgumentError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2020-01-10 16:29:20 +08:00
|
|
|
alias export_formatted formatted
|
2019-12-06 19:50:19 +08:00
|
|
|
end
|