handle chip and chip values operations
This commit is contained in:
23
app/models/chip.rb
Normal file
23
app/models/chip.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
class Chip < ApplicationRecord
|
||||
has_many :base_chip_values, class_name: "ChipValue", foreign_key: "base_chip_id", dependent: :delete_all
|
||||
has_many :secondary_chip_values, class_name: "ChipValue", foreign_key: "secondary_chip_id", dependent: :delete_all
|
||||
|
||||
validates :name, uniqueness: true
|
||||
validates :name, :symbol, presence: true
|
||||
|
||||
after_create :add_chip_values
|
||||
|
||||
private
|
||||
|
||||
# When new chip(currency) is added, add new record to the chip_values table for every existing chip(currency)
|
||||
def add_chip_values
|
||||
Chip.all.each do |chip|
|
||||
next if chip.name == name
|
||||
|
||||
transaction do
|
||||
ChipValue.new(base_chip: self, secondary_chip: chip, value: 0).save
|
||||
ChipValue.new(base_chip: chip, secondary_chip: self, value: 0).save
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
53
app/models/chip_value.rb
Normal file
53
app/models/chip_value.rb
Normal file
@@ -0,0 +1,53 @@
|
||||
class ChipValue < ApplicationRecord
|
||||
belongs_to :base_chip, class_name: 'Chip'
|
||||
belongs_to :secondary_chip, class_name: 'Chip'
|
||||
|
||||
validates :base_chip_id, uniqueness: { scope: :secondary_chip_id }
|
||||
|
||||
def self.create_full_chip_value(params)
|
||||
base_chip_id = params[:base_chip_id]
|
||||
secondary_chip_id = params[:secondary_chip_id]
|
||||
value = params[:value].to_f
|
||||
|
||||
chips = Chip.where(id: [base_chip_id, secondary_chip_id])
|
||||
base_chip = chips.first
|
||||
secondary_chip = chips.second
|
||||
|
||||
mirrored_value = value.zero? ? 0 : (1 / value)
|
||||
|
||||
base_chip_value = ChipValue.new(base_chip: base_chip, secondary_chip: secondary_chip, value: value)
|
||||
mirrored_chip_value = ChipValue.new(base_chip: secondary_chip, secondary_chip: base_chip, value: mirrored_value)
|
||||
|
||||
transaction do
|
||||
base_chip_value.save
|
||||
mirrored_chip_value.save
|
||||
end
|
||||
end
|
||||
|
||||
def self.update_full_chip_value(params)
|
||||
id = params[:id]
|
||||
new_value = params[:value].to_f
|
||||
mirrored_value = new_value.zero? ? 0 : (1 / new_value)
|
||||
|
||||
chip_value = ChipValue.find(id)
|
||||
mirrored_chip_value = ChipValue.where(base_chip_id: chip_value.secondary_chip_id, secondary_chip_id: chip_value.base_chip_id).first
|
||||
|
||||
chip_value.value = new_value
|
||||
mirrored_chip_value.value = mirrored_value
|
||||
|
||||
transaction do
|
||||
chip_value.save
|
||||
mirrored_chip_value.save
|
||||
end
|
||||
end
|
||||
|
||||
def self.destroy_full_chip_value(id)
|
||||
chip_value = ChipValue.find(id)
|
||||
mirrored_chip_value = ChipValue.where(base_chip_id: chip_value.secondary_chip_id, secondary_chip_id: chip_value.base_chip_id).first
|
||||
|
||||
transaction do
|
||||
chip_value.destroy
|
||||
mirrored_chip_value.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user