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

Ensure cache_key is invalidated when updating translations #104

Merged
merged 4 commits into from
Sep 30, 2017
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
2 changes: 1 addition & 1 deletion lib/mobility/active_record/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ActiveRecord
class Translation < ::ActiveRecord::Base
self.abstract_class = true

belongs_to :translatable, polymorphic: true
belongs_to :translatable, polymorphic: true, touch: true

validates :key, presence: true, uniqueness: { scope: [:translatable_id, :translatable_type, :locale] }
validates :translatable, presence: true
Expand Down
3 changes: 2 additions & 1 deletion lib/mobility/backends/active_record/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def self.configure(options)
translation_class.belongs_to :translated_model,
class_name: name,
foreign_key: options[:foreign_key],
inverse_of: association_name
inverse_of: association_name,
touch: true

module_name = "MobilityArTable#{association_name.to_s.camelcase}"
unless const_defined?(module_name)
Expand Down
10 changes: 10 additions & 0 deletions spec/active_record/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def up
create_table "articles" do |t|
t.string :slug
t.boolean :published
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "article_translations" do |t|
Expand Down Expand Up @@ -81,12 +83,16 @@ def up
t.text :author_pt_br
t.text :author_ru
t.boolean :published
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "serialized_posts" do |t|
t.text :title
t.text :content
t.boolean :published
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

if ENV['DB'] == 'postgres'
Expand All @@ -99,6 +105,8 @@ def up
t.jsonb :content, default: ''
end
t.boolean :published
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

execute "CREATE EXTENSION IF NOT EXISTS hstore"
Expand All @@ -107,6 +115,8 @@ def up
t.hstore :title, default: ''
t.hstore :content, default: ''
t.boolean :published
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/mobility/backends/active_record/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

subject { comment }

include_cache_key_examples "Comment", :content

describe "#read" do
it "returns attribute in locale from appropriate column" do
aggregate_failures do
Expand Down
1 change: 1 addition & 0 deletions spec/mobility/backends/active_record/hstore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
include_querying_examples 'HstorePost'
include_validation_examples 'HstorePost'
include_dup_examples 'HstorePost'
include_cache_key_examples 'HstorePost'

describe "non-text values" do
it "converts non-string types to strings when saving" do
Expand Down
1 change: 1 addition & 0 deletions spec/mobility/backends/active_record/jsonb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
include_querying_examples 'JsonbPost'
include_validation_examples 'JsonbPost'
include_dup_examples 'JsonbPost'
include_cache_key_examples 'JsonbPost'

describe "non-text values" do
it "stores non-string types as-is when saving", rails_version_geq: '5.0' do
Expand Down
1 change: 1 addition & 0 deletions spec/mobility/backends/active_record/key_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
let(:article) { Article.new }
include_accessor_examples "Article"
include_dup_examples "Article"
include_cache_key_examples "Article"

it "finds translation on every read/write" do
expect(title_backend.send(:translations)).to receive(:find).thrice.and_call_original
Expand Down
1 change: 1 addition & 0 deletions spec/mobility/backends/active_record/serialized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
include_accessor_examples 'SerializedPost'
include_serialization_examples 'SerializedPost'
include_dup_examples 'SerializedPost'
include_cache_key_examples 'SerializedPost'

describe "non-text values" do
it "converts non-string types to strings when saving", rails_version_geq: '5.0' do
Expand Down
1 change: 1 addition & 0 deletions spec/mobility/backends/active_record/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
before { Article.translates :title, :content, backend: :table, cache: false }
include_accessor_examples "Article"
include_dup_examples "Article"
include_cache_key_examples "Article"

it "finds translation on every read/write" do
article = Article.new
Expand Down
9 changes: 5 additions & 4 deletions spec/mobility/plugins/active_record/dirty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def clear_changes_information
article.save

expect(article.changed?).to eq(false)
expect(article.previous_changes).to eq({ "title_en" => ["foo", "bar"]})
expect(article.previous_changes).to include({ "title_en" => ["foo", "bar"]})
end
end

Expand All @@ -101,7 +101,7 @@ def clear_changes_information

article.save

expect(article.instance_variable_get(:@actual_previous_changes)).to eq({ "title_en" => ["foo", "bar"]})
expect(article.instance_variable_get(:@actual_previous_changes)).to include({ "title_en" => ["foo", "bar"]})
end

it "tracks changes in multiple locales" do
Expand Down Expand Up @@ -136,8 +136,9 @@ def clear_changes_information

article.save

expect(article.previous_changes).to eq({"title_en" => ["English title 1", "English title 2"],
"title_fr" => ["Titre en Francais 1", "Titre en Francais 2"]})
expect(article.previous_changes).to include({
"title_en" => ["English title 1", "English title 2"],
"title_fr" => ["Titre en Francais 1", "Titre en Francais 2"]})
end

it "resets changes when locale is set to original value" do
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
RSpec.configure do |config|
config.include Helpers
config.include Mobility::Util
if defined?(ActiveSupport)
require 'active_support/testing/time_helpers'
config.include ActiveSupport::Testing::TimeHelpers
end

config.filter_run focus: true
config.run_all_when_everything_filtered = true
Expand Down
4 changes: 4 additions & 0 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def include_backend_examples *args
def include_dup_examples *args
it_behaves_like "dupable model", *args
end

def include_cache_key_examples *args
it_behaves_like "cache key", *args
end
end

module ActiveRecord
Expand Down
12 changes: 12 additions & 0 deletions spec/support/shared_examples/cache_key_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
shared_examples_for "cache key" do |model_class_name, attribute=:title|
let(:model_class) { constantize(model_class_name) }

it "changes cache key when translation updated" do
model = model_class.create!(attribute => "foo")
original_cache_key = model.cache_key
travel 1.second do
model.update_attributes!(attribute => "bar")
end
expect(model.cache_key).to_not eq(original_cache_key)
end
end