Skip to content

Commit

Permalink
working transfers model
Browse files Browse the repository at this point in the history
  • Loading branch information
SelenaSmall committed Sep 20, 2015
1 parent 694cb3a commit 1d4caf0
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/assets/javascripts/transfer.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/transfer.scss
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Transfer controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
51 changes: 51 additions & 0 deletions app/controllers/transfer_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,51 @@
class TransferController < ApplicationController
before_action :set_mybudget
before_action :set_transfer, only: [:show, :edit, :update, :destroy]

def index
@transfers = @mybudget.transfers.all
end

def new
@transfer = @mybudget.transfers.build
end

def edit
end

def create
@transfer = @mybudget.transfers.build(transfer_params)

respond_to do |format|
if @transfer.save
format.html { redirect_to [@mybudget, @transfers], notice: 'transfer was successfully created.' }
format.json { render :show, status: :created, location: @transfer }
format.js {redirect_via_turbolinks_to [@mybudget, @transfers]}
else
format.html { render :new }
format.json { render json: @transfer.errors, status: :unprocessable_entity }
end
end
end

def update
respond_to do |format|
if @transfer.update(transfer_params)
format.html { redirect_to edit_transfer_path(@transfer)}
end
end
end

private
def set_mybudget
@mybudget = Mybudget.find(params[:mybudget_id])
end

def set_transfer
@transfer = Transfer.find(params[:id])
end

def transfer_params
params.require(:transfer).permit(:transfer, :amount)
end
end
2 changes: 2 additions & 0 deletions app/helpers/transfer_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
module TransferHelper
end
1 change: 1 addition & 0 deletions app/models/mybudget.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
class Mybudget < ActiveRecord::Base class Mybudget < ActiveRecord::Base
has_many :incomes has_many :incomes
has_many :expenses has_many :expenses
has_many :transfers
end end
3 changes: 3 additions & 0 deletions app/models/transfer.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
class Transfer < ActiveRecord::Base
belongs_to :mybudget
end
32 changes: 32 additions & 0 deletions app/views/mybudgets/_transfers.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="bordered-form" >
<h2>transfers</h2>
<div> - this is going to be an transfers show page which will list all of the transfers and allow the user to add new transfer values as the week goes. In the long run this will help keep track of projections.</div>

<!--<%= link_to 'Add transfer', new_mybudget_transfer_path(@mybudget, @transfer) %>-->
<%= link_to new_mybudget_transfer_path(@mybudget, @transfer),
remote: true, id: "new-item-link", class: "btn btn-sm btn-default round" do %> New
<%end%>


<table class="table" >
<thead>
<tr>
<th>transfers</th>
</tr>
</thead>
<tbody>
<% @mybudget.transfers.each do |transfer| %>
<tr>
<div id="transfers"></div>
<td class="table-items"><%= transfer.transfer %></td>
<td class="amount">$<%= transfer.amount %></td>
<td></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<div>TOTAL transfer</div>
<div class="total-output"><%= Transfer.sum(:amount) %>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/mybudgets/show.html.erb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<div> <div>
<%= render 'mybudgets/incomes' %> <%= render 'mybudgets/incomes' %>
<%= render 'mybudgets/expenses' %> <%= render 'mybudgets/expenses' %>
<%= render 'mybudgets/transfers' %>
</div> </div>
</center> </center>
</div> </div>
Expand Down
25 changes: 25 additions & 0 deletions app/views/transfer/_form.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="bordered-form">
<%= form_for [@mybudget, @transfer] do |f| %>

<table>
<tr>
<td class="field-name">
<%= f.label :transfer %><br>
</td>
<td class="field"><%= f.text_field :transfer %></td>
</tr>
<tr>
<td class="field-name"> <%= f.label :amount %><br></td>
<td class="field"><%= f.number_field :amount %></td>
</tr>
<tr>
<td class="field-name" id="total-field-name">
<%= f.label :total_transfer %><br></td>
<td class="total-output"><%= @transfer.total_transfer%></td>
</tr>
</table>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</div>
29 changes: 29 additions & 0 deletions app/views/transfer/_modal_form_new.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="modal-dialog reorder-modal-form">
<div class="modal-content">
<%= bootstrap_form_for([@mybudget, @transfer], remote: true) do |f| %>
<div class="modal-header">
<h4 class="modal-title">New Transfer</h4>
</div>

<div class="modal-body">
<table>
<tr>
<td class="field-name">
<%= f.label :transfer %><br>
</td>
<td class="field"><%= f.text_field :transfer %></td>
</tr>
<tr>
<td class="field-name"> <%= f.label :amount %><br></td>
<td class="field"><%= f.number_field :amount %></td>
</tr>
</table>
</div>

<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" data-behaviour="form-clear">Cancel</button>
<%= f.submit class: "btn btn-primary", data: { trigger: 'loading' } %>
</div>
<% end %>
</div>
</div>
6 changes: 6 additions & 0 deletions app/views/transfer/edit.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="row">
<div class="col-md-8">
<%= render 'transfers/new_transfers_form' %>
<div class="col-md-4">
</div>
</div>
2 changes: 2 additions & 0 deletions app/views/transfer/new.js.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
$("#new-transfer-modal-form").html("<%= j render('transfers/modal_form_new', transfer: @transfer) %>");
$('#new-transfer-modal-form').modal('show')
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
resources :mybudgets, except: [:index] do resources :mybudgets, except: [:index] do
resources :incomes, except: [:show, :index] resources :incomes, except: [:show, :index]
resources :expenses, except: [:show, :index] resources :expenses, except: [:show, :index]
resources :transfers, except: [:show, :index]
end end
# The priority is based upon order of creation: first created -> highest priority. # The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes". # See how all your routes lay out with "rake routes".
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20150920064544_create_transfers.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateTransfers < ActiveRecord::Migration
def change
create_table :transfers do |t|
t.belongs_to :mybudget, index: true
t.string :transfer, null: false, limit: 100
t.decimal :amount, null: false, default: 0
t.datetime :value_from
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.


ActiveRecord::Schema.define(version: 20150917073356) do ActiveRecord::Schema.define(version: 20150920064544) do


# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
Expand Down Expand Up @@ -40,4 +40,13 @@
t.datetime "value_from" t.datetime "value_from"
end end


create_table "transfers", force: :cascade do |t|
t.integer "mybudget_id"
t.string "transfer", limit: 100, null: false
t.decimal "amount", default: 0.0, null: false
t.datetime "value_from"
end

add_index "transfers", ["mybudget_id"], name: "index_transfers_on_mybudget_id", using: :btree

end end
7 changes: 7 additions & 0 deletions test/controllers/transfer_controller_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TransferControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
11 changes: 11 additions & 0 deletions test/fixtures/transfers.yml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/transfer_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TransferTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 1d4caf0

Please sign in to comment.