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 turbolinks
//= require_tree . //= require_tree .
$(document).ready(function() { ready = function() {
$(".select2-selector").select2(); $(".select2-selector").select2();
}); }
$(document).ready(ready)
$(document).on('page:load', ready)

View file

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

View file

@ -1,9 +1,11 @@
require 'factory_girl' require 'factory_girl'
require 'faker'
task :sow => :environment do task :sow => :environment do
users = FactoryGirl.create_list(:user, 20) users = FactoryGirl.create_list(:user, 20)
100.times do 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
end end