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

Commit

Permalink
✨ Allow search for items with demo (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv authored Jul 17, 2022
1 parent d60960e commit d0c9c81
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/clients/nintendo/item_data_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ItemDataAdapter

ATTRIBUTES = %i[
item_type title description release_date release_date_display website_url banner_url external_id nsuid genres
developer publisher franchises demo_nsuid num_of_players
developer publisher franchises with_demo num_of_players
].freeze
IMAGE_BASE_URL = "https://assets.nintendo.com/image/upload/ar_16:9,b_auto:border,c_lpad/b_white/f_auto/q_auto/dpr_auto/c_scale,w_720/v1"
ITEM_TYPES = {
Expand Down Expand Up @@ -85,8 +85,8 @@ def franchises
data["franchises"].to_a.compact
end

def demo_nsuid
data["demoNsuid"]
def with_demo
data["demoNsuid"].present?
end

def num_of_players
Expand Down
14 changes: 13 additions & 1 deletion app/components/games/filter_form_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
</div>
</div>

<div class="grid grid-cols-4 md:grid-cols-4 gap-2">
<div class="grid grid-cols-3 md:grid-cols-4 gap-2">
<div class="form-control">
<%= form.label :on_sale, class: "label cursor-pointer flex flex-col gap-2 justify-center" do %>
<%= form.check_box :on_sale,
Expand Down Expand Up @@ -140,6 +140,18 @@
</span>
<% end %>
</div>

<div class="form-control">
<%= form.label :with_demo, class: "label cursor-pointer flex flex-col gap-2 justify-center" do %>
<%= form.check_box :with_demo,
checked: filters_form_object.with_demo,
class: "toggle toggle-primary"
%>
<span class="label-text font-bold text-center">
<%= t(".with_demo") %>
</span>
<% end %>
</div>
</div>

<div class="modal-action flex gap-2 flex-row">
Expand Down
11 changes: 11 additions & 0 deletions app/components/item_details/specs_section_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@
</dl>
<% end %>

<dl>
<dt class="font-bold flex flex-row gap-2">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 stroke-accent" viewBox="0 0 24 24" stroke-width="2"> <path stroke-linecap="round" stroke-linejoin="round" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" /> </svg>
<%= t(".with_demo") %>
</dt>

<dd class="text-slate-300">
<%= t(item.with_demo?, scope: :yes_or_no) %>
</dd>
</dl>

<% if item.website_url.present? %>
<p>
<%= link_to item.website_url, class: "link flex gap-2", target: "_blank", rel: "noopener noreferrer" do %>
Expand Down
1 change: 1 addition & 0 deletions app/form_objects/game_filters_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class GameFiltersForm
attribute :new_release, :boolean
attribute :coming_soon, :boolean
attribute :pre_order, :boolean
attribute :with_demo, :boolean
attribute :wishlisted, :boolean

def self.build(attributes = {})
Expand Down
3 changes: 2 additions & 1 deletion app/lib/items_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ItemsFilter
filter_new_release
filter_coming_soon
filter_pre_order
filter_with_demo
].freeze

def initialize(filters_form:, relation: Item)
Expand Down Expand Up @@ -54,7 +55,7 @@ def filter_language
self.relation = relation.where("languages @> ARRAY[?]::varchar[]", filters_form.language)
end

%i[on_sale new_release coming_soon pre_order].each do |scope|
%i[on_sale new_release coming_soon pre_order with_demo].each do |scope|
define_method :"filter_#{scope}" do
self.relation = relation.send(scope) if filters_form.public_send(scope)
end
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/items/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Scopes
scope :new_release, -> { where(new_release: true) }
scope :coming_soon, -> { where(coming_soon: true) }
scope :pre_order, -> { where(pre_order: true) }
scope :with_demo, -> { where(with_demo: true) }
scope :pending_scrap, -> { where(last_scraped_at: (..24.hours.ago)).or(where(last_scraped_at: nil)) }
scope :with_prices, -> { joins(:price).includes(:price) }
scope :including_prices, -> { left_joins(:price).includes(:price) }
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR/components/games.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pt-BR:
pre_order: Pré-venda
coming_soon: Em breve
new_release: Lançamento
with_demo: Demo disponível
all: Tudo
close: Fechar
clear: Limpar
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR/components/item_details.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ pt-BR:
languages: Idiomas
num_of_players: Qtde. de Jogadores
rom_size: Espaço em disco
with_demo: Versão demo disponível?
official_website: Site Oficial
5 changes: 5 additions & 0 deletions config/locales/pt-BR/global.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
pt-BR:
yes_or_no:
"true": Sim
"false": Não

time:
formats:
shorter: "%d/%m/%Y %H:%M"


time_distance_from_now: Há %{distance}

website_description: Descubra o próximo jogo do seu Nintendo Switch.
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20220717164537_add_with_demo_to_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddWithDemoToItems < ActiveRecord::Migration[7.0]
def change
add_column :items, :with_demo, :boolean, null: false, default: false
remove_column :items, :demo_nsuid
add_index :items, :with_demo, where: :with_demo
end
end
5 changes: 3 additions & 2 deletions db/schema.rb

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

18 changes: 14 additions & 4 deletions spec/clients/nintendo/item_data_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,21 @@
end
end

describe "#demo_nsuid" do
it "returns demoNsuid" do
adapted_data = described_class.adapt({ "demoNsuid" => "some nsuid" })
describe "#with_demo" do
context "when demo_nsuid is present" do
it "returns true" do
adapted_data = described_class.adapt({ "demoNsuid" => "some nsuid" })

expect(adapted_data[:demo_nsuid]).to eq "some nsuid"
expect(adapted_data[:with_demo]).to be true
end
end

context "when demo_nsuid is blank" do
it "returns false" do
adapted_data = described_class.adapt({ "demoNsuid" => nil })

expect(adapted_data[:with_demo]).to be false
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/factories/items_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
developer { Faker::Lorem.word }
genres { Faker::Lorem.words(number: 3) }
franchises { Faker::Lorem.words(number: 2) }
demo_nsuid { Faker::Lorem.word }
num_of_players { Faker::Lorem.word }
on_sale { Faker::Boolean.boolean }
new_release { Faker::Boolean.boolean }
coming_soon { Faker::Boolean.boolean }
pre_order { Faker::Boolean.boolean }
with_demo { Faker::Boolean.boolean }
all_time_visits { Faker::Number.number(digits: 3) }
last_week_visits { Faker::Number.number(digits: 2) }
languages { I18nData.languages.keys.sample(3) }
Expand Down
1 change: 1 addition & 0 deletions spec/form_objects/game_filters_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
new_release: an_instance_of(ActiveModel::Type::Boolean),
coming_soon: an_instance_of(ActiveModel::Type::Boolean),
pre_order: an_instance_of(ActiveModel::Type::Boolean),
with_demo: an_instance_of(ActiveModel::Type::Boolean),
wishlisted: an_instance_of(ActiveModel::Type::Boolean)
}.stringify_keys
end
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/items_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,17 @@
expect(result.to_a).to eq [item]
end
end

context "when with_demo param is true" do
it "returns with_demo items" do
filters_form = GameFiltersForm.build(with_demo: true)
item_with_demo = create(:item, with_demo: true)
create_list(:item, 3, with_demo: false) # items without demo

result = described_class.apply(relation: Item, filters_form: filters_form)

expect(result.to_a).to eq [item_with_demo]
end
end
end
end
9 changes: 9 additions & 0 deletions spec/models/concerns/items/item_scopes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
end
end

describe ".with_demo" do
it "returns items with demo" do
item_with_demo = create(:item, with_demo: true)
create(:item, with_demo: false) # item without demo

expect(described_class.with_demo.to_a).to eq [item_with_demo]
end
end

describe ".pending_scrap" do
let!(:not_scraped) { create(:item, last_scraped_at: nil) }
let!(:scraped_long_ago) { create(:item, last_scraped_at: 25.hours.ago) }
Expand Down

0 comments on commit d0c9c81

Please sign in to comment.