2019-12-06 19:50:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryDateValue < RepositoryDateTimeValueBase
|
2022-03-23 16:51:42 +08:00
|
|
|
include RepositoryValueWithReminders
|
|
|
|
|
2022-02-21 19:08:35 +08:00
|
|
|
self.skip_time_zone_conversion_for_attributes = [:data]
|
|
|
|
|
|
|
|
before_save -> { self.data = data.to_date }, if: :data_changed?
|
|
|
|
|
|
|
|
def data_different?(new_data)
|
|
|
|
Date.parse(new_data).to_date != data.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-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 'today'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date = ?", Time.zone.now.to_date)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'yesterday'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date = ?", Time.zone.now.to_date - 1.day)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'last_week'
|
2022-02-04 17:30:11 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date >= ? AND #{join_alias}.data::date < ?",
|
2022-02-21 19:08:35 +08:00
|
|
|
Time.zone.now.beginning_of_week.to_date - 1.week, Time.zone.now.beginning_of_week.to_date)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'this_month'
|
2022-02-22 21:14:16 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date >= ? AND #{join_alias}.data::date <= ?",
|
|
|
|
Time.zone.now.beginning_of_month.to_date, Time.zone.now.end_of_month.to_date)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'last_year'
|
2022-02-04 17:30:11 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date >= ? AND #{join_alias}.data::date < ?",
|
2022-02-21 19:08:35 +08:00
|
|
|
Time.zone.now.beginning_of_year.to_date - 1.year, Time.zone.now.beginning_of_year.to_date)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'this_year'
|
2022-02-22 21:14:16 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date >= ? AND #{join_alias}.data::date <= ?",
|
|
|
|
Time.zone.now.beginning_of_year.to_date, Time.zone.now.end_of_year.to_date)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'equal_to'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date = ?", Date.parse(parameters['date']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'unequal_to'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where.not("#{join_alias}.data::date = ?", Date.parse(parameters['date']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'greater_than_or_equal_to'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date >= ?", Date.parse(parameters['date']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'less_than'
|
2022-02-21 19:08:35 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date < ?", Date.parse(parameters['date']))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'between'
|
2022-02-04 17:30:11 +08:00
|
|
|
repository_rows.where("#{join_alias}.data::date > ? AND #{join_alias}.data::date < ?",
|
2022-02-21 19:08:35 +08:00
|
|
|
Date.parse(parameters['start_date']), Date.parse(parameters['end_date']))
|
2021-12-21 19:38:52 +08:00
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryDateValue!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-09 20:48:24 +08:00
|
|
|
def self.new_with_payload(payload, attributes)
|
|
|
|
value = new(attributes)
|
2022-02-21 19:08:35 +08:00
|
|
|
value.data = Date.parse(payload)
|
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)
|
|
|
|
self.data = Date.parse(new_data)
|
|
|
|
self.last_modified_by = user
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2020-03-18 04:20:50 +08:00
|
|
|
def self.import_from_text(text, attributes, options = {})
|
2020-03-18 16:53:47 +08:00
|
|
|
date_format = (options.dig(:user, :settings, :date_format) || Constants::DEFAULT_DATE_FORMAT).gsub(/%-/, '%')
|
2020-03-18 04:20:50 +08:00
|
|
|
new(attributes.merge(data: DateTime.strptime(text, date_format)))
|
|
|
|
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
|