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 :slight_smile:

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