Permalink
Browse files

implement transactions filter by category

  • Loading branch information...
1 parent b696ce2 commit 687454c574c366ac85a3394b3f21eccf288b7297 @ck3g committed Mar 24, 2013
@@ -5,8 +5,10 @@ class TransactionsController < ApplicationController
before_filter :find_accounts, only: [:new, :create]
after_filter :expire_statistics_cache, only: [:create, :destroy]
+ has_scope :category
+
def index
- @transactions = current_user.transactions.includes(:category, :account).order('created_at desc').page(params[:page])
+ @transactions = apply_scopes(current_user.transactions.includes(:category, :account)).order('created_at desc').page(params[:page])
end
def new
@@ -1,2 +1,9 @@
module TransactionsHelper
+ def category_name(transaction)
+ if transaction.category
+ transaction.category_name
+ else
+ t('parts.transactions.no_category')
+ end
+ end
end
@@ -16,6 +16,8 @@ class Transaction < ActiveRecord::Base
after_create :affect_on_account_after_create
before_destroy :affect_on_account_before_destroy
+ scope :category, ->(category_id) { where(category_id: category_id) }
+
def income?
category_type_id == CategoryType.income
end
@@ -15,7 +15,7 @@
- @categories.each do |category|
%tr{class: category_type(category)}
%td.span3= t("parts.categories.#{category.category_type_name}")
- %td= category.name
+ %td= link_to category.name, transactions_path(category: category.id)
%td.span2
= link_to edit_category_path(category), title: t('common.edit'), class: "btn btn-mini" do
%i.icon-pencil
@@ -20,7 +20,7 @@
%td.span2
= I18n.l(transaction.created_at.to_date, format: :long)
%td
- .category= transaction.category ? transaction.category_name : t('parts.transactions.no_category')
+ .category= link_to category_name(transaction), transactions_path(category: transaction.category_id)
.comment= transaction.comment
%td.span2.number= get_number_to_currency(transaction.income? ? transaction.summ : -transaction.summ)
%td.span2= transaction.account_name
@@ -139,4 +139,16 @@
build(:transaction, summ: 0).should_not be_valid
end
+ describe ".scopes" do
+ describe ".category" do
+ before do
+ spending_transaction
+ income_transaction
+ end
+ it "returns transactions for same category only" do
+ expect(Transaction.category(category.id)).to eq [spending_transaction]
+ end
+ end
+ end
+
end

0 comments on commit 687454c

Please sign in to comment.