Jordan Elver
2008-Apr-24 09:27 UTC
Encapsulating multiple model logic and checking returns
Hi, I posted the other day, but I don''t think I was clear enough, this post tries to break down the essence of the problem :) What is the best way to check what happened within a method call whilst still encapsulating the logic within the object. For example, I have an object method which calls upon other objects to get results back. Depending on the result of that call, different actions need to take place. So, I have something like this: class Cart has_one :postage_method attr_accessor :postage_updated def add(product) # do adding product calculate_postage end def remove(product) # do removal of product calculate_postage end private def calculate_postage rate = postage_method.find_rate_for_weight(weight) if rate.some_condition # update db self.postage_updated = true else # update db end end end class CartController before_filter :get_cart_from_db # into @cart def add cart = @cart.add(product_from_params) if cart.postage_updated # tell user postage was automatically updated else # do something else end end end To me, the problem with this approach, is that you need to repeat these checks for every method where you are calling the method, which soon adds up and is not DRY. So: class CartController before_filter :get_cart_from_db # into @cart def add cart = @cart.add(product_from_params) if cart.postage_updated # tell user postage was automatically updated else # do something else end end def remove cart = @cart.remove(product_from_params) if cart.postage_updated # tell user postage was automatically updated else # do something else end end ..etc end Also, what happens if I need to check multiple different conditions? Am I going about this in entirely the wrong way? Many thanks for any advice. Cheers, Jordan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---