mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-23 07:09:39 +02:00
33 lines
751 B
Ruby
33 lines
751 B
Ruby
class Rule < ApplicationRecord
|
|
UnsupportedResourceTypeError = Class.new(StandardError)
|
|
|
|
belongs_to :family
|
|
has_many :conditions, dependent: :destroy
|
|
has_many :actions, dependent: :destroy
|
|
|
|
validates :resource_type, presence: true
|
|
|
|
def apply
|
|
scope = resource_scope
|
|
|
|
conditions.each do |condition|
|
|
scope = condition.apply(scope)
|
|
end
|
|
|
|
actions.each do |action|
|
|
action.apply(scope)
|
|
end
|
|
end
|
|
|
|
def resource_scope
|
|
case resource_type
|
|
when "transaction"
|
|
family.transactions
|
|
.active
|
|
.with_entry
|
|
.where(account_entries: { date: effective_date..nil })
|
|
else
|
|
raise UnsupportedResourceTypeError, "Unsupported resource type: #{resource_type}"
|
|
end
|
|
end
|
|
end
|