diff --git a/app/views/account/categories/index.html.erb b/app/views/account/categories/index.html.erb
index 9742a90f5..e6dbaacf1 100644
--- a/app/views/account/categories/index.html.erb
+++ b/app/views/account/categories/index.html.erb
@@ -5,7 +5,7 @@
<%= render partial: "account/shared/search_form", locals: {
q: @q,
search_url: account_categories_path,
- search_attribute: :name_cont
+ search_attribute: @search_attribute
} %>
diff --git a/app/views/account/categories/new.html.erb b/app/views/account/categories/new.html.erb
index f16c39b90..f9f6156d6 100644
--- a/app/views/account/categories/new.html.erb
+++ b/app/views/account/categories/new.html.erb
@@ -1,14 +1,15 @@
-<%= simple_form_for @category, url: { action: 'create' }, html: { novalidate: false } do |f| %>
+<%= simple_form_for @category, url: { action: "create" }, html: { novalidate: false } do |f| %>
- <%= f.submit t('.form.create_category_button'), class: 'btn btn-green me-2' %>
- <%= link_to account_categories_path, class: 'btn btn-danger d-flex align-items-center justify-content-center' do %>
- <%= t('buttons.cancel') %>
+ <%= f.submit t(".form.create_category_button"), class: "btn btn-green me-2" %>
+ <%= link_to account_categories_path, class: "btn btn-danger d-flex align-items-center justify-content-center" do %>
+ <%= t("buttons.cancel") %>
<% end %>
<% end %>
diff --git a/config/locales/en/en.yml b/config/locales/en/en.yml
index 90f476e0e..49f228fbc 100644
--- a/config/locales/en/en.yml
+++ b/config/locales/en/en.yml
@@ -669,6 +669,8 @@ en:
preferable: "Preferable"
category:
name: "Title"
+ en_name: "Title in English"
+ uk_name: "Title in Ukrainian"
priority: "Priority"
preferable: "Main"
price:
diff --git a/config/locales/uk/uk.yml b/config/locales/uk/uk.yml
index 4354d31c7..6e7fc087c 100644
--- a/config/locales/uk/uk.yml
+++ b/config/locales/uk/uk.yml
@@ -546,7 +546,7 @@ uk:
invalid: "містить неприпустимі символи"
category:
attributes:
- name:
+ uk_name: &name_errors
blank: "не може бути порожньою"
too_long:
one: "занадто довга (максимум %{count} знак)"
@@ -560,6 +560,8 @@ uk:
other: "занадто коротка (мінімум %{count} знаків)"
invalid: "містить неприпустимі символи"
taken: "вже викориcтовується, будь ласка, оберіть іншу"
+ en_name:
+ <<: *name_errors
site_setting:
attributes:
title:
@@ -666,6 +668,8 @@ uk:
preferable: "Бажаний"
category:
name: "Назва"
+ en_name: "Назва Англійською"
+ uk_name: "Назва Українською"
priority: "Пріоритет"
preferable: "Основна"
price:
diff --git a/db/migrate/20240515233604_add_en_name_and_rename_name_to_uk_name_in_categories.rb b/db/migrate/20240515233604_add_en_name_and_rename_name_to_uk_name_in_categories.rb
new file mode 100644
index 000000000..cfd745f35
--- /dev/null
+++ b/db/migrate/20240515233604_add_en_name_and_rename_name_to_uk_name_in_categories.rb
@@ -0,0 +1,17 @@
+class AddEnNameAndRenameNameToUkNameInCategories < ActiveRecord::Migration[7.1]
+ def up
+ remove_index :categories, name: "index_categories_on_name"
+ rename_column :categories, :name, :uk_name
+ add_index :categories, "lower((uk_name)::text)", name: "index_categories_on_uk_name", unique: true
+ add_column :categories, :en_name, :string
+ add_index :categories, "lower((en_name)::text)", name: "index_categories_on_en_name", unique: true
+ end
+
+ def down
+ remove_index :categories, name: "index_categories_on_uk_name"
+ rename_column :categories, :uk_name, :name
+ add_index :categories, "lower((name)::text)", name: "index_categories_on_name", unique: true
+ remove_index :categories, name: "index_categories_on_en_name"
+ remove_column :categories, :en_name
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 7042f48cf..8885764a3 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.1].define(version: 2024_02_08_154012) do
+ActiveRecord::Schema[7.1].define(version: 2024_05_15_233604) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
@@ -56,12 +56,14 @@
end
create_table "categories", force: :cascade do |t|
- t.string "name"
+ t.string "uk_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "priority", default: 0, null: false
t.boolean "preferable", default: false, null: false
- t.index "lower((name)::text)", name: "index_categories_on_name", unique: true
+ t.string "en_name"
+ t.index "lower((en_name)::text)", name: "index_categories_on_en_name", unique: true
+ t.index "lower((uk_name)::text)", name: "index_categories_on_uk_name", unique: true
end
create_table "category_categoryables", force: :cascade do |t|
diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb
index 6b20e3332..9148b7609 100644
--- a/spec/factories/categories.rb
+++ b/spec/factories/categories.rb
@@ -10,10 +10,12 @@
#
FactoryBot.define do
factory :category do
- name { "name" }
+ en_name { "category" }
+ uk_name { "категорія" }
trait :budgetary do
- name { "budgetary" }
+ en_name { "budgetary" }
+ uk_name { "бюджетна" }
after(:create) do |category|
create(:diapers_period, category: category)
@@ -21,7 +23,8 @@
end
trait :medium do
- name { "medium" }
+ en_name { "medium" }
+ uk_name { "середня" }
preferable { true }
after(:create) do |category|
@@ -30,7 +33,8 @@
end
trait :without_diapers_period do
- name { "without-diapers-period" }
+ en_name { "without-diapers-period" }
+ uk_name { "без-diapers-period" }
end
end
end
diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb
index 4cf531c9a..514c45006 100644
--- a/spec/models/category_spec.rb
+++ b/spec/models/category_spec.rb
@@ -18,19 +18,22 @@
end
describe "validations" do
- it { is_expected.to validate_presence_of(:name) }
- it { is_expected.to validate_length_of(:name).is_at_least(3).is_at_most(30) }
- it { is_expected.to validate_uniqueness_of(:name).case_insensitive }
+ it { is_expected.to validate_presence_of(:en_name) }
+ it { is_expected.to validate_length_of(:en_name).is_at_least(3).is_at_most(30) }
+ it { is_expected.to validate_uniqueness_of(:en_name).case_insensitive }
+ it { is_expected.to validate_presence_of(:uk_name) }
+ it { is_expected.to validate_length_of(:uk_name).is_at_least(3).is_at_most(30) }
+ it { is_expected.to validate_uniqueness_of(:uk_name).case_insensitive }
it { is_expected.to validate_numericality_of(:priority).is_greater_than_or_equal_to(0) }
end
context "checking the number of errors" do
- let(:category) { build(:category, name: "") }
+ let(:category) { build(:category, en_name: "") }
it "returns only one error message when field is blank" do
category.valid?
- expect(category.errors.full_messages_for(:name).length).to eq 1
- expect(category.errors.full_messages_for(:name)).to include("Title can't be blank")
+ expect(category.errors.full_messages_for(:en_name).length).to eq 1
+ expect(category.errors.full_messages_for(:en_name)).to include("Title in English can't be blank")
end
end
end
diff --git a/spec/models/conserns/translatable_spec.rb b/spec/models/conserns/translatable_spec.rb
new file mode 100644
index 000000000..3460bab3f
--- /dev/null
+++ b/spec/models/conserns/translatable_spec.rb
@@ -0,0 +1,32 @@
+require "rails_helper"
+
+RSpec.describe Translatable do
+ describe "translations" do
+ context "when en_name and uk_name are exist in model" do
+ let(:dummy_translatable) { DummyTranslatable.new }
+
+ it "returns the en_name when locale is set to :en" do
+ allow(I18n).to receive(:locale).and_return(:en)
+
+ expect(dummy_translatable.name).to eq("Diapers")
+ end
+
+ it "returns the uk_name when locale is set to :uk" do
+ allow(I18n).to receive(:locale).and_return(:uk)
+
+ expect(dummy_translatable.name).to eq("Підгузки")
+ end
+ end
+
+ context "when en_name is not exist and a default locale is uk" do
+ let(:dummy_translatable) { DummyTranslatable.new(en_name: nil) }
+
+ it "returns the uk_name when locale is set to :en" do
+ allow(I18n).to receive(:locale).and_return(:en)
+ allow(I18n).to receive(:default_locale).and_return(:uk)
+
+ expect(dummy_translatable.name).to eq("Підгузки")
+ end
+ end
+ end
+end
diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb
index 28ad265d9..324a3d7ea 100644
--- a/spec/models/product_spec.rb
+++ b/spec/models/product_spec.rb
@@ -70,7 +70,7 @@
describe "#find_or_build_price_for_category" do
let(:product) { create(:product, title: "Valid Title") }
- let(:category) { create(:category, name: "Valid Name") }
+ let(:category) { create(:category, :medium) }
let(:valid_sum) { 10.0 }
context "when the product has a price for the category" do
diff --git a/spec/requests/categories_spec.rb b/spec/requests/categories_spec.rb
index 03456537b..8d0648d8e 100644
--- a/spec/requests/categories_spec.rb
+++ b/spec/requests/categories_spec.rb
@@ -2,9 +2,9 @@
RSpec.describe Account::CategoriesController, type: :request do
let!(:category) { create(:category, :budgetary) }
- let(:valid_attributes) { { category: { name: "medium" }} }
- let(:invalid_attributes) { { category: { name: "" }} }
- let(:new_attributes) { { category: { name: "premium" }} }
+ let(:valid_attributes) { { category: { en_name: "medium", uk_name: "середня" }} }
+ let(:invalid_attributes) { { category: { en_name: "" }} }
+ let(:new_attributes) { { category: { en_name: "premium" }} }
include_context :authorize_admin
diff --git a/spec/services/categories/preferable_service_spec.rb b/spec/services/categories/preferable_service_spec.rb
index 6bd6ac4d3..cca69c37b 100644
--- a/spec/services/categories/preferable_service_spec.rb
+++ b/spec/services/categories/preferable_service_spec.rb
@@ -3,8 +3,8 @@
RSpec.describe Categories::PreferableService do
describe "#call" do
let!(:become_preferable_category) { create(:category, :medium) }
- let!(:category) { create(:category, name: "category") }
- let!(:current_preferable_category) { create(:category, name: "current preferable category", preferable: true) }
+ let!(:category) { create(:category, :budgetary) }
+ let!(:current_preferable_category) { create(:category, preferable: true) }
let!(:service) { described_class.new(become_preferable_category) }
context "when the category is preferable" do
diff --git a/spec/support/dummy_translatable.rb b/spec/support/dummy_translatable.rb
new file mode 100644
index 000000000..e96b264f4
--- /dev/null
+++ b/spec/support/dummy_translatable.rb
@@ -0,0 +1,22 @@
+class DummyTranslatable
+ include Translatable
+
+ attr_accessor :uk_name
+
+ translates :name
+
+ def initialize(en_name: "Diapers", uk_name: "Підгузки")
+ @en_name = en_name
+ @uk_name = uk_name
+
+ define_en_name if @en_name
+ end
+
+ private
+
+ # Define en_name only if en_name is present for testing case with uk defaul locale
+ # when en_name not exist at all
+ def define_en_name
+ define_singleton_method(:en_name) { @en_name }
+ end
+end