Merge branch 'add-support-for-multicurrency' into 'master'
Add support for multicurrency See merge request saburly/gangsta/roraccounting!5
This commit was merged in pull request #5.
This commit is contained in:
40
app/controllers/chip_values_controller.rb
Normal file
40
app/controllers/chip_values_controller.rb
Normal file
@@ -0,0 +1,40 @@
|
||||
class ChipValuesController < ApplicationController
|
||||
|
||||
def create
|
||||
if ChipValue.create_full_chip_value(chip_value_params)
|
||||
json_response all_chips_response
|
||||
else
|
||||
error_response :bad_request
|
||||
end
|
||||
rescue StandardError
|
||||
error_response :bad_request
|
||||
end
|
||||
|
||||
def update
|
||||
if ChipValue.update_full_chip_value(chip_value_params)
|
||||
json_response all_chips_response
|
||||
end
|
||||
rescue StandardError
|
||||
error_response :bad_request
|
||||
end
|
||||
|
||||
def destroy
|
||||
if ChipValue.destroy_full_chip_value(params[:id])
|
||||
json_response all_chips_response
|
||||
else
|
||||
error_response :bad_request
|
||||
end
|
||||
rescue StandardError
|
||||
error_response :bad_request
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def chip_value_params
|
||||
params.require(:chip_value).permit(:id, :base_chip_id, :secondary_chip_id, :value)
|
||||
end
|
||||
|
||||
def all_chips_response
|
||||
Chip.all.order(:name).to_json(include: :base_chip_values)
|
||||
end
|
||||
end
|
||||
29
app/controllers/chips_controller.rb
Normal file
29
app/controllers/chips_controller.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
class ChipsController < ApplicationController
|
||||
def index
|
||||
json_response Chip.all.order(:name).to_json(include: :base_chip_values)
|
||||
end
|
||||
|
||||
def create
|
||||
chip = Chip.new(chip_params)
|
||||
if chip.save
|
||||
json_response chip
|
||||
else
|
||||
error_response :bad_request
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
chip_id = params[:id]
|
||||
if chip_id.present? && Chip.destroy(chip_id)
|
||||
index
|
||||
else
|
||||
error_response :bad_request
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def chip_params
|
||||
params.require(:chip).permit(:id, :name, :symbol, :enabled)
|
||||
end
|
||||
end
|
||||
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