2012-02-25 42 views
0

對於以下RubyOnRails代碼,是否有移動的「利潤」計算出來的查看,並納入模型的方式..所以也許有一個名爲total_incometotal_expense屬性?如何在模型中添加額外屬性以將計算移出視圖?

型號 - transaction.rb

class Transaction < ActiveRecord::Base 
    attr_accessible :name, :amount, :category 
    scope :incomes, :conditions => { :category => 'Income' } 
    scope :expenses, :conditions => { :category => 'Expense' } 
end 

控制器 - transactions_controller.rb

class TransactionsController < ApplicationController 

    def index 
    @incomes = Transaction.incomes 
    @expenses = Transaction.expenses 
    @transaction = Transaction.new 
    end 

視圖 - index.html.erb

<pre> 
<strong>Income</strong> 
    <% @incomes.each do |income| %> 
    <%= income.name %> - <%= number_to_currency((income.amount.nil? ? 0 : income.amount)) %> 
    <% end %> 
    <strong>Subtotal:</strong> <%= number_to_currency(@income_total = @incomes.sum(:amount)) %> 

<strong>Expenses</strong> 
    <% @expenses.each do |expense| %> 
    <%= expense.name %> - <%= number_to_currency((expense.amount.nil? ? 0 : expense.amount)) %> 
    <% end %> 
    <strong>Subtotal:</strong> <%= number_to_currency(@expenses_total = @expenses.sum(:amount)) %> 

<strong>Profit: <%= number_to_currency(@income_total - @expenses_total) %></strong> 
</pre> 
+1

與您的問題無關:小心,交易是許多編程環境中的保留字,可能會導致問題。爲了避免問題,我總是調整一下名稱的習慣。 – DGM 2012-02-25 01:35:59

+1

@DGM感謝您指出了這一點。一些RoR保留字(我發現它包括'transaction')列在[RoR舊wiki](http://oldwiki.rubyonrails.org/rails/pages/ReservedWords)上。 – Turgs 2012-02-25 06:14:30

回答

2

對於只需要添加類的方法來交易最基本的變化

class Transaction < ActiveRecord::Base 
    attr_accessible :name, :amount, :category 
    scope :incomes, :conditions => { :category => 'Income' } 
    scope :expenses, :conditions => { :category => 'Expense' } 

    def self.income_total 
    incomes.sum :amount 
    end 

    def self.expenses_total 
    expenses.sum :amount 
    end 

end 

class TransactionsController < ApplicationController 

    def index 
    @incomes = Transaction.incomes 
    @expenses = Transaction.expenses 
    @income_total = Transaction.income_total 
    @expenses_total = Transaction.expenses_total 
    @transaction = Transaction.new 
    end 
+0

只是我正在尋找的東西的類型。謝謝。 – Turgs 2012-02-25 06:11:19

1

這些天,我寧願把它們包裝多,相互依存的實例變量成一個新的主持人班,像這樣(未經測試)

class TransactionPresenter 

    attr_reader :income_total, :expenses_total 

    def initialize 
    @incomes = Transaction.incomes 
    @expenses = Transaction.expenses 
    end 

    def each_income(&block) 
    @incomes.each(&block) 
    end 

    def each_expense(&block) 
    @incomes.each(&block) 
    end 

    def income_total 
    @income_total ||= number_to_currency(@incomes.sum(&:amount)) 
    end 

    def expenses_total 
    @expenses_total ||= number_to_currency(@expenses.sum(&:amount)) 
    end 

    def name_and_amount(income_or_expense) 
    "#{income_or_expense.name} - #{number_to_currency((income.amount.nil? ? 0 : income.amount))}" 
    end 

    def profit 
    number_to_currency(income_total - expenses_total) 
    end 

end 

# controller 

def index 
    @presenter = TransactionPresenter.new 
end 

# view 

<pre> 
<strong>Income</strong> 
    <% @presenter.each_income do |income| %> 
    <%= @presenter.name_and_amount %> 
    <% end %> 
    <strong>Subtotal:</strong> <%= @presenter.income_total %> 

<strong>Expenses</strong> 
    <% @presenter.each_expense do |expense| %> 
    <%= @presenter.name_and_amount %> 
    <% end %> 
    <strong>Subtotal:</strong> <%= @presenter.expenses_total %> 

<strong>Profit: <%= @presenter.profit %></strong> 
</pre> 
+0

我將不得不閱讀使用演示者類。我從來沒有見過這種類型的東西。 – Turgs 2012-02-25 06:22:00

相關問題