Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
Prepare for 2021.07.19 release (#57)
Browse files Browse the repository at this point in the history
* ✨ Add order option: discounted amount (#55)

* ✨ Order games by popularity (#56)
  • Loading branch information
stephannv authored Jul 19, 2021
1 parent b2119d8 commit 0591ff9
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 32 deletions.
35 changes: 12 additions & 23 deletions app/controllers/games_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,27 @@ class GamesController < ApplicationController
before_action :authenticate_user!, only: %i[wishlist]

def index
list_games(filter: filter_params, sort: sort_param || 'release_date_desc')
list_games(filter: filter_params)
end

def on_sale
list_games(
filter: filter_params.merge(on_sale: true),
sort: sort_param || 'discount_start_date_desc'
)
list_games(filter: filter_params.merge(on_sale: true))
end

def new_releases
list_games(
filter: filter_params.merge(new_release: true),
sort: sort_param || 'release_date_desc'
)
list_games(filter: filter_params.merge(new_release: true))
end

def coming_soon
list_games(
filter: filter_params.merge(coming_soon: true),
sort: sort_param || 'release_date_asc'
)
list_games(filter: filter_params.merge(coming_soon: true))
end

def pre_order
list_games(
filter: filter_params.merge(pre_order: true),
sort: sort_param || 'release_date_asc'
)
list_games(filter: filter_params.merge(pre_order: true))
end

def wishlist
list_games(
filter: filter_params.merge(wishlisted: true),
sort: sort_param.presence
)
list_games(filter: filter_params.merge(wishlisted: true))
end

def show
Expand All @@ -52,8 +37,12 @@ def show

private

def list_games(filter:, sort:)
result = Items::List.result(filter_params: filter, sort_param: sort, user: current_user)
def list_games(filter:)
result = Items::List.result(
filter_params: filter,
sort_param: sort_param || 'all_time_visits_desc',
user: current_user
)

@pagy, @games = pagy(result.items)
end
Expand Down
7 changes: 7 additions & 0 deletions app/data_adapters/nintendo_price_data_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def adapt
discount_started_at: discount_started_at,
discount_ends_at: discount_ends_at,
discount_percentage: discount_percentage,
discounted_amount: discounted_amount,
state: state,
gold_points: gold_points
}
Expand Down Expand Up @@ -56,6 +57,12 @@ def discount_percentage
((1 - (discount_amount.cents.to_f / regular_amount.cents)) * 100).round
end

def discounted_amount
return if discount_amount_data.nil?

[regular_amount - discount_amount, 0].max
end

# rubocop:disable Metrics/MethodLength
def state
case @data['sales_status']
Expand Down
15 changes: 14 additions & 1 deletion app/lib/items_sorter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

class ItemsSorter
OPTIONS = {
all_time_visits_desc: {
text: I18n.t('games.sort_options.all_time_visits_desc'),
query: 'items.all_time_visits DESC NULLS LAST'
},
last_week_visits_desc: {
text: I18n.t('games.sort_options.last_week_visits_desc'),
query: 'items.last_week_visits DESC NULLS LAST'
},
title_asc: {
text: I18n.t('games.sort_options.title_asc'),
query: 'items.title ASC'
Expand All @@ -24,6 +32,11 @@ class ItemsSorter
query: 'coalesce(prices.discount_amount_cents, prices.regular_amount_cents) DESC NULLS LAST',
left_joins: :price
},
discounted_amount_desc: {
text: I18n.t('games.sort_options.discounted_amount_desc'),
query: 'prices.discounted_amount_cents DESC NULLS LAST',
left_joins: :price
},
discount_percentage_desc: {
text: I18n.t('games.sort_options.discount_percentage_desc'),
query: 'prices.discount_percentage DESC NULLS LAST',
Expand Down Expand Up @@ -53,7 +66,7 @@ def self.apply(relation, params)
end

def apply
sort_key = OPTIONS.key?(param) ? param : :title_asc
sort_key = OPTIONS.key?(param) ? param : :all_time_visits_desc
option = OPTIONS[sort_key]

self.relation = relation
Expand Down
1 change: 1 addition & 0 deletions app/models/price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Price < ApplicationRecord

monetize :regular_amount_cents, numericality: { greater_than_or_equal_to: 0 }
monetize :discount_amount_cents, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }
monetize :discounted_amount_cents, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }

validates :item_id, presence: true
validates :nsuid, presence: true
Expand Down
5 changes: 4 additions & 1 deletion config/locales/pt-BR/global.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ pt-BR:

games:
sort_options:
all_time_visits_desc: Populares
last_week_visits_desc: Tendências
title_asc: Título (A-Z)
release_date_asc: Antigos
release_date_desc: Recentes
price_asc: Mais baratos
price_desc: Mais caros
discount_percentage_desc: Maiores descontos
discounted_amount_desc: Maiores descontos ($)
discount_percentage_desc: Maiores descontos (%)
discount_start_date_desc: Promoções recentes
discount_end_date_asc: Promoções encerrando
5 changes: 5 additions & 0 deletions db/migrate/20210630195127_add_discounted_amount_to_prices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDiscountedAmountToPrices < ActiveRecord::Migration[6.1]
def change
add_monetize :prices, :discounted_amount, amount: { null: true, default: nil }
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 24 additions & 6 deletions spec/data_adapters/nintendo_price_data_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
'price' => {
'regular_price' => {
'raw_value' => 50,
'currency' => 'MXN'
'currency' => 'BRL'
},
'discount_price' => {
'raw_value' => 20,
'currency' => 'MXN',
'currency' => 'BRL',
'start_datetime' => Time.zone.yesterday.to_s,
'end_datetime' => Time.zone.tomorrow.to_s
}
Expand All @@ -34,10 +34,10 @@

describe '#regular_amount' do
context 'when regular price is present' do
let(:data) { { 'price' => { 'regular_price' => { 'raw_value' => 50, 'currency' => 'MXN' } } } }
let(:data) { { 'price' => { 'regular_price' => { 'raw_value' => 50, 'currency' => 'BRL' } } } }

it 'returns regular price as money object' do
expect(adapted_data[:regular_amount]).to eq Money.new(5000, 'MXN')
expect(adapted_data[:regular_amount]).to eq Money.new(5000, 'BRL')
end
end

Expand All @@ -55,7 +55,7 @@
let(:data) { discount_data }

it 'returns discount price as money object' do
expect(adapted_data[:discount_amount]).to eq Money.new(2000, 'MXN')
expect(adapted_data[:discount_amount]).to eq Money.new(2000, 'BRL')
end
end

Expand Down Expand Up @@ -104,11 +104,29 @@
end
end

describe '#discounted_amount' do
context 'when discount price is present' do
let(:data) { discount_data }

it 'returns diff between regular price and discount price' do
expect(adapted_data[:discounted_amount]).to eq Money.new(3000, 'BRL')
end
end

context 'when discount price is nil' do
let(:data) { { 'price' => { 'discount_price' => nil } } }

it 'returns nil' do
expect(adapted_data[:discounted_amount]).to be_nil
end
end
end

describe '#discount_percentage' do
context 'when discount price is present' do
let(:data) { discount_data }

it 'returns discount end date as date object' do
it 'returns discount percentage' do
expect(adapted_data[:discount_percentage]).to eq 60
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/lib/items_sorter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
describe '::OPTIONS' do
subject(:options) { described_class::OPTIONS }

it 'has all time visits desc sort option' do
expect(options[:all_time_visits_desc]).to include(text: I18n.t('games.sort_options.all_time_visits_desc'))
end

it 'has last week visits desc sort option' do
expect(options[:last_week_visits_desc]).to include(text: I18n.t('games.sort_options.last_week_visits_desc'))
end

it 'has title asc sort option' do
expect(options[:title_asc]).to include(text: I18n.t('games.sort_options.title_asc'))
end
Expand All @@ -26,6 +34,10 @@
expect(options[:price_desc]).to include(text: I18n.t('games.sort_options.price_desc'))
end

it 'has discounted amount desc sort option' do
expect(options[:discounted_amount_desc]).to include(text: I18n.t('games.sort_options.discounted_amount_desc'))
end

it 'has discount percentage desc sort option' do
expect(options[:discount_percentage_desc]).to include(text: I18n.t('games.sort_options.discount_percentage_desc'))
end
Expand Down Expand Up @@ -97,6 +109,17 @@
end
end

context 'when sort options is discounted_amount_desc' do
let(:sort_option) { 'discounted_amount_desc' }
let!(:item_a) { create(:price, discounted_amount: 40).item }
let!(:item_c) { create(:price, discounted_amount: 100).item }
let!(:item_b) { create(:price, discounted_amount: 60).item }

it 'sorts items by discount percentage descending' do
expect(result.to_a).to eq [item_c, item_b, item_a]
end
end

context 'when sort options is discount_percentage_desc' do
let(:sort_option) { 'discount_percentage_desc' }
let!(:item_c) { create(:price, discount_percentage: 100).item }
Expand Down
1 change: 1 addition & 0 deletions spec/models/price_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
describe 'Configurations' do
it { is_expected.to monetize(:regular_amount) }
it { is_expected.to monetize(:discount_amount).allow_nil }
it { is_expected.to monetize(:discounted_amount).allow_nil }

it 'has state enum' do
expect(described_class.enumerations).to include(state: PriceStates)
Expand Down

0 comments on commit 0591ff9

Please sign in to comment.