2019-12-06 19:50:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryDateRangeValue < RepositoryDateTimeRangeValueBase
|
2022-02-21 19:08:35 +08:00
|
|
|
self.skip_time_zone_conversion_for_attributes = %i(start_time end_time)
|
|
|
|
|
|
|
|
before_save -> { self.start_time = start_time.to_date }, if: :start_time_changed?
|
|
|
|
before_save -> { self.end_time = end_time.to_date }, if: :end_time_changed?
|
|
|
|
|
|
|
|
def data_different?(new_data)
|
2020-02-17 07:26:20 +08:00
|
|
|
data = new_data.is_a?(String) ? JSON.parse(new_data).symbolize_keys : new_data
|
2022-02-21 19:08:35 +08:00
|
|
|
st = Date.parse(data[:start_time])
|
|
|
|
et = Date.parse(data[:end_time])
|
2019-12-10 20:39:08 +08:00
|
|
|
st.to_date != start_time.to_date || et.to_date != end_time.to_date
|
2019-12-08 23:53:59 +08:00
|
|
|
end
|
|
|
|
|
2019-12-10 20:39:08 +08:00
|
|
|
def formatted
|
|
|
|
super(:full_date)
|
2019-12-08 23:53:59 +08:00
|
|
|
end
|
2019-12-09 20:48:24 +08:00
|
|
|
|
2022-02-22 20:29:50 +08:00
|
|
|
def self.add_filter_condition(repository_rows, join_alias, filter_element)
|
2022-01-31 16:21:08 +08:00
|
|
|
parameters = filter_element.parameters
|
|
|
|
case filter_element.operator
|
|
|
|
when 'equal_to'
|
2022-02-04 17:30:11 +08:00
|
|
|
repository_rows.where("#{join_alias}.start_time::date = ? AND #{join_alias}.end_time::date = ?",
|
2022-02-21 19:08:35 +08:00
|
|
|
Date.parse(parameters['start_date']), Date.parse(parameters['end_date']))
|
2022-01-31 16:21:08 +08:00
|
|
|
when 'unequal_to'
|
2022-02-04 17:30:11 +08:00
|
|
|
repository_rows.where.not("#{join_alias}.start_time::date = ? AND #{join_alias}.end_time::date = ?",
|
2022-02-21 19:08:35 +08:00
|
|
|
Date.parse(parameters['start_date']), Date.parse(parameters['end_date']))
|
2022-01-31 16:21:08 +08:00
|
|
|
when 'greater_than'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where("#{join_alias}.start_time::date > ?", Date.parse(parameters['end_date']))
|
2022-01-31 16:21:08 +08:00
|
|
|
when 'greater_than_or_equal_to'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where("#{join_alias}.start_time::date >= ?", Date.parse(parameters['end_date']))
|
2022-01-31 16:21:08 +08:00
|
|
|
when 'less_than'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where("#{join_alias}.end_time::date < ?", Date.parse(parameters['start_date']))
|
2022-01-31 16:21:08 +08:00
|
|
|
when 'less_than_or_equal_to'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where("#{join_alias}.end_time::date <= ?", Date.parse(parameters['start_date']))
|
2022-01-31 16:21:08 +08:00
|
|
|
when 'between'
|
2022-02-04 17:30:11 +08:00
|
|
|
repository_rows.where("#{join_alias}.start_time::date > ? AND #{join_alias}.end_time::date < ?",
|
2022-02-21 19:08:35 +08:00
|
|
|
Date.parse(parameters['start_date']), Date.parse(parameters['end_date']))
|
2022-01-31 16:21:08 +08:00
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryDateRangeValue!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-09 20:48:24 +08:00
|
|
|
def self.new_with_payload(payload, attributes)
|
2020-02-17 07:26:20 +08:00
|
|
|
data = payload.is_a?(String) ? JSON.parse(payload).symbolize_keys : payload
|
2019-12-17 20:42:25 +08:00
|
|
|
|
2019-12-09 20:48:24 +08:00
|
|
|
value = new(attributes)
|
2022-02-21 19:08:35 +08:00
|
|
|
value.start_time = Date.parse(data[:start_time])
|
|
|
|
value.end_time = Date.parse(data[:end_time])
|
2019-12-09 20:48:24 +08:00
|
|
|
value
|
|
|
|
end
|
2020-01-10 16:29:20 +08:00
|
|
|
|
2022-02-21 19:08:35 +08:00
|
|
|
def update_data!(new_data, user)
|
|
|
|
data = new_data.is_a?(String) ? JSON.parse(new_data).symbolize_keys : new_data
|
|
|
|
self.start_time = Date.parse(data[:start_time])
|
|
|
|
self.end_time = Date.parse(data[:end_time])
|
|
|
|
self.last_modified_by = user
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2020-01-10 16:29:20 +08:00
|
|
|
alias export_formatted formatted
|
2019-12-06 19:50:19 +08:00
|
|
|
end
|