From 8293662fffa64497457216109ca10edaf67d0373 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Fri, 11 Sep 2015 15:16:20 +0200 Subject: [PATCH] allow negative amounts to swap creditor and debtor --- app/models/transaction.rb | 8 ++++++++ app/views/transactions/_new.html.haml | 9 +++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/models/transaction.rb b/app/models/transaction.rb index dc042a9..3c7b8a5 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -24,6 +24,14 @@ class Transaction < ActiveRecord::Base validates :amount, numericality: { greater_than: 0 } 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) return creditor if user == debtor return debtor if user == creditor diff --git a/app/views/transactions/_new.html.haml b/app/views/transactions/_new.html.haml index ee751b1..56947e0 100644 --- a/app/views/transactions/_new.html.haml +++ b/app/views/transactions/_new.html.haml @@ -3,11 +3,7 @@ = render 'partials/form_errors', object: @transaction = form_for @transaction, remote: true, html: { class: "pure-form form-inline" } do |f| - - if current_user.penning - = 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.hidden_field :debtor, value: @user.name = f.select :creditor, options_from_collection_for_select(User.all.order(:name), :name, :name), { include_blank: true }, @@ -19,6 +15,7 @@ %span.input-group-addon %span.glyphicon.glyphicon-euro = 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 = f.submit "Send it!", class: "pure-button pure-button-primary"