Rails - moving out calculations from my views?

后端 未结 2 1063
悲哀的现实
悲哀的现实 2021-02-20 15:59

Currently I\'m performing some calculations in my views, which is a bad thing, of course:

<% categories.each do |c| %>
  ....
    <%= c.transactions.sum         


        
2条回答
  •  爱一瞬间的悲伤
    2021-02-20 16:48

    The main code smell you're seeing is called the law of Demeter (which, like many programming "laws", you should think of it more like a "guideline of Demeter").

    What you could do is move the actual calculation step into a method on the category, e.g.

    class Category < ActiveRecord::Base
      def transaction_amount
        transactions.sum("amount_cents")
      end
    end
    
    <% categories.each do |c| %>
      ....
        <%= c.transaction_amount %>
      ....
    <% end %>
    

    Technically speaking the calculation is still performed while rendering the view, but the logic for how that summed amount gets calculated is no longer inside the view itself. All the view now cares about is that it can send the message transaction_amount to the category objects. This also leaves room for you to add a cache for the sums, or for you to stop passing actual records, but instead pass static objects (that aren't ActiveRecord models) that come out of some piece of code that performs the sum in a more efficient manner.

提交回复
热议问题