From ccc2d71a7dbf187384a3521e3c8a3f8f2cf22285 Mon Sep 17 00:00:00 2001 From: Charley DAVID Date: Mon, 15 May 2017 14:52:16 -0300 Subject: [PATCH] Add best deals --- app/controllers/application_controller.rb | 20 +++++++++++++++++++- config/routes.rb | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 929b0da..ea7aa5a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,7 +7,7 @@ def prices currency = Price.pluck(:currency).include?(params[:currency]) ? params[:currency] : nil csv = CSV.generate(headers: true) do |rows| - rows << %w(title).concat(countries) + rows << %w(Title).concat(countries) games.each do |code, games| game = games.first prices = games.flat_map(&:prices) @@ -25,6 +25,24 @@ def prices send_data csv, type: 'text/csv; charset=utf-8; header=present' end + def best_deals + games = Game.all.includes(:prices).sort_by(&:title).group_by(&:parsed_game_code) + currencies = Price.pluck(:currency).uniq.sort + + csv = CSV.generate(headers: true) do |rows| + rows << ['Title', 'Country'].concat(currencies) + + games.each do |code, games| + game = games.first + lowest = games.flat_map(&:prices).min_by { |price| price.value.exchange_to('USD') } + rates = currencies.map { |c| lowest.value.exchange_to(c) } + rows << [game.title, lowest.country].concat(rates) + end + end + + send_data csv, type: 'text/csv; charset=utf-8; header=present' + end + def rates currencies = Price.pluck(:currency).uniq.sort diff --git a/config/routes.rb b/config/routes.rb index 32e8fc2..36249b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html get :prices, to: 'application#prices' + get :best_deals, to: 'application#best_deals' get :rates, to: 'application#rates' get :glossary, to: 'application#glossary' end