Skip to content

Instantly share code, notes, and snippets.

@jmbejar
Last active August 26, 2016 19:56
Show Gist options
  • Save jmbejar/64b0d7a9d333b7f0a31f44220cb5012f to your computer and use it in GitHub Desktop.
Save jmbejar/64b0d7a9d333b7f0a31f44220cb5012f to your computer and use it in GitHub Desktop.
Entries Listing & Form - Rails
  • Diff en AccountsController
  • EntriesController
  • Account#entries_with_balance (método del modelo)
  • Template del account/show (listado) y entries/new
  • Entries Helper (algunos métodos)

Cosas que no mostramos

  • Agregamos el resource nesteado en rutas
  • Template entries/edit (igual a entries/new)
  • Template de formulario de entries
class AccountsController < ApplicationController
# ...
# GET /accounts/1
+ def show
+ @entries_with_balance = @account.entries_with_balance
+ end
# ...
end
class EntriesController < ApplicationController
before_action :set_account
before_action :set_entry, only: [:edit, :update, :destroy]
before_action :set_entries_with_balance, only: [:new, :edit]
# GET /entries/new
def new
@entry = EntryService.new_entry(@account)
end
# GET /entries/1/edit
def edit
end
# POST /entries
def create
@entry = EntryService.create(@account, entry_params, counterpart_account_id)
respond_to do |format|
if @entry.valid?
format.js
else
format.js { render :error }
end
end
end
# PATCH/PUT /entries/1
def update
respond_to do |format|
if EntryService.update(@entry, entry_params, counterpart_account_id)
format.js
else
format.js { render :error }
end
end
end
# DELETE /entries/1
def destroy
EntryService.destroy(@entry)
end
private
def set_account
@account = current_user.accounts.find(params[:account_id])
end
def counterpart_account_id
if account_id = params[:counterpart_account_id]
current_user.accounts.find(account_id).id
end
end
def set_entry
@entry = @account.entries.find(params[:id])
end
def set_entries_with_balance
@entries_with_balance = @account.entries_with_balance
end
def entry_params
params
.require(:entry)
.permit(entry_transaction_attributes:
[:id, :category_id , :amount, :amount_currency, :kind, :date, :notes])
end
end
class Account < ApplicationRecord
+ def entries_with_balance
+ balance = starting_balance
+ _entries = entries.includes(entry_transaction: [:entry_on_origin, :category])
+ _entries.by_date.map do |entry|
+ balance += entry.value
+ [entry, balance]
+ end.reverse!
+ end
end
<div class="section-container--header row">
<div class="col-md-10 col-xs-8">
<h2><%= @account.name.titleize %></h2>
</div>
<div class="col-md-2 col-xs-4 col--no-gutter">
<%= link_to 'New Transaction', new_account_entry_path(@account),
id: 'newEntryButton',
class: 'btn btn-primary'
%>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div id="entriesListing" data-account-id="<%= @account.id %>" class="listing">
<% if @entries_with_balance.any? %>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Date</th>
<th>Category</th>
<th>Amount</th>
<th>Balance</th>
<th class="col-md-2 col-xs-4"></th>
</tr>
</thead>
<tbody>
<% @entries_with_balance.each do |entry, balance| %>
<%= render partial: 'entries/entry', locals: { entry: entry, balance: balance } %>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="alert alert-info" role="alert">
<strong>Heads up!</strong> Click on the New Transaction button to add your first transaction
</div>
<% end %>
</div>
</div>
</div>
<%= render template: 'accounts/show' %>
<%= render 'modal' %>
module EntriesHelper
def categories_options
[
['Favorites', categories_options_group(Category.favorited(current_user)) ],
['Other categories', categories_options_group(Category.no_favorited(current_user))]
]
end
def categories_options_group(categories)
categories.map {|c| [c.name, c.id, { data: { kind: c.kind } }]}
end
def accounts_options(without_account)
accounts_options_group(current_user.accounts.where.not(id: without_account))
end
def accounts_options_group(accounts)
options_for_select(accounts.map {|a| [a.name, a.id]})
end
def counterpart_account_label(entry)
if entry.transfer?
"#{entry.origin? ? 'Destination' : 'Origin'} account"
else
"Destination account"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment