Sha256: bf956f13a6e8cdaa277bdc6f7ba021ca5646005db838b7cd369f201475ce4073

Contents?: true

Size: 1.87 KB

Versions: 53

Compression:

Stored size: 1.87 KB

Contents

# A thought recorded by a Participant.
class Thought < ActiveRecord::Base
  EFFECTS = {
    helpful: "helpful",
    harmful: "harmful",
    neither: "neither"
  }
  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
    def clean_attribute(attr)
      if self[attr].respond_to?(:strip)
        cleaned = self[attr].strip
        send("#{ attr }=", cleaned == "" ? nil : cleaned)
      end
    end

    clean_attribute(:challenging_thought)
    clean_attribute(:act_as_if)
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
think_feel_do_engine-3.19.7 app/models/thought.rb
think_feel_do_engine-3.19.6 app/models/thought.rb
think_feel_do_engine-3.19.5 app/models/thought.rb
think_feel_do_engine-3.19.4 app/models/thought.rb
think_feel_do_engine-3.19.3 app/models/thought.rb
think_feel_do_engine-3.19.2 app/models/thought.rb
think_feel_do_engine-3.19.1 app/models/thought.rb
think_feel_do_engine-3.19.0 app/models/thought.rb
think_feel_do_engine-3.18.0 app/models/thought.rb
think_feel_do_engine-3.17.2 app/models/thought.rb
think_feel_do_engine-3.17.1 app/models/thought.rb
think_feel_do_engine-3.17.0 app/models/thought.rb
think_feel_do_engine-3.16.3 app/models/thought.rb
think_feel_do_engine-3.16.2 app/models/thought.rb
think_feel_do_engine-3.16.1 app/models/thought.rb
think_feel_do_engine-3.15.7 app/models/thought.rb
think_feel_do_engine-3.16.0 app/models/thought.rb
think_feel_do_engine-3.15.6 app/models/thought.rb
think_feel_do_engine-3.15.5 app/models/thought.rb
think_feel_do_engine-3.15.4 app/models/thought.rb