Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Just an example how to refactor and separate code and their concerns.
class OrderService
class << self
def find_or_create_purchase_for_order(order, credit_card_id, shipping_fee = 0.0)
user, vendor = order.buyer, order.seller
purchase = Purchase.find_by_user_id_and_order_id(user.id, order.id)
return purchase if purchase.present?
create_purchase order, credit_card_id, shipping_fee
end
def create_purchase(order, credit_card_id, shipping_fee)
price, total = calculate_price order
order.buyer.purchases.create(
seller_id: vendor.id,
order_id: order.id,
shipping_address_id: order.cart.shipping_address.id,
purchase_price: price,
sales_tax: sales_tax,
shipping_fee: shipping_fee,
credit_card_id: credit_card_id,
total_cost: total,
for_pickup: true
)
end
def calculate_price(order)
vendor, sales_tax, price = order.seller, find_sales_tax(order), order.price
total = (price + price * sales_tax) - vendor.reppio_cut
[price, total]
end
def find_sales_tax(order)
user, vendor = order.buyer, order.seller
sales_tax = SalesTax.find_by_user_id(vendor.id)
sales_tax.send(
if (addr = order.cart.shipping_address).present?
addr.state.upcase.to_sym
else
user.state.upcase.to_sym
end
).to_f rescue 0.0
end
end
end
class OrderService
def self.find_or_create_purchase_for_order(order, credit_card_id, shipping_fee = 0.0)
user = order.buyer
purchase = Purchase.find_by_user_id_and_order_id(user.id, order.id)
vendor = order.seller
unless purchase
if order.cart.shipping_address
shipping_address = order.cart.shipping_address
sales_tax_row = SalesTax.find_by_user_id(vendor.id)
sales_tax = eval("sales_tax_row." + shipping_address.state.upcase).to_f rescue 0.0
end
sales_tax_row = SalesTax.find_by_user_id(vendor.id)
begin
sales_tax = eval("sales_tax_row." + user.state.upcase).to_f rescue 0.0000
rescue
sales_tax = 0.0000
end
price = order.price
total = (price+ price*sales_tax) - vendor.reppio_cut
purchase = user.purchases.build(
:seller_id => vendor.id,
:order_id => order.id,
:shipping_address_id => order.cart.shipping_address.id,
:purchase_price => price,
:sales_tax => sales_tax,
:shipping_fee => shipping_fee,
:credit_card_id => credit_card_id,
:total_cost => total,
:for_pickup => true
)
purchase.save
end
purchase
end
end
@beeplove

This comment has been minimized.

Copy link

commented Aug 28, 2013

If you are specially after shorter code!

def find_or_create_purchase_for_order order, credit_card_id, shipping_fee = 0.0
    user, vendor = order.buyer, order.seller
    purchase     = Purchase.find_by_user_id_and_order_id(user.id, order.id)

    purchase.present? && purchase || create_purchase(order, credit_card_id, shipping_fee)
end

def find_sales_tax order
    user, vendor, addr = order.buyer, order.seller, order.cart.shipping_address
    sales_tax    = SalesTax.find_by_user_id(vendor.id)

    sales_tax.send(
        (addr.present? && addr.state.upcase || user.state.upcase).to_sym
    ).to_f rescue 0.0
end
@rubyrider

This comment has been minimized.

Copy link

commented Aug 28, 2013

great! Seems no way for me to make a refactor!

@we4tech

This comment has been minimized.

Copy link
Owner Author

commented Aug 28, 2013

hi @beeplove bhai your version looks nifty and better. specially i love those && || over ternary operator

@smsohan

This comment has been minimized.

Copy link

commented Aug 28, 2013

Even shorter, and readable.

def find_or_create_purchase_for_order(order, credit_card_id, shipping_fee = 0.0)
   Purchase.exists?(user_id: order.buyer_id, order_id: order.id) || create_purchase(order, credit_card_id, shipping_fee)
end
@smsohan

This comment has been minimized.

Copy link

commented Aug 28, 2013

Here goes the other method, shortened and sweetened:

def find_sales_tax(order)
    sales_tax    = SalesTax.find_by_user_id(vendor.id)
    state = order.cart.shipping_address.try(:state) || order.buyer.state
    state ? sales_tax.send(state.to_sym).to_f : 0.0
end
@we4tech

This comment has been minimized.

Copy link
Owner Author

commented Aug 29, 2013

hi @smsohan bhai, I LOVE this version! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.