Class: RewardAllocation

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

Overview

== Schema Information

Table name: reward_allocations
Database name: primary

id :integer not null, primary key
amount :decimal(8, 2) not null
charity_name :string not null
notes :text
created_at :datetime not null
updated_at :datetime not null
charity_id :integer
creator_id :integer
employee_review_id :integer not null
updater_id :integer

Indexes

index_reward_allocations_on_charity_id (charity_id)
index_reward_allocations_on_employee_review_id (employee_review_id)

Foreign Keys

fk_rails_... (charity_id => charities.id)
fk_rails_... (employee_review_id => employee_reviews.id) ON DELETE => cascade

Constant Summary

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#amountBigDecimal (readonly)

Returns:

  • (BigDecimal)


34
# File 'app/models/reward_allocation.rb', line 34

validates :amount, :charity, presence: true

Instance Method Details

#charities_for_selectArray<Array<String>>

Returns:

  • (Array<Array<String>>)


37
38
39
# File 'app/models/reward_allocation.rb', line 37

def charities_for_select
  Charity.active.map { |c| [c.to_s, "#{c}|#{c.id}"] }
end

#charityCharity?

Returns:

Validations:



32
# File 'app/models/reward_allocation.rb', line 32

belongs_to :charity, optional: true

#charity_comboString?

Returns:

  • (String, nil)


49
50
51
52
53
# File 'app/models/reward_allocation.rb', line 49

def charity_combo
  return "#{charity}|#{charity_id}" if charity

  charity_name
end

#charity_combo=(val) ⇒ String

Parameters:

  • val (String)

    "name|id" pair from the charity select

Returns:

  • (String)


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

def charity_combo=(val)
  self.charity_name, self.charity_id = val.split('|')
  self.charity = Charity.create(name: charity_name, status: 'review') if charity_id.blank? && charity_name.present?
end

#company_matchObject

Future proofing so that if in the future we want to increase match to certain charities we can do so



56
57
58
# File 'app/models/reward_allocation.rb', line 56

def company_match
  amount || 0.0
end

#employee_reviewEmployeeReview?

Returns:



30
# File 'app/models/reward_allocation.rb', line 30

belongs_to :employee_review, inverse_of: :reward_allocations, optional: true

#total_donationBigDecimal

Returns:

  • (BigDecimal)


61
62
63
# File 'app/models/reward_allocation.rb', line 61

def total_donation
  company_match + (amount || 0.0)
end