Class: Charity
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Charity
- Includes:
- Models::Auditable, PgSearch::Model
- Defined in:
- app/models/charity.rb
Overview
== Schema Information
Table name: charities
Database name: primary
id :integer not null, primary key
address :text
category :string
cause :text
efficiency :decimal(, )
fund :string
name :string
phone :string
status :enum default("active")
website :string
created_at :datetime not null
updated_at :datetime not null
company_id :integer
creator_id :integer
supplier_id :integer
updater_id :integer
Indexes
index_charities_on_supplier_id (supplier_id)
Foreign Keys
fk_rails_... (supplier_id => parties.id)
Constant Summary collapse
- CATEGORIES =
Categories.
['Direct Relief', 'Health & Disease', 'Hunger', 'Homelessness', 'Environmental', 'Victims of Abuse', 'Animals', 'Community', 'Government Related', 'Youth', 'Global Aid'].sort
- MATCH_FACTOR =
Match factor.
2- WITH_DONATION_TO_DATE_SQL =
With donation to date sql.
<<-SQL.squish.freeze select sum(ra.amount)*#{MATCH_FACTOR} from reward_allocations ra where ra.charity_id = charities.id SQL
- WITH_DONATION_PROCESS_REWARD_SQL =
With donation process reward sql.
<<-SQL.squish.freeze select sum(ra.amount)*#{MATCH_FACTOR} from reward_allocations ra inner join employee_reviews er on er.id = ra.employee_review_id where ra.charity_id = charities.id and er.state = 'process_reward' SQL
Constants included from Models::Auditable
Models::Auditable::ALWAYS_IGNORED
Constants included from Models::Schedulable
Models::Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #category ⇒ String readonly
- #fund ⇒ String readonly
- #name ⇒ String readonly
- #phone ⇒ String readonly
- #website ⇒ String readonly
Has many collapse
Belongs to collapse
Methods included from Models::Auditable
Class Method Summary collapse
- .categories_for_select ⇒ void
- .ransackable_scopes(_auth_object = nil) ⇒ void
-
.select_donations_to_date ⇒ ActiveRecord::Relation<Charity>
A relation of Charities that are select donations to date.
-
.select_donations_to_process ⇒ ActiveRecord::Relation<Charity>
A relation of Charities that are select donations to process.
- .top_charities(limit: 3) ⇒ void
-
.with_company_id ⇒ ActiveRecord::Relation<Charity>
A relation of Charities that are with company id.
-
.with_donations_to_process ⇒ ActiveRecord::Relation<Charity>
A relation of Charities that are with donations to process.
Instance Method Summary collapse
Methods included from Models::Auditable
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransortable_attributes, #to_relation
Methods included from Models::Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#category ⇒ String (readonly)
71 |
# File 'app/models/charity.rb', line 71 validates :category, inclusion: { in: CATEGORIES }, if: :category |
#fund ⇒ String (readonly)
69 |
# File 'app/models/charity.rb', line 69 validates :fund, uniqueness: { scope: :name }, if: :fund |
#name ⇒ String (readonly)
67 |
# File 'app/models/charity.rb', line 67 validates :name, presence: true |
#phone ⇒ String (readonly)
73 |
# File 'app/models/charity.rb', line 73 validates :phone, phone_format: true, if: :phone |
#website ⇒ String (readonly)
75 |
# File 'app/models/charity.rb', line 75 validates :website, format: ContactPoint::WEBSITE_REGEX, if: :website |
Class Method Details
.categories_for_select ⇒ void
This method returns an undefined value.
122 123 124 |
# File 'app/models/charity.rb', line 122 def self.categories_for_select CATEGORIES end |
.ransackable_scopes(_auth_object = nil) ⇒ void
105 106 107 |
# File 'app/models/charity.rb', line 105 def self.ransackable_scopes(_auth_object = nil) %i[search with_company_id] end |
.select_donations_to_date ⇒ ActiveRecord::Relation<Charity>
A relation of Charities that are select donations to date. Active Record Scope
93 |
# File 'app/models/charity.rb', line 93 scope :select_donations_to_date, -> { select("charities.*,(#{WITH_DONATION_TO_DATE_SQL}) as donations_to_date") } |
.select_donations_to_process ⇒ ActiveRecord::Relation<Charity>
A relation of Charities that are select donations to process. Active Record Scope
94 |
# File 'app/models/charity.rb', line 94 scope :select_donations_to_process, -> { select("charities.*, (#{WITH_DONATION_PROCESS_REWARD_SQL}) as donations_to_process") } |
.top_charities(limit: 3) ⇒ void
113 114 115 116 117 118 119 |
# File 'app/models/charity.rb', line 113 def self.top_charities(limit: 3) Charity.joins(:reward_allocations) .group(:name, :fund, :id) .select('name,fund,charities.id,count(reward_allocations.id),sum(amount)') .order('sum(amount) desc, count(reward_allocations.id) desc') .limit(limit) end |
.with_company_id ⇒ ActiveRecord::Relation<Charity>
A relation of Charities that are with company id. Active Record Scope
96 |
# File 'app/models/charity.rb', line 96 scope :with_company_id, ->(cid) { where('charities.company_id = ? or charities.company_id IS NULL', cid) } |
.with_donations_to_process ⇒ ActiveRecord::Relation<Charity>
A relation of Charities that are with donations to process. Active Record Scope
95 |
# File 'app/models/charity.rb', line 95 scope :with_donations_to_process, -> { where("(#{WITH_DONATION_PROCESS_REWARD_SQL}) > 0") } |
Instance Method Details
#reward_allocations ⇒ ActiveRecord::Relation<RewardAllocation>
59 |
# File 'app/models/charity.rb', line 59 has_many :reward_allocations |
#to_s ⇒ String
127 128 129 |
# File 'app/models/charity.rb', line 127 def to_s [try(:name), try(:fund)].compact.join(' - ') end |
#total_donations_process_reward ⇒ void
This method returns an undefined value.
137 138 139 |
# File 'app/models/charity.rb', line 137 def total_donations_process_reward reward_allocations.joins(:employee_review).where(employee_review: { state: 'process_reward' }).map(&:total_donation) end |
#total_donations_to_date ⇒ void
This method returns an undefined value.
132 133 134 |
# File 'app/models/charity.rb', line 132 def total_donations_to_date reward_allocations.sum(&:total_donation) end |