<commit_before># == Schema Information
#
# Table name: transactions
#
#  id                         :integer          not null, primary key
#  account_id                 :integer          not null
#  direction                  :integer          default(0), not null
#  amount_cents               :integer          default(0), not null
#  amount_currency            :string           default("USD"), not null
#  calculated_amount_cents    :integer          default(0), not null
#  calculated_amount_currency :string           default("USD"), not null
#  rate                       :float            default(1.0), not null
#  kind                       :integer          default(0), not null
#  category_id                :integer
#  memo                       :string           default(""), not null
#  created_at                 :datetime         not null
#  updated_at                 :datetime         not null
#

class Transaction < ActiveRecord::Base
  monetize :amount_cents,
           with_model_currency: :amount_cents_currency

  monetize :calculated_amount_cents,
           with_model_currency: :calculated_amount_currency

  validates :amount_cents, numericality: { greater_than: 0 }

  validates :amount_currency,
            :calculated_amount_currency,
            inclusion: { in: Const::CURRENCY_CODES }

  validates :account_id, :direction, :kind, presence: true

  enum direction: Const::TRANSACTION_DIRECTIONS
  enum kind: Const::TRANSACTION_KINDS

  belongs_to :category
  belongs_to :account

  # scope :with_account, -> { includes(:account) }
  # scope :history, -> { with_account.order(created_at: :desc) }
  # scope :recent_history, -> { history.limit(Const::RECENT_HISTORY_LENGTH) }
  # scope :outflows, -> { where(direction: Const::OUTFLOW) }
  # scope :inflows, -> { where(direction: Const::INFLOW) }

  scope :expenses, -> { where(kind: Const::EXPENSE) }

  before_create :refresh_rate
  before_update :refresh_rate_if_currency_changed
  before_save :refresh_calculated_amount
  after_save :update_budget

  delegate :currency, to: :account, prefix: :account

  # def uncategorized?
  #   category.nil?
  # end

  private

  def refresh_rate
    self.rate = (amount_currency == account_currency) ? 1 : load_rate
  end

  def refresh_rate_if_currency_changed<commit_msg>Update budget after Transaction destroy<commit_after># == Schema Information
#
# Table name: transactions
#
#  id                         :integer          not null, primary key
#  account_id                 :integer          not null
#  direction                  :integer          default(0), not null
#  amount_cents               :integer          default(0), not null
#  amount_currency            :string           default("USD"), not null
#  calculated_amount_cents    :integer          default(0), not null
#  calculated_amount_currency :string           default("USD"), not null
#  rate                       :float            default(1.0), not null
#  kind                       :integer          default(0), not null
#  category_id                :integer
#  memo                       :string           default(""), not null
#  created_at                 :datetime         not null
#  updated_at                 :datetime         not null
#

class Transaction < ActiveRecord::Base
  monetize :amount_cents,
           with_model_currency: :amount_cents_currency

  monetize :calculated_amount_cents,
           with_model_currency: :calculated_amount_currency

  validates :amount_cents, numericality: { greater_than: 0 }

  validates :amount_currency,
            :calculated_amount_currency,
            inclusion: { in: Const::CURRENCY_CODES }

  validates :account_id, :direction, :kind, presence: true

  enum direction: Const::TRANSACTION_DIRECTIONS
  enum kind: Const::TRANSACTION_KINDS

  belongs_to :category
  belongs_to :account

  # scope :with_account, -> { includes(:account) }
  # scope :history, -> { with_account.order(created_at: :desc) }
  # scope :recent_history, -> { history.limit(Const::RECENT_HISTORY_LENGTH) }
  # scope :outflows, -> { where(direction: Const::OUTFLOW) }
  # scope :inflows, -> { where(direction: Const::INFLOW) }

  scope :expenses, -> { where(kind: Const::EXPENSE) }

  before_create :refresh_rate
  before_update :refresh_rate_if_currency_changed
  before_save :refresh_calculated_amount
  after_save :update_budget
  after_destroy :update_budget

  delegate :currency, to: :account, prefix: :account

  # def uncategorized?
  #   category.nil?
  # end

  private

  def refresh_rate
    self.rate = (amount_currency == account_currency) ? 1 : load_rate
  end

  def refresh_rate_if_currency_changed
{'fraction_non_alphanumeric': 0.08833010960670536, 'fraction_numerical': 0.003438641736514077, 'mean_word_length': 2.496619083395943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2850985', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1304, 'n_words': 315}