如果在:deadline
之后设置date_started
,我如何才能:date_started
== Date.current
?
create_table "challenges", force: true do |t|
t.string "action"
t.datetime "date_started"
t.date "deadline" # This is nil upon create
end
def date_started_sets_deadline
if self.date_started > Date.current
self.deadline = self.date_started
end
end
class Challenge < ActiveRecord::Base
attr_accessor :missed_one, :missed_two, :missed_three, :dot_set_tag_owner
CATEGORY = ['One-Shot', 'Ongoing']
serialize :committed, Array
acts_as_taggable
belongs_to :user
has_many :notes
has_many :notifications
has_many :comments
has_many :missed_dates, -> { order(:id) }
has_many :challenge_likes
has_many :likers, through: :challenge_likes, class_name: 'User', source: :liker
has_many :commentators, -> { distinct }, through: :comments, source: :user
#before_save :set_tag_owner, :unless => :dot_set_tag_owner
before_save :date_started_sets_deadline
accepts_nested_attributes_for :notes, :reject_if => :all_blank, :allow_destroy => true
scope :oneshot, -> { where(categories: 'One-Shot') }
scope :ongoing, -> { where(categories: 'Ongoing') }
scope :deadline_not_present, -> { where(deadline: nil) }
scope :deadline_present, -> { where.not(deadline: nil) }
scope :date_started_not_present, -> { where(date_started: nil) }
scope :date_started_present, -> { where.not(date_started: nil) }
scope :accomplished, -> { where(accomplished: true) }
scope :unaccomplished, -> { where(accomplished: nil) }
scope :publish, ->{ where(:conceal => false) }
scope :incomplete, -> {where("completed_at is null OR completed_at < ?", Time.current.beginning_of_day)}
scope :unarchived, -> { where(archive: false) }
scope :archived, -> { order("deadline").limit(20) }
def date_started_sets_deadline
if self.date_started.is_a?(DateTime)
started = self.date_started.to_date
if started > Date.current
self.deadline = started
end
end
end
def to_param
"#{id} #{action}".parameterize
end
def refresh_freebie
if Date.sunday
challenge.freebie = 0
challenge.freebie_date = nil
end
end
def days_left_challenged
self.days_challenged - ((date_started.to_date)..Date.yesterday).count do |date|
committed_wdays.include? date.wday
end + self.missed_days
end
def self.committed_for_today
today_name = Date::ABBR_DAYNAMES[Date.current.wday].downcase
ids = all.select { |h| h.committed.include? today_name }.map(&:id)
where(id: ids)
end
def committed_wdays
committed.map do |day|
Date::ABBR_DAYNAMES.index(day.titleize)
end
end
def days_done_challenged
((date_started.to_date)..Date.yesterday).count do |date|
committed_wdays.include? date.wday
end - self.missed_days
end
def days_left_challenged
self.days_challenged - ((date_started.to_date)..Date.yesterday).count do |date|
committed_wdays.include? date.wday
end + self.missed_days
end
def challenge_progress
days_done_challenged / (days_done_challenged + days_left_challenged).to_f * 100
end
end
答案 0 :(得分:1)
您将问题标记为ruby-on-rails,因此我建议您在模型中使用before_save
回调。如果设置了date_started
,您可以将其转换为日期并根据当前日期进行检查。可能存在更具可读性的实现。
class Challenge < ActiveRecord::Base
before_save : date_started_sets_deadline
private
def date_started_sets_deadline
if self.date_started.is_a?(DateTime)
started = self.date_started.to_date
if started > Date.current
self.deadline = started
end
end
end
end