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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make eager loading single translation work #24

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/mobility/backends/action_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class ActionText < ActiveRecord::KeyValue
# override to return record instead of value
def read(locale, **options)
return super if self.options[:plain]

if model.association_cached?("rich_text_#{attribute}")
eager_loaded = model.public_send("rich_text_#{attribute}")
return eager_loaded if eager_loaded.locale == locale.to_s
end

translation_for(locale, **options)
end

Expand Down
14 changes: 12 additions & 2 deletions test_app/test/mobility_action_text_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ class ActionTextTest < ActiveSupport::TestCase

assert_no_queries do
assert_equal 'Hello world!', post.content.to_plain_text

skip('FIXME: this should execute no queries')
end
end

test 'correct translation is returned if single translation for different locale was eager loaded' do
post = assert_queries(2) { Mobility.with_locale(:en) { Post.with_rich_text_content.last } }

assert_equal 'Bonjour le monde !', Mobility.with_locale(:fr) { post.content }.to_plain_text
end

test 'post content is eager loaded with all rich text' do
post = assert_queries(2) { Post.with_all_rich_text.last }

Expand All @@ -113,6 +117,12 @@ class ActionTextTest < ActiveSupport::TestCase
end
end

test 'correct translation is returned if all translations for different locale were eager loaded' do
post = assert_queries(2) { Mobility.with_locale(:en) { Post.with_all_rich_text.last } }

assert_equal 'Bonjour le monde !', Mobility.with_locale(:fr) { post.content }.to_plain_text
end

test 'post non_i18n_content is eager loaded with all rich text' do
post = assert_queries(2) { Post.with_all_rich_text.last }

Expand Down