Permalink
Browse files

add item variants table and populate on user item create

  • Loading branch information...
birarda committed Sep 22, 2016
1 parent 2342aa7 commit 6b3a4c3fc52c8b7f1454fc3b1563cfb6a943e411
View
@@ -0,0 +1,16 @@
+# == Schema Information
+#
+# Table name: item_variants
+#
+# id :integer not null, primary key
+# item_id :integer
+# certification :integer
+# paint_color :integer
+# inventory_percentage :float
+# wishlist_percentage :float
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+class ItemVariant < ApplicationRecord
+end
View
@@ -2,21 +2,17 @@
#
# Table name: user_items
#
-# id :integer not null, primary key
-# item_id :integer
-# user_id :integer
-# created_at :datetime not null
-# updated_at :datetime not null
-# paint_color :integer
-# certification :integer
-# list :integer
-# listed_percentage :float
+# id :integer not null, primary key
+# item_id :integer
+# user_id :integer
+# created_at :datetime not null
+# updated_at :datetime not null
+# paint_color :integer
+# certification :integer
+# list :integer
#
class UserItem < ApplicationRecord
- belongs_to :user
- belongs_to :item
-
enum list: [
:inventory,
:wishlist
@@ -56,9 +52,14 @@ class UserItem < ApplicationRecord
:orange
]
+ belongs_to :user
+ belongs_to :item
+
attr_accessor :certified
attr_accessor :painted
+ after_create :optionally_add_missing_variant
+
validates :item_id, presence: true
# validates_uniqueness_of :item_id, scope: [:certification, :paint_color], message: 'is already in your inventory'
validate :common_is_special
@@ -81,13 +82,21 @@ def owned_percentage
listed_percentage = 100 * (total_item_count.to_f / total_users)
end
- self.update_column(:listed_percentage, listed_percentage)
listed_percentage.round(1)
end
end
private
+ def optionally_add_missing_variant
+ # find or create the ItemVariant that represents this item
+ ItemVariant.find_or_create_by({
+ item_id: self.item_id,
+ certification: self.certification,
+ paint_color: self.paint_color
+ })
+ end
+
def common_is_special
if self.item && self.item.common_base? && (self.certification.nil? && self.paint_color.nil?)
errors.add(:item_id, "that is common is only tradeable if it is painted or certified")
@@ -0,0 +1,15 @@
+class CreateItemVariants < ActiveRecord::Migration[5.0]
+ def change
+ create_table :item_variants do |t|
+ t.integer :item_id
+ t.integer :certification
+ t.integer :paint_color
+ t.float :inventory_percentage
+ t.float :wishlist_percentage
+
+ t.timestamps
+ end
+
+ add_index :item_variants, [:item_id, :certification, :paint_color], unique: true, name: 'item_variant_id_cert_paint_index'
+ end
+end
@@ -0,0 +1,5 @@
+class RemoveListedPercentageFromUserItems < ActiveRecord::Migration[5.0]
+ def change
+ remove_column :user_items, :listed_percentage
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20160920014425) do
+ActiveRecord::Schema.define(version: 20160922060858) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -27,6 +27,17 @@
t.index ["sender_id", "sender_deleted"], name: "index_conversations_on_sender_id_and_sender_deleted", using: :btree
end
+ create_table "item_variants", force: :cascade do |t|
+ t.integer "item_id"
+ t.integer "certification"
+ t.integer "paint_color"
+ t.float "inventory_percentage"
+ t.float "wishlist_percentage"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["item_id", "certification", "paint_color"], name: "item_variant_id_cert_paint_index", unique: true, using: :btree
+ end
+
create_table "items", force: :cascade do |t|
t.string "name"
t.integer "kind", default: 0
@@ -61,12 +72,11 @@
create_table "user_items", force: :cascade do |t|
t.integer "item_id"
t.integer "user_id"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
t.integer "paint_color"
t.integer "certification"
t.integer "list"
- t.float "listed_percentage"
t.index ["certification"], name: "index_user_items_on_certification", using: :btree
t.index ["item_id"], name: "index_user_items_on_item_id", using: :btree
t.index ["list"], name: "index_user_items_on_list", using: :btree
@@ -0,0 +1,15 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ item_id: 1
+ certification: 1
+ paint_color: 1
+ inventory_percentage: 1.5
+ wishlist_percentage: 1.5
+
+two:
+ item_id: 1
+ certification: 1
+ paint_color: 1
+ inventory_percentage: 1.5
+ wishlist_percentage: 1.5
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ItemVariantTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 6b3a4c3

Please sign in to comment.