Class: RequestExpense
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- RequestExpense
- 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
- #approved_amount ⇒ Decimal
- #approved_currency ⇒ String
- #authorized_amount ⇒ Decimal
- #created_at ⇒ DateTime
- #description ⇒ String
- #estimated_amount ⇒ Decimal
- #estimated_currency ⇒ String
- #subject ⇒ String
- #total_amount ⇒ Decimal
- #updated_at ⇒ DateTime
Belongs to collapse
Delegated Instance Attributes collapse
-
#false_event ⇒ Object
Alias for Request#event.
-
#false_reimbursement ⇒ Object
Alias for Request#reimbursement.
Class Method Summary collapse
-
.by_attr_for_requests ⇒ ActiveRecord::Relation<RequestExpense>
A relation of RequestExpenses that are by attr for requests.
- .currency_field_for(attr) ⇒ Object
Instance Method Summary collapse
-
#authorized_can_be_calculated? ⇒ Boolean
Checks whether authorized_amount can be automatically calculated or needs to be manually set.
-
#authorized_currency ⇒ Object
Convenience method that simply aliases approved_currency since currency cannot be changed after approval.
-
#total_currency ⇒ Object
Convenience method that simply aliases approved_currency since currency cannot be changed after approval.
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes
Instance Attribute Details
#approved_amount ⇒ 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
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
Validations (if => -> { request&.reimbursement&.submitted? } ):
23 |
# File 'app/models/request_expense.rb', line 23 validates :authorized_amount, presence: true, if: -> { request&.reimbursement&.submitted? } |
#created_at ⇒ DateTime
184 |
# File 'db/schema.rb', line 184 t.datetime "created_at" |
#description ⇒ String
179 |
# File 'db/schema.rb', line 179 t.string "description" |
#estimated_amount ⇒ Decimal
Validations (if => -> { request&.submitted? } ):
20 |
# File 'app/models/request_expense.rb', line 20 validates :estimated_amount, :estimated_currency, presence: true, if: -> { request&.submitted? } |
#estimated_currency ⇒ String
Validations (if => -> { request&.submitted? } ):
20 |
# File 'app/models/request_expense.rb', line 20 validates :estimated_amount, :estimated_currency, presence: true, if: -> { request&.submitted? } |
#subject ⇒ String
Validations:
19 |
# File 'app/models/request_expense.rb', line 19 validates :request, :subject, presence: true |
#total_amount ⇒ Decimal
Validations (if => -> { request&.reimbursement&.submitted? } ):
22 |
# File 'app/models/request_expense.rb', line 22 validates :total_amount, presence: true, if: -> { request&.reimbursement&.submitted? } |
#updated_at ⇒ 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
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
67 68 69 |
# File 'app/models/request_expense.rb', line 67 def !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 send(RequestExpense.currency_field_for(:authorized)) end |
#false_event ⇒ Object
Alias for Request#event
17 |
# File 'app/models/request_expense.rb', line 17 delegate :event, to: :request, prefix: false |
#false_reimbursement ⇒ Object
Alias for Request#reimbursement
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 |