Skip to content

Instantly share code, notes, and snippets.

@garciadanny
Last active December 14, 2015 03:39
Show Gist options
  • Save garciadanny/5022754 to your computer and use it in GitHub Desktop.
Save garciadanny/5022754 to your computer and use it in GitHub Desktop.
Using the send method. (Refactoring Sales_Engine)
def self.most_revenue(number_of_results)
items = get_items_with_appropriate_values(:unit_price)
sort_and_return_items(items, number_of_results)
end
def self.most_items(number_of_results)
items = get_items_with_appropriate_values(:quantity)
sort_and_return_items(items, number_of_results)
end
def self.get_items_with_appropriate_values(value)
items = Hash.new(0)
Invoices.find_all_paid_invoices.each do |invoice|
invoice.invoice_items.each do |inv_item|
item = Item.find_by_id(inv_item.item_id)
items[item] += inv_item.send(value)
end
end
end
def self.sort_and_return_items(items, number_of_results)
items.sort_by {|k, v| -v }.map {|hash| hash[0] }.take(number_of_results)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment