Class: RequestExpense

Inherits:
ApplicationRecord show all
Defined in:
app/models/request_expense.rb

Overview

This class holds all the information related to every expense in a request.

As there is only one reimbursement per request, RequestExpense objects hold information from both the Request and the Reimbursement corresponding objects, so total_currency and authorized_currency attributes are updated during the reimbursement process

Instance Attribute Summary collapse

Belongs to collapse

Delegated Instance Attributes collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes

Instance Attribute Details

#approved_amount ⇒ Decimal

Returns:

  • (Decimal)

Validations (if => -> { request&.approved? } ):



21
# File 'app/models/request_expense.rb', line 21

validates :approved_amount, :approved_currency, presence: true, if: -> { request&.approved? }

#approved_currency ⇒ String

Returns:

  • (String)

Validations (if => -> { request&.approved? } ):



21
# File 'app/models/request_expense.rb', line 21

validates :approved_amount, :approved_currency, presence: true, if: -> { request&.approved? }

#authorized_amount ⇒ Decimal

Returns:

  • (Decimal)

Validations (if => -> { request&.reimbursement&.submitted? } ):



23
# File 'app/models/request_expense.rb', line 23

validates :authorized_amount, presence: true, if: -> { request&.reimbursement&. }

#created_at ⇒ DateTime

Returns:

  • (DateTime)


184
# File 'db/schema.rb', line 184

t.datetime "created_at"

#description ⇒ String

Returns:

  • (String)


179
# File 'db/schema.rb', line 179

t.string "description"

#estimated_amount ⇒ Decimal

Returns:

  • (Decimal)

Validations (if => -> { request&.submitted? } ):



20
# File 'app/models/request_expense.rb', line 20

validates :estimated_amount, :estimated_currency, presence: true, if: -> { request&. }

#estimated_currency ⇒ String

Returns:

  • (String)

Validations (if => -> { request&.submitted? } ):



20
# File 'app/models/request_expense.rb', line 20

validates :estimated_amount, :estimated_currency, presence: true, if: -> { request&. }

#subject ⇒ String

Returns:

  • (String)

Validations:



19
# File 'app/models/request_expense.rb', line 19

validates :request, :subject, presence: true

#total_amount ⇒ Decimal

Returns:

  • (Decimal)

Validations (if => -> { request&.reimbursement&.submitted? } ):



22
# File 'app/models/request_expense.rb', line 22

validates :total_amount, presence: true, if: -> { request&.reimbursement&. }

#updated_at ⇒ DateTime

Returns:

  • (DateTime)


185
# File 'db/schema.rb', line 185

t.datetime "updated_at"

Class Method Details

.by_attr_for_requests ⇒ ActiveRecord::Relation<RequestExpense>

A relation of RequestExpenses that are by attr for requests. Active Record Scope

Returns:

See Also:



30
31
32
33
34
# File 'app/models/request_expense.rb', line 30

scope :by_attr_for_requests, lambda { |attr, req_ids|
  currency_field = RequestExpense.currency_field_for(attr)
  amount_field = :"#{attr}_amount"
  group(currency_field).where(["#{amount_field} is not null and request_id in (?)", req_ids]).order(currency_field)
}

.currency_field_for(attr) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/request_expense.rb', line 47

def self.currency_field_for(attr)
  case attr
  when :estimated
    :estimated_currency
  when :approved
    :approved_currency
  when :total
    :estimated_currency
  when :authorized
    :approved_currency
  end
end

Instance Method Details

#authorized_can_be_calculated? ⇒ Boolean

Checks whether authorized_amount can be automatically calculated or needs to be manually set.

It can only be automated if the currency for both total and approved are the same, otherwise exchange rates and other rules may come into play

Returns:

  • (Boolean) —

    true whether automatic calculation is possible



67
68
69
# File 'app/models/request_expense.rb', line 67

def authorized_can_be_calculated?
  !total_currency.blank? && !approved_currency.blank? && total_currency == approved_currency
end

#authorized_currency ⇒ Object

Convenience method that simply aliases approved_currency since currency cannot be changed after approval



43
44
45
# File 'app/models/request_expense.rb', line 43

def authorized_currency
  send(RequestExpense.currency_field_for(:authorized))
end

#false_event ⇒ Object

Alias for Request#event

Returns:

  • (Object) —

    Request#false_event

See Also:



17
# File 'app/models/request_expense.rb', line 17

delegate :event, to: :request, prefix: false

#false_reimbursement ⇒ Object

Alias for Request#reimbursement

Returns:

  • (Object) —

    Request#false_reimbursement

See Also:



16
# File 'app/models/request_expense.rb', line 16

delegate :reimbursement, to: :request, prefix: false

#request ⇒ ReimbursableRequest

Validations:



12
13
14
# File 'app/models/request_expense.rb', line 12

belongs_to :request, inverse_of: :expenses,
class_name: 'ReimbursableRequest',
foreign_key: 'request_id'

#total_currency ⇒ Object

Convenience method that simply aliases approved_currency since currency cannot be changed after approval



38
39
40
# File 'app/models/request_expense.rb', line 38

def total_currency
  send(RequestExpense.currency_field_for(:total))
end