Initial commit
This commit is contained in:
52
app/models/concerns/fee_chargeable.rb
Normal file
52
app/models/concerns/fee_chargeable.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
# encoding: UTF-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
module FeeChargeable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
attr_readonly :amount, :fee
|
||||
|
||||
validates :amount, presence: true, numericality: { greater_than: 0.to_d }
|
||||
validates :fee, presence: true, numericality: { greater_than_or_equal_to: 0.to_d }
|
||||
|
||||
if self <= Deposit
|
||||
before_validation on: :create do
|
||||
next unless currency
|
||||
self.fee ||= currency.deposit_fee
|
||||
self.amount = amount.to_d - fee
|
||||
end
|
||||
|
||||
validates :fee, numericality: { less_than: :amount }, if: -> (record) { record.amount.to_d > 0.to_d }
|
||||
end
|
||||
|
||||
if self <= Withdraw
|
||||
attr_readonly :sum
|
||||
|
||||
before_validation on: :create do
|
||||
next unless currency
|
||||
|
||||
self.sum ||= 0.to_d
|
||||
self.fee ||= currency.withdraw_fee
|
||||
self.amount = sum - fee
|
||||
end
|
||||
|
||||
validates :sum,
|
||||
presence: true,
|
||||
numericality: { greater_than: 0.to_d },
|
||||
precision: { less_than_or_eq_to: ->(w) { w.currency.precision } }
|
||||
|
||||
validates :amount,
|
||||
precision: { less_than_or_eq_to: ->(w) { w.currency.precision } }
|
||||
|
||||
validate on: :create do
|
||||
next if !account || [sum, amount, fee].any?(&:blank?)
|
||||
|
||||
if sum > account.balance || (amount + fee) > sum
|
||||
# raise ::Account::AccountError, 'Account balance is insufficient'
|
||||
errors.add(:id, 'Account balance is insufficient')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
23
app/models/concerns/model_caching.rb
Normal file
23
app/models/concerns/model_caching.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
module ModelCaching
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def write_cache(action, value, expiration_time)
|
||||
Rails.cache.write(cache_key_generator(action), value, expires_in: expiration_time.seconds)
|
||||
end
|
||||
|
||||
def read_cache(action)
|
||||
Rails.cache.read(cache_key_generator(action))
|
||||
end
|
||||
|
||||
def delete_cache(action)
|
||||
Rails.cache.delete(cache_key_generator(action))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cache_key_generator(action)
|
||||
class_name = self.class.name.to_s
|
||||
object_id = id.to_s
|
||||
"#{class_name}_#{object_id}_#{action}"
|
||||
end
|
||||
end
|
||||
23
app/models/concerns/precision_validator.rb
Normal file
23
app/models/concerns/precision_validator.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
# encoding: UTF-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
class PrecisionValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
return unless options.key?(:less_than_or_eq_to) && value.present?
|
||||
|
||||
precision = if options[:less_than_or_eq_to].respond_to?(:call)
|
||||
options[:less_than_or_eq_to].call(record)
|
||||
else
|
||||
options[:less_than_or_eq_to]
|
||||
end
|
||||
|
||||
unless value.is_a?(Numeric)
|
||||
record.errors.add(attribute, 'must be a number')
|
||||
return
|
||||
end
|
||||
|
||||
unless value.round(precision) == value
|
||||
record.errors.add(attribute, "precision must be less than or equal to #{precision}")
|
||||
end
|
||||
end
|
||||
end
|
||||
17
app/models/concerns/tid_identifiable.rb
Normal file
17
app/models/concerns/tid_identifiable.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
# encoding: UTF-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
module TIDIdentifiable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
validates :tid, presence: true, uniqueness: { case_sensitive: false }
|
||||
|
||||
before_validation do
|
||||
next unless tid.blank?
|
||||
begin
|
||||
self.tid = "TID#{SecureRandom.hex(5).upcase}"
|
||||
end while self.class.where(tid: tid).any?
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user