I have a mode to put my code of controller in lib/
to reduce the code of the controller, I don't know if this works with my instance variables. I need some pass the action of controller to a lib/
, to make a sample call require
or include
in action, some to organize my code more.
The action is:
def calculate_ship
pacote = Correios::Frete::Pacote.new
@products = buy_cart.line_items
@products.each do |p|
p.product.length = 16 if !p.product.length
p.product.weight = 0.3 if !p.product.weight
p.product.width = 11 if !p.product.width
p.product.height = 6 if !p.product.height
@item = Correios::Frete::PacoteItem.new :peso => p.product.weight, :comprimento => p.product.length, :largura => p.product.width, :altura => p.product.height
while p.quantity > 0
pacote.add_item(@item)
p.quantity -= 1
end
end
frete = Correios::Frete::Calculador.new :cep_origem => "95520-000",
:cep_destino => params[:cep],
:encomenda => pacote
servicos = frete.calcular :sedex, :pac
@pac = servicos[:pac].valor
@sedex = servicos[:sedex].valor
flash[:error] = servicos[:sedex].msg_erro
end
How to move this into lib/
? I'm not accustomed to programing seriously with lib/
, etc.