allow negative amounts to swap creditor and debtor
This commit is contained in:
parent
bd5f444869
commit
8293662fff
2 changed files with 11 additions and 6 deletions
|
@ -24,6 +24,14 @@ class Transaction < ActiveRecord::Base
|
||||||
validates :amount, numericality: { greater_than: 0 }
|
validates :amount, numericality: { greater_than: 0 }
|
||||||
validate :different_debtor_creditor
|
validate :different_debtor_creditor
|
||||||
|
|
||||||
|
def initialize *args, **kwargs
|
||||||
|
super *args, **kwargs
|
||||||
|
if amount < 0
|
||||||
|
self.creditor, self.debtor = debtor, creditor
|
||||||
|
self.amount = self.amount.abs
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def peer_of(user)
|
def peer_of(user)
|
||||||
return creditor if user == debtor
|
return creditor if user == debtor
|
||||||
return debtor if user == creditor
|
return debtor if user == creditor
|
||||||
|
|
|
@ -3,11 +3,7 @@
|
||||||
= render 'partials/form_errors', object: @transaction
|
= render 'partials/form_errors', object: @transaction
|
||||||
|
|
||||||
= form_for @transaction, remote: true, html: { class: "pure-form form-inline" } do |f|
|
= form_for @transaction, remote: true, html: { class: "pure-form form-inline" } do |f|
|
||||||
- if current_user.penning
|
= f.hidden_field :debtor, value: @user.name
|
||||||
= f.collection_select :debtor, User.all, :name, :name,
|
|
||||||
{ selected: @transaction.debtor.try(:name) || current_user.name }, { class: 'select2-selector form-control'}
|
|
||||||
- else
|
|
||||||
= f.hidden_field :debtor, value: current_user.name
|
|
||||||
= f.select :creditor,
|
= f.select :creditor,
|
||||||
options_from_collection_for_select(User.all.order(:name), :name, :name),
|
options_from_collection_for_select(User.all.order(:name), :name, :name),
|
||||||
{ include_blank: true },
|
{ include_blank: true },
|
||||||
|
@ -19,6 +15,7 @@
|
||||||
%span.input-group-addon
|
%span.input-group-addon
|
||||||
%span.glyphicon.glyphicon-euro
|
%span.glyphicon.glyphicon-euro
|
||||||
= f.number_field :euros, value: amount(@transaction.amount),
|
= f.number_field :euros, value: amount(@transaction.amount),
|
||||||
placeholder: "Bedrag", step: 0.01, min: 0.01, class: "form-control", size: 20
|
placeholder: "Bedrag", step: 0.01, min: (0.01 unless current_user.penning),
|
||||||
|
class: "form-control", size: 20
|
||||||
-# placeholder: "Bedrag", step: 0.01, min: 0.01, class: "pure-group-addon price", size: 20, required: true
|
-# placeholder: "Bedrag", step: 0.01, min: 0.01, class: "pure-group-addon price", size: 20, required: true
|
||||||
= f.submit "Send it!", class: "pure-button pure-button-primary"
|
= f.submit "Send it!", class: "pure-button pure-button-primary"
|
||||||
|
|
Loading…
Reference in a new issue