Sha256: 6cdc5f7fda0e7bd69de26ebd61421d8d0ba4fbbcf53c8c65b97a19c0bf3fdda1

Contents?: true

Size: 1.92 KB

Versions: 13

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true
# A thought recorded by a Participant.
class Thought < ActiveRecord::Base
  EFFECTS = {
    helpful: "helpful",
    harmful: "harmful",
    neither: "neither"
  }.freeze
  IDENTIFIED = "Identified"
  ASSIGNED_A_PATTERN = "Assigned a pattern to"
  RESHAPED = "Reshaped"

  belongs_to :participant
  belongs_to :pattern, class_name: "ThoughtPattern", foreign_key: :pattern_id

  before_validation :clean_attributes

  validates :participant, :content, :effect, presence: true
  validates :effect, inclusion: { in: EFFECTS.values }

  delegate :description,
           :recommendations,
           :title,
           to: :pattern,
           prefix: true,
           allow_nil: true

  scope :helpful, -> { where(effect: EFFECTS[:helpful]) }

  scope :harmful, -> { where(effect: EFFECTS[:harmful]) }

  scope :no_pattern, -> { where(pattern_id: nil) }

  scope :unreflected, lambda {
    where(effect: EFFECTS[:harmful])
      .where(arel_table[:challenging_thought].eq(nil)
        .or(arel_table[:act_as_if].eq(nil)))
  }

  scope :last_seven_days, lambda {
    where(
      arel_table[:created_at]
        .gteq(Time.current.advance(days: -7).beginning_of_day)
    )
  }

  scope :for_day, lambda { |time|
    where(
      arel_table[:created_at]
        .gteq(time.beginning_of_day)
        .and(arel_table[:created_at].lteq(time.end_of_day))
    )
  }

  def status_label
    if !pattern_id && !challenging_thought
      IDENTIFIED
    elsif pattern_id && !challenging_thought
      ASSIGNED_A_PATTERN
    elsif challenging_thought
      RESHAPED
    end
  end

  def shared_description
    "Thought: #{content}"
  end

  private

  def clean_attributes
    clean_attribute = lambda do |attr|
      if self[attr].respond_to?(:strip)
        cleaned = self[attr].strip
        send("#{attr}=", cleaned == "" ? nil : cleaned)
      end
    end

    clean_attribute.call(:challenging_thought)
    clean_attribute.call(:act_as_if)
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
think_feel_do_engine-3.22.9 app/models/thought.rb
think_feel_do_engine-3.22.8 app/models/thought.rb
think_feel_do_engine-3.22.7 app/models/thought.rb
think_feel_do_engine-3.22.6 app/models/thought.rb
think_feel_do_engine-3.22.5 app/models/thought.rb
think_feel_do_engine-3.22.4 app/models/thought.rb
think_feel_do_engine-3.22.2 app/models/thought.rb
think_feel_do_engine-3.22.1 app/models/thought.rb
think_feel_do_engine-3.22.0 app/models/thought.rb
think_feel_do_engine-3.21.2 app/models/thought.rb
think_feel_do_engine-3.21.1 app/models/thought.rb
think_feel_do_engine-3.21.0 app/models/thought.rb
think_feel_do_engine-3.20.1 app/models/thought.rb