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

refactor: Refactor item types and create some concerns for item #197

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/enumerations/item_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ItemTypes < EnumerateIt::Base
associate_values :game, :game_bundle, :dlc, :dlc_bundle
end
8 changes: 4 additions & 4 deletions app/lib/nintendo/item_data_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class ItemDataAdapter
].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 = {
nil => :game,
"Individual" => :dlc,
"Bundle" => :dlc_bundle,
"ROM Bundle" => :game_bundle
nil => ItemTypes::GAME,
"Individual" => ItemTypes::DLC,
"Bundle" => ItemTypes::DLC_BUNDLE,
"ROM Bundle" => ItemTypes::GAME_BUNDLE
}.freeze

def initialize(data)
Expand Down
31 changes: 31 additions & 0 deletions app/models/concerns/items/relations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "active_support/concern"

module Items
module Relations
extend ActiveSupport::Concern

included do
has_one :raw_item, dependent: :destroy
has_one :price, dependent: :destroy

has_many :wishlist_items, dependent: :destroy
has_many :parent_relationships,
class_name: "ItemRelationship",
foreign_key: :parent_id,
dependent: :destroy,
inverse_of: :parent
has_many :child_relationships,
class_name: "ItemRelationship",
foreign_key: :child_id,
dependent: :destroy,
inverse_of: :child
has_many :events, class_name: "ItemEvent", dependent: :destroy

has_many :children, through: :parent_relationships, class_name: "Item", source: :child
has_many :parents, through: :child_relationships, class_name: "Item", source: :parent
has_many :price_history_items, through: :price, source: :history_items
end
end
end
2 changes: 1 addition & 1 deletion app/models/concerns/items/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Scopes
extend ActiveSupport::Concern

included do
scope :only_games, -> { where(item_type: %i[game game_bundle]) }
scope :only_games, -> { where(item_type: [ItemTypes::GAME, ItemTypes::GAME_BUNDLE]) }
scope :with_nsuid, -> { where.not(nsuid: nil) }
scope :on_sale, -> { where(on_sale: true) }
scope :new_release, -> { where(new_release: true) }
Expand Down
23 changes: 23 additions & 0 deletions app/models/concerns/items/validations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "active_support/concern"

module Items
module Validations
extend ActiveSupport::Concern

included do
validates :item_type, presence: true
validates :title, presence: true
validates :external_id, presence: true

validates :external_id, uniqueness: true

validates :title, length: { maximum: 1024 }
validates :description, length: { maximum: 8192 }
validates :nsuid, length: { maximum: 32 }
validates :external_id, length: { maximum: 256 }
validates :release_date_display, length: { maximum: 64 }
end
end
end
36 changes: 3 additions & 33 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,20 @@
class Item < ApplicationRecord
include FriendlyId
include PgSearch::Model
include Items::Relations
include Items::Scopes
include Items::Validations

attribute :wishlisted, :boolean, default: false # this attr will be filled using WithWishlistedColumnQuery

has_one :raw_item, dependent: :destroy
has_one :price, dependent: :destroy

has_many :wishlist_items, dependent: :destroy
has_many :parent_relationships,
class_name: "ItemRelationship",
foreign_key: :parent_id,
dependent: :destroy,
inverse_of: :parent
has_many :child_relationships,
class_name: "ItemRelationship",
foreign_key: :child_id,
dependent: :destroy,
inverse_of: :child
has_many :events, class_name: "ItemEvent", dependent: :destroy

has_many :children, through: :parent_relationships, class_name: "Item", source: :child
has_many :parents, through: :child_relationships, class_name: "Item", source: :parent
has_many :price_history_items, through: :price, source: :history_items

enum :item_type, game: 0, game_bundle: 1, dlc: 2, dlc_bundle: 3
has_enumeration_for :item_type, with: ItemTypes, create_helpers: true, required: true, create_scopes: true

friendly_id :title, use: :history

monetize :current_price_cents, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }

pg_search_scope :search_by_title, against: :title, using: { tsearch: { dictionary: "english" } }, ignoring: :accents

validates :item_type, presence: true
validates :title, presence: true
validates :external_id, presence: true

validates :external_id, uniqueness: true

validates :title, length: { maximum: 1024 }
validates :description, length: { maximum: 8192 }
validates :nsuid, length: { maximum: 32 }
validates :external_id, length: { maximum: 256 }
validates :release_date_display, length: { maximum: 64 }

def to_param
slug
end
Expand Down
6 changes: 6 additions & 0 deletions config/locales/pt-BR/enumerations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pt-BR:
permanent_price_change: Reajuste de preço
price_state_change: Remoção da eShop

item_types:
game: Jogo
game_bundle: 'Bundle'
dlc: 'DLC'
dlc_bundle: 'Bundle de DLC'

price_states:
pre_order: Pré-venda
on_sale: Disponível
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20221029000002_change_item_type_column_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeItemTypeColumnType < ActiveRecord::Migration[7.0]
def change
change_column(:items, :item_type, :string)
end
end
4 changes: 2 additions & 2 deletions db/schema.rb

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

4 changes: 3 additions & 1 deletion spec/factories/items_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

FactoryBot.define do
factory :item do
item_type { Item.item_types.keys.sample }
traits_for_enum :item_type, ItemTypes.list

item_type { ItemTypes.list.sample }
external_id { Faker::Internet.unique.uuid }
title { Faker::Game.title }
slug { title.parameterize }
Expand Down
16 changes: 8 additions & 8 deletions spec/lib/nintendo/item_data_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
RSpec.describe Nintendo::ItemDataAdapter, type: :lib do
describe "#item_type" do
context "when dlcType is nil" do
it "returns :game" do
it "returns ItemTypes::GAME" do
adapted_data = described_class.adapt({ "dlcType" => nil })

expect(adapted_data[:item_type]).to eq :game
expect(adapted_data[:item_type]).to eq ItemTypes::GAME
end
end

context "when dlcType is Individual" do
it "returns :dlc" do
it "returns ItemTypes::DLC" do
adapted_data = described_class.adapt({ "dlcType" => "Individual" })

expect(adapted_data[:item_type]).to eq :dlc
expect(adapted_data[:item_type]).to eq ItemTypes::DLC
end
end

context "when dlcType is Bundle" do
it "returns :dlc_bundler" do
it "returns ItemTypes::DLC_BUNDLE" do
adapted_data = described_class.adapt({ "dlcType" => "Bundle" })

expect(adapted_data[:item_type]).to eq :dlc_bundle
expect(adapted_data[:item_type]).to eq ItemTypes::DLC_BUNDLE
end
end

context "when dlcType is ROM Bundle" do
it "returns :game_bundle" do
it "returns ItemTypes::GAME_BUNDLE" do
adapted_data = described_class.adapt({ "dlcType" => "ROM Bundle" })

expect(adapted_data[:item_type]).to eq :game_bundle
expect(adapted_data[:item_type]).to eq ItemTypes::GAME_BUNDLE
end
end

Expand Down