diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index ca57169..e33c2a5 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -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) diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index d451478..15ddebb 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -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 diff --git a/app/views/transactions/create.html.haml b/app/views/transactions/create.html.haml deleted file mode 100644 index fa9cbf4..0000000 --- a/app/views/transactions/create.html.haml +++ /dev/null @@ -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 diff --git a/app/views/transactions/new.html.haml b/app/views/transactions/new.html.haml index 94b2670..d65c6d5 100644 --- a/app/views/transactions/new.html.haml +++ b/app/views/transactions/new.html.haml @@ -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!" diff --git a/lib/tasks/devseed.rake b/lib/tasks/devseed.rake index 0f8ff65..eccac63 100644 --- a/lib/tasks/devseed.rake +++ b/lib/tasks/devseed.rake @@ -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