Skip to content

Instantly share code, notes, and snippets.

@mwlang
Last active August 29, 2015 14:27
Show Gist options
  • Save mwlang/64b49551e3d27800e479 to your computer and use it in GitHub Desktop.
Save mwlang/64b49551e3d27800e479 to your computer and use it in GitHub Desktop.
How would you refactor this? (Rails 1.2, Ruby 1.8.7)
# == Schema Information
# Table name: line_items
# id :integer not null, primary key
# item_id :integer not null
# order_id :integer not null
# quantity :integer not null
# discount_percentage :decimal(18, 2 default(0.0)
# ... many fields redacted
#
class LineItem < ActiveRecord::Base
belongs_to :item
belongs_to :order
def customer_discount_percentage_quantity business_account_id, group_account_id, voucher_id
if self.item.customer_discount_group_id
query = "EXEC item_sp_get_customer_discount " +
" @quantity = #{self.quantity}" +
", @business_account_id = #{business_account_id.blank?? 0:business_account_id} " +
", @group_account_id = #{group_account_id.blank?? 0: group_account_id}" +
", @customer_discount_group_id = #{self.item.customer_discount_group_id}"
result_set = LineItem.find_by_sql(query)
voucher_discount_percentage = 0.0
unless voucher_id.blank?
voucher_detail = DiscountVoucher.find_by_id(voucher_id)
unless voucher_detail.blank?
unless voucher_detail.voucher_discount_percentage.blank?
voucher_discount_percentage = voucher_detail.voucher_discount_percentage
end
end
end
if result_set[0]!=nil
if voucher_discount_percentage > 0
self.discount_percentage = voucher_discount_percentage
return ( voucher_discount_percentage)
else
self.discount_percentage = result_set[0].discount_percentage
return (result_set[0].discount_percentage )
end
else
voucher_discount_percentage
end
end
0.0
end
end
# == Schema Information
# Table name: line_items
# id :integer not null, primary key
# item_id :integer not null
# order_id :integer not null
# quantity :integer not null
# discount_percentage :decimal(18, 2 default(0.0)
# ... many fields redacted
#
class LineItem < ActiveRecord::Base
belongs_to :item
belongs_to :order
def query_discount_percentage quantity, business_account_id, group_account_id, customer_discount_group_id
query = "EXEC item_sp_get_customer_discount" +
" @quantity = #{[quantity.to_i, 1].max}" +
", @business_account_id = #{business_account_id.to_i}" +
", @group_account_id = #{group_account_id.to_i}" +
", @customer_discount_group_id = #{customer_discount_group_id.to_i}"
result = LineItem.find_by_sql(query).first
result ? result.discount_percentage : 0.0
end
def voucher_discount_percentage voucher_id
if (voucher = DiscountVoucher.find_by_id(voucher_id)) && !voucher.voucher_discount_percentage.blank?
voucher.voucher_discount_percentage
end
end
def customer_discount_percentage_quantity business_account_id, group_account_id, voucher_id
return 0.0 unless self.item.customer_discount_group_id
if discount_percent = voucher_discount_percentage(voucher_id)
return self.discount_percentage = discount_percent
end
discount_percent = query_discount_percentage quantity,
business_account_id, group_account_id, customer_discount_group_id
self.discount_percentage = discount_percent
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment