Skip to content

Commit

Permalink
add uniqueness for product and validation in model, change all auplac…
Browse files Browse the repository at this point in the history
…ated products if exists with migration
  • Loading branch information
AndriyAndriyovuch committed Sep 17, 2023
1 parent f8acd86 commit 1a95a02
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Product < ApplicationRecord
has_many :prices, as: :priceable, dependent: :destroy
has_many :categories_by_prices, through: :prices, source: :category

validates :title, presence: true, length: { in: 2..50 }
validates :title, presence: true, length: { in: 2..50 }, uniqueness: true

scope :ordered_by_title, -> { order(:title) }

Expand Down
1 change: 1 addition & 0 deletions config/locales/en/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ en:
blank: can't be blank
too_long: too long (maximum %{count} symbols)
too_short: too short (minimum %{count} symbols)
taken: "has already been taken, please choose another one"
form_errors:
one: "One error prohibited %{model} from being saved"
other: "%{count} errors prohibited this %{model} from being saved"
Expand Down
1 change: 1 addition & 0 deletions config/locales/uk/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ uk:
few: занадто коротка (мінімум %{count} знаки)
many: занадто коротка (мінімум %{count} знаків)
other: занадто коротка (мінімум %{count} знаку)
taken: "вже викоритовується, будь ласка оберіть іншу"
user:
attributes:
avatar:
Expand Down
20 changes: 20 additions & 0 deletions db/migrate/20230917082603_add_index_to_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class AddIndexToProduct < ActiveRecord::Migration[6.1]
def up
duplicated_titles = Product
.select(:title)
.group(:title)
.having('count(title) > 1')
.pluck(:title)

duplicated_titles.each do |dup_title|
products = Product.where(title: dup_title)
products.map { |product| product.update(title: "#{product.title}_#{product.uuid}")}
end

add_index :products, :title, unique: true
end

def down
remove_index :products, :title
end
end
6 changes: 4 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_07_10_212500) do
ActiveRecord::Schema.define(version: 2023_09_17_082603) do

# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
Expand Down Expand Up @@ -159,6 +159,7 @@
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["product_type_id"], name: "index_products_on_product_type_id"
t.index ["title"], name: "index_products_on_title", unique: true
t.index ["uuid"], name: "index_products_on_uuid", unique: true
end

Expand Down Expand Up @@ -200,7 +201,8 @@
end

create_table "versions", force: :cascade do |t|
t.string "item_type", null: false
t.string "item_type"
t.string "{:null=>false}"
t.bigint "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
Expand Down

0 comments on commit 1a95a02

Please sign in to comment.