# Holds the completion status of a task for each participant class TaskStatus < ActiveRecord::Base belongs_to :membership belongs_to :task has_one :participant, through: :membership has_many :engagements, dependent: :destroy delegate :bit_core_content_module, :bit_core_content_module_id, :release_day, :title, to: :task delegate :participant_id, to: :membership, prefix: false scope :for_content_module, lambda { |content_module| joins(:task) .where(tasks: { bit_core_content_module_id: content_module.id }) } scope :for_content_module_ids, lambda { |ids| joins(:task) .where(tasks: { bit_core_content_module_id: ids }) } scope :completed, -> { where(arel_table[:completed_at].not_eq(nil)) } scope :incomplete, -> { where(arel_table[:completed_at].eq(nil)) } scope :by_position, lambda { joins(:task, task: :bit_core_content_module) .order("bit_core_content_modules.position ASC") } scope :available_by_day, lambda { |day_in_study| where(arel_table[:start_day].lteq(day_in_study)) } scope :incomplete_by_day, lambda { |day_in_study| where(arel_table[:start_day].lteq(day_in_study)) .incomplete } scope :incomplete_on_day, lambda { |day_in_study| where(arel_table[:start_day].eq(day_in_study)) .incomplete } scope :not_terminated_by_day, lambda { |day_in_study| tasks = Arel::Table.new(:tasks) joins(:task) .where( tasks[:termination_day].eq(nil) .or( tasks[:termination_day].gteq(day_in_study) ) ) } def accessible? available_for_learning_on <= Date.today end def completed? completed_at.present? end def previous_completed? return true if is_first? self .class .find_by_id(membership_task_status_ids[index_for_membership - 1]) .completed? end def provider_viz? try(:bit_core_content_module).try(:is_viz) end def mark_complete if completed_at save! else update_attributes(completed_at: DateTime.current) end end def is_lesson? task.bit_core_content_module.type == "ContentModules::LessonModule" end def notify_today? today = Time.now start_day == ((today.to_date - membership.start_date.to_date).to_i + 1) end def available_for_learning_on membership.start_date + release_day - 1 end private def index_for_membership membership_task_status_ids .find_index(id) end def is_first? index_for_membership.zero? end def membership_task_status_ids membership .ordered_task_status_ids end end