Remove redundant I18nContent scope

Since two records cannot have the same key, having a scope that will
always return just one record is the same as using `find_by_key`.
This commit is contained in:
Javi Martín
2019-09-22 00:14:13 +02:00
parent 9fcea17849
commit 9d627f2db9
3 changed files with 1 additions and 19 deletions

View File

@@ -1,7 +1,4 @@
class I18nContent < ApplicationRecord
scope :by_key, ->(key) { where(key: key) }
validates :key, uniqueness: true
translates :value, touch: true

View File

@@ -9,7 +9,7 @@ module ActionView
def t(key, options = {})
current_locale = options[:locale].presence || I18n.locale
i18_content = I18nContent.by_key(key).first
i18_content = I18nContent.find_by_key(key)
translation = I18nContentTranslation.where(i18n_content_id: i18_content&.id,
locale: current_locale).first&.value
translation.presence || translate(key, options)

View File

@@ -14,21 +14,6 @@ RSpec.describe I18nContent, type: :model do
expect(i18n_content.errors.size).to eq(1)
end
context "Scopes" do
it "return one record when #by_key is used" do
content = create(:i18n_content)
key = "debates.form.debate_title"
debate_title = create(:i18n_content, key: key)
expect(I18nContent.all.size).to eq(2)
query = I18nContent.by_key(key)
expect(query.size).to eq(1)
expect(query).to eq([debate_title])
end
end
context "Globalize" do
it "translates key into multiple languages" do
key = "devise_views.mailer.confirmation_instructions.welcome"