scinote-web/app/models/repository_date_time_range_value.rb

52 lines
2.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2019-12-06 19:50:19 +08:00
class RepositoryDateTimeRangeValue < RepositoryDateTimeRangeValueBase
def data_different?(new_data)
data = new_data.is_a?(String) ? JSON.parse(new_data).symbolize_keys : new_data
2019-12-17 18:27:07 +08:00
st = Time.zone.parse(data[:start_time])
et = Time.zone.parse(data[:end_time])
2019-12-10 20:39:08 +08:00
st.to_i != start_time.to_i || et.to_i != end_time.to_i
end
2019-12-10 20:39:08 +08:00
def formatted
super(:full_with_comma)
end
def self.add_filter_condition(repository_rows, join_alias, filter_element)
parameters = filter_element.parameters
case filter_element.operator
when 'equal_to'
repository_rows.where("#{join_alias}.start_time = ? AND #{join_alias}.end_time = ?",
Time.zone.parse(parameters['start_datetime']), Time.zone.parse(parameters['end_datetime']))
when 'unequal_to'
repository_rows.where.not("#{join_alias}.start_time = ? AND #{join_alias}.end_time = ?",
Time.zone.parse(parameters['start_datetime']),
Time.zone.parse(parameters['end_datetime']))
when 'greater_than'
repository_rows.where("#{join_alias}.start_time > ?", Time.zone.parse(parameters['end_datetime']))
when 'greater_than_or_equal_to'
repository_rows.where("#{join_alias}.start_time >= ?", Time.zone.parse(parameters['end_datetime']))
when 'less_than'
repository_rows.where("#{join_alias}.end_time < ?", Time.zone.parse(parameters['start_datetime']))
when 'less_than_or_equal_to'
repository_rows.where("#{join_alias}.end_time <= ?", Time.zone.parse(parameters['start_datetime']))
when 'between'
repository_rows.where("#{join_alias}.start_time > ? AND #{join_alias}.end_time < ?",
Time.zone.parse(parameters['start_datetime']), Time.zone.parse(parameters['end_datetime']))
else
raise ArgumentError, 'Wrong operator for RepositoryDateTimeRangeValue!'
end
end
def self.new_with_payload(payload, attributes)
data = payload.is_a?(String) ? JSON.parse(payload).symbolize_keys : payload
2019-12-17 18:27:07 +08:00
value = new(attributes)
2019-12-17 18:27:07 +08:00
value.start_time = Time.zone.parse(data[:start_time])
value.end_time = Time.zone.parse(data[:end_time])
value
end
2020-01-10 16:29:20 +08:00
alias export_formatted formatted
end