mirror of
https://github.com/internetee/registry.git
synced 2025-06-07 21:25:39 +02:00
added action
This commit is contained in:
parent
d25847571f
commit
43518b5832
3 changed files with 26 additions and 6 deletions
|
@ -16,12 +16,12 @@ module Admin
|
||||||
|
|
||||||
def update
|
def update
|
||||||
if @account.valid?
|
if @account.valid?
|
||||||
@sum = params[:account][:balance].to_f - @account.balance
|
action = Actions::AccountActivityCreate.new(@account, params[:account][:balance],
|
||||||
action = Actions::AccountActivityCreate.new(@account, @sum, params[:description],
|
params[:description], AccountActivity::UPDATE_CREDIT)
|
||||||
AccountActivity::UPDATE_CREDIT)
|
redirect_to admin_accounts_path, notice: t('.updated') and return if action.call
|
||||||
redirect_to admin_accounts_path, notice: t('.updated') if action.call
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
flash[:alert] = t('invalid_balance')
|
||||||
render 'edit'
|
render 'edit'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,35 @@
|
||||||
module Actions
|
module Actions
|
||||||
class AccountActivityCreate
|
class AccountActivityCreate
|
||||||
def initialize(account, sum, description, type)
|
def initialize(account, new_balance, description, type)
|
||||||
@account = account
|
@account = account
|
||||||
@sum = sum
|
@new_balance = new_balance
|
||||||
@description = description
|
@description = description
|
||||||
@type = type
|
@type = type
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
|
validate_new_balance
|
||||||
|
return false if @error
|
||||||
|
|
||||||
|
calc_sum
|
||||||
create_activity
|
create_activity
|
||||||
commit
|
commit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def calc_sum
|
||||||
|
@sum = @new_balance.to_f - @account.balance
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_new_balance
|
||||||
|
return if @new_balance.blank?
|
||||||
|
|
||||||
|
begin
|
||||||
|
!Float(@new_balance).nil?
|
||||||
|
rescue StandardError
|
||||||
|
@error = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def create_activity
|
def create_activity
|
||||||
@activity = AccountActivity.new(account: @account,
|
@activity = AccountActivity.new(account: @account,
|
||||||
sum: @sum,
|
sum: @sum,
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
en:
|
en:
|
||||||
edit_balance: Edit balance
|
edit_balance: Edit balance
|
||||||
|
invalid_balance: The balance must be a number
|
||||||
|
|
||||||
admin:
|
admin:
|
||||||
accounts:
|
accounts:
|
||||||
index:
|
index:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue