Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin basic customization texts #3488

Merged
merged 2 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
Simplify getting I18nContent translations
This code might be slightly slower because it performs one query per
field in the form, but I didn't notice any differences on my development
machine, and the code is now much easier to understand.
  • Loading branch information
javierm committed May 21, 2019
commit 1135441cbde5b5d5d0eed8bdef0a2c1d5a7d4df1
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
before_action :delete_translations, only: [:update]

def index
fetch_existing_keys
append_or_create_keys
@content = @content[@tab.to_s]
@tab = params[:tab] || :debates
@content = I18nContent.content_for(@tab)
end

def update
Expand Down Expand Up @@ -50,29 +49,6 @@ def delete_translations
end
end

def fetch_existing_keys
@existing_keys = {}
@tab = params[:tab] || :debates

I18nContent.begins_with_key(@tab).map { |content|
@existing_keys[content.key] = content
}
end

def append_or_create_keys
@content = {}
I18n.backend.send(:init_translations) unless I18n.backend.initialized?

locale = params[:locale] || I18n.locale
translations = I18n.backend.send(:translations)[locale.to_sym]

translations.each do |key, value|
@content[key.to_s] = I18nContent.flat_hash(value).keys.map { |string|
@existing_keys["#{key.to_s}.#{string}"] || I18nContent.new(key: "#{key.to_s}.#{string}")
}
end
end

def translation_params
I18nContent.translated_attribute_names.product(enabled_translations).map do |attr_name, loc|
I18nContent.localized_attr_name_for(attr_name, loc)
Expand Down
6 changes: 2 additions & 4 deletions app/helpers/site_customization_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ def site_customization_display_translation_style(locale)
end

def translation_for_locale(content, locale)
i18n_content = I18nContent.where(key: content.key).first

if i18n_content.present?
if content.present?
I18nContentTranslation.where(
i18n_content_id: i18n_content.id,
i18n_content_id: content.id,
locale: locale
).first.try(:value)
else
Expand Down
14 changes: 13 additions & 1 deletion app/models/i18n_content.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class I18nContent < ApplicationRecord

scope :by_key, ->(key) { where(key: key) }
scope :begins_with_key, ->(key) { where("key ILIKE ?", "#{key}%") }

validates :key, uniqueness: true

Expand Down Expand Up @@ -46,4 +45,17 @@ def self.flat_hash(input, path = nil, output = {})
return output
end

def self.content_for(tab)
flat_hash(translations_for(tab)).keys.map do |string|
I18nContent.find_or_initialize_by(key: string)
end
end

def self.translations_for(tab)
I18n.backend.send(:init_translations) unless I18n.backend.initialized?

I18n.backend.send(:translations)[I18n.locale].select do |key, _translations|
key.to_s == tab.to_s
end
end
end
14 changes: 0 additions & 14 deletions spec/models/i18n_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@
expect(query.size).to eq(1)
expect(query).to eq([debate_title])
end

it "return all matching records when #begins_with_key is used" do
debate_text = create(:i18n_content, key: "debates.form.debate_text")
debate_title = create(:i18n_content, key: "debates.form.debate_title")
proposal_title = create(:i18n_content, key: "proposals.form.proposal_title")

expect(I18nContent.all.size).to eq(3)

query = I18nContent.begins_with_key("debates")

expect(query.size).to eq(2)
expect(query).to eq([debate_text, debate_title])
expect(query).not_to include(proposal_title)
end
end

context "Globalize" do
Expand Down