Rewrite transaction controller

This commit is contained in:
benji 2015-09-08 21:07:00 +02:00
parent 272923d08d
commit bb5a4019e6
5 changed files with 23 additions and 27 deletions

View file

@ -17,7 +17,9 @@
//= require turbolinks
//= require_tree .
$(document).ready(function() {
$(".select2-selector").select2();
});
ready = function() {
$(".select2-selector").select2();
}
$(document).ready(ready)
$(document).on('page:load', ready)

View file

@ -1,7 +1,6 @@
class TransactionsController < ApplicationController
def index
p params
respond_to do |format|
format.html
format.json { render json: TransactionDatatable.new(view_context) }
@ -13,10 +12,18 @@ class TransactionsController < ApplicationController
end
def create
creditor = User.find params.require(:transaction).require(:creditor)
debtor = current_user
amount = params.require(:transaction).require(:amount)
@transaction = Transaction.create debtor: debtor, creditor: creditor, amount: amount, origin: I18n.t('origin.created_by_user'), message: "Transaction by #{debtor.name} to #{creditor.name}"
@transaction = current_user.outgoing_transactions.build transaction_params.merge(origin: I18n.t('origin.created_by_user'))
if @transaction.save
redirect_to current_user
else
render 'new'
end
end
private
def transaction_params
params.require(:transaction).permit(:creditor_id, :amount, :message)
end
end

View file

@ -1,17 +0,0 @@
%table
%tbody
%tr
%th Debtor
%td= @transaction.debtor.name
%tr
%th Creditor
%td= @transaction.creditor.name
%tr
%th Amount
%td= @transaction.amount
%tr
%th Origin
%td= @transaction.origin
%tr
%th Message
%td= @transaction.message

View file

@ -1,4 +1,6 @@
= @transaction.errors.full_messages.join(", ")
= form_for @transaction do |f|
= f.collection_select :creditor, User.all, :id, :name, {}, { class: 'select2-selector' }
= f.collection_select :creditor_id, User.all, :id, :name, {}, { class: 'select2-selector' }
= f.number_field :amount
= f.text_field :message, required: true
= f.submit "Send it!"

View file

@ -1,9 +1,11 @@
require 'factory_girl'
require 'faker'
task :sow => :environment do
users = FactoryGirl.create_list(:user, 20)
100.times do
FactoryGirl.create :transaction, debtor: users.sample, creditor: users.sample
sample_users = users.sample(2)
FactoryGirl.create :transaction, debtor: sample_users[0], creditor: sample_users[1], amount: 1 + rand(100)
end
end