docbrown/app/models/credit.rb
rhymes c356983de8
Replace activerecord-import with Rails 6 insert_all and upsert_all (#8407)
* Replace Credits.import with .insert_all

* Fix Notifications::Reactions::Send and replace .import with .upsert to avoid race conditions

* Replace .import! with .upsert_all in Notifications::NotifiableAction::Send

* Replace PodcastEpisode.import! with upsert

* Stop testing gems, test the code

* Remove activerecord-import

* Fix redundancies

* clarify comment

* Replace Credit.import with insert_all

* hello knapsack

* Do not try to insert if there is nothing to
2020-06-12 11:16:06 +02:00

51 lines
1.5 KiB
Ruby

class Credit < ApplicationRecord
attr_accessor :number_to_purchase
belongs_to :user, optional: true
belongs_to :organization, optional: true
belongs_to :purchase, polymorphic: true, optional: true
scope :spent, -> { where(spent: true) }
scope :unspent, -> { where(spent: false) }
%i[user organization].each do |type|
counter_culture type,
column_name: ->(model) { "#{model.spent ? 'spent' : 'unspent'}_credits_count" },
column_names: {
["credits.spent = ?", true] => "spent_credits_count",
["credits.spent = ?", false] => "unspent_credits_count",
["credits.id > ?", 0] => "credits_count"
}
end
def self.add_to(user_or_org, amount)
return unless amount.positive?
now = Time.current
association_id = "#{user_or_org.class.name.underscore}_id"
attributes = Array.new(amount) do
{
association_id => user_or_org.id,
:created_at => now,
:updated_at => now
}
end
insert_all(attributes)
update_cache_columns(user_or_org)
end
def self.remove_from(user_or_org, amount)
user_or_org.credits.where(spent: false).limit(amount).delete_all
update_cache_columns(user_or_org)
end
def self.update_cache_columns(user_or_org)
user_or_org.update(
credits_count: user_or_org.credits.size,
spent_credits_count: user_or_org.credits.where(spent: true).size,
unspent_credits_count: user_or_org.credits.where(spent: false).size,
)
end
end