diff --git a/app/controllers/dictionary_controller.rb b/app/controllers/dictionary_controller.rb
index 0f9ba1b..722e7ba 100644
--- a/app/controllers/dictionary_controller.rb
+++ b/app/controllers/dictionary_controller.rb
@@ -1,6 +1,12 @@
class DictionaryController < ApplicationController
def index
- @parts_of_speech = PartOfSpeech.all
+ @english = Language.where(name: "English")
+ @parts_of_speech = PartOfSpeech.where(language_id: @english)
+
+ @parts_of_speech.each do |pos|
+ pos.translations.build
+ end
+
@words = Word.all
end
diff --git a/app/models/part_of_speech.rb b/app/models/part_of_speech.rb
index 2da3a7a..7f2930f 100644
--- a/app/models/part_of_speech.rb
+++ b/app/models/part_of_speech.rb
@@ -1,3 +1,5 @@
class PartOfSpeech < ApplicationRecord
belongs_to :language
+
+ has_many :translations, class_name: "PartOfSpeech", foreign_key: "part_of_speech_id"
end
diff --git a/app/views/application/_partsofspeech.html.erb b/app/views/application/_partsofspeech.html.erb
index 36e9fac..cc0983b 100644
--- a/app/views/application/_partsofspeech.html.erb
+++ b/app/views/application/_partsofspeech.html.erb
@@ -4,11 +4,17 @@
Part of Speech |
Definition |
+ <% unless current_user.blank? %>
+ Translation |
+ <% end %>
<% parts_of_speech.each do |item| %>
<%= item.pos %> |
<%= item.definition %> |
+ <% unless current_user.blank? %>
+ <%= item.translations.first.definition %> |
+ <% end %>
<% end %>
diff --git a/db/migrate/20231031192706_add_user_id_to_part_of_speech.rb b/db/migrate/20231031192706_add_user_id_to_part_of_speech.rb
new file mode 100644
index 0000000..41b8054
--- /dev/null
+++ b/db/migrate/20231031192706_add_user_id_to_part_of_speech.rb
@@ -0,0 +1,7 @@
+class AddUserIdToPartOfSpeech < ActiveRecord::Migration[7.1]
+ def change
+ change_table :part_of_speeches do |t|
+ t.references :user, null: true, foreign_key: true
+ end
+ end
+end
diff --git a/db/migrate/20231031200646_add_self_referential_relationship_to_part_of_speech.rb b/db/migrate/20231031200646_add_self_referential_relationship_to_part_of_speech.rb
new file mode 100644
index 0000000..c439dee
--- /dev/null
+++ b/db/migrate/20231031200646_add_self_referential_relationship_to_part_of_speech.rb
@@ -0,0 +1,7 @@
+class AddSelfReferentialRelationshipToPartOfSpeech < ActiveRecord::Migration[7.1]
+ def change
+ change_table :part_of_speeches do |t|
+ t.references :part_of_speech, null: true, foreign_key: true
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index d907cd6..4c1665b 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: 2023_10_25_010226) do
+ActiveRecord::Schema[7.1].define(version: 2023_10_31_200646) do
create_table "definitions", force: :cascade do |t|
t.string "pos"
t.string "definition"
@@ -40,7 +40,11 @@ ActiveRecord::Schema[7.1].define(version: 2023_10_25_010226) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "language_id", null: false
+ t.integer "user_id"
+ t.integer "part_of_speech_id"
t.index ["language_id"], name: "index_part_of_speeches_on_language_id"
+ t.index ["part_of_speech_id"], name: "index_part_of_speeches_on_part_of_speech_id"
+ t.index ["user_id"], name: "index_part_of_speeches_on_user_id"
end
create_table "users", force: :cascade do |t|
@@ -64,4 +68,6 @@ ActiveRecord::Schema[7.1].define(version: 2023_10_25_010226) do
add_foreign_key "definitions", "languages"
add_foreign_key "definitions", "words"
add_foreign_key "part_of_speeches", "languages"
+ add_foreign_key "part_of_speeches", "part_of_speeches"
+ add_foreign_key "part_of_speeches", "users"
end
diff --git a/spec/factories/part_of_speech.rb b/spec/factories/part_of_speech.rb
index a38a5d7..28a9e84 100644
--- a/spec/factories/part_of_speech.rb
+++ b/spec/factories/part_of_speech.rb
@@ -1,7 +1,13 @@
FactoryBot.define do
factory :part_of_speech do
sequence(:pos) { |n| "test-#{n.to_s}" }
- language_id { FactoryBot.create(:language).id }
+ language_id { FactoryBot.create(:language_english).id }
definition { "test definition" }
end
+
+ factory :part_of_speech_translation, class: :part_of_speech do
+ sequence(:pos) { |n| "test-#{n.to_s}" }
+ language_id { FactoryBot.create(:language).id }
+ definition { "translated definition" }
+ end
end
diff --git a/spec/requests/dictionary_spec.rb b/spec/requests/dictionary_spec.rb
index 266332d..89916d8 100644
--- a/spec/requests/dictionary_spec.rb
+++ b/spec/requests/dictionary_spec.rb
@@ -23,6 +23,19 @@ RSpec.describe "Dictionary", type: :request do
get "/dictionary"
expect(response.body).to include("#{Word.count} word entries in database")
end
+
+ describe "signed in" do
+ before :each do
+ sign_in FactoryBot.create(:user)
+ end
+
+ it "only lists the 11 parts of speech, in english" do
+ 11.times { FactoryBot.create(:part_of_speech) }
+ FactoryBot.create(:part_of_speech_translation)
+ get "/dictionary"
+ expect(response.body).to have_selector(%(tr), count: 12)
+ end
+ end
end
describe "GET /show" do