scinote-web/app/models/repository_date_value.rb

79 lines
3.1 KiB
Ruby
Raw Normal View History

2019-12-06 19:50:19 +08:00
# frozen_string_literal: true
class RepositoryDateValue < RepositoryDateTimeValueBase
include RepositoryValueWithReminders
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
end
2019-12-10 20:39:08 +08:00
def formatted
super(:full_date)
end
def self.add_filter_condition(repository_rows, join_alias, filter_element)
parameters = filter_element.parameters
case filter_element.operator
when 'today'
repository_rows.where("#{join_alias}.data::date = ?", Time.zone.now.to_date)
when 'yesterday'
repository_rows.where("#{join_alias}.data::date = ?", Time.zone.now.to_date - 1.day)
when 'last_week'
repository_rows.where("#{join_alias}.data::date >= ? AND #{join_alias}.data::date < ?",
Time.zone.now.beginning_of_week.to_date - 1.week, Time.zone.now.beginning_of_week.to_date)
when 'this_month'
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)
when 'last_year'
repository_rows.where("#{join_alias}.data::date >= ? AND #{join_alias}.data::date < ?",
Time.zone.now.beginning_of_year.to_date - 1.year, Time.zone.now.beginning_of_year.to_date)
when 'this_year'
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)
when 'equal_to'
repository_rows.where("#{join_alias}.data::date = ?", Date.parse(parameters['date']))
when 'unequal_to'
repository_rows.where.not("#{join_alias}.data::date = ?", Date.parse(parameters['date']))
when 'greater_than_or_equal_to'
repository_rows.where("#{join_alias}.data::date >= ?", Date.parse(parameters['date']))
when 'less_than'
repository_rows.where("#{join_alias}.data::date < ?", Date.parse(parameters['date']))
when 'between'
repository_rows.where("#{join_alias}.data::date > ? AND #{join_alias}.data::date < ?",
Date.parse(parameters['start_date']), Date.parse(parameters['end_date']))
else
raise ArgumentError, 'Wrong operator for RepositoryDateValue!'
end
end
def self.new_with_payload(payload, attributes)
value = new(attributes)
value.data = Date.parse(payload)
value
end
2020-01-10 16:29:20 +08:00
def update_data!(new_data, user, preview: false)
self.data = if new_data.is_a?(String)
Date.parse(new_data)
else
new_data.to_time.utc
end
self.last_modified_by = user
preview ? validate : save!
end
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(/%-/, '%')
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