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

Container backend / cache : write method doesn't allow cache update #670

Open
dramalho opened this issue Feb 6, 2025 · 0 comments
Open

Comments

@dramalho
Copy link

dramalho commented Feb 6, 2025

Following up on this deranged sequence of comments I clone the repo and added a test to prove the point (at least as I see it)

# spec/mobility/backends/active_record/container_spec.rb
  context "with cache plugin" do
    plugins :active_record, :reader, :writer, :cache
    before { translates ContainerPost, :title, :content, backend: :container }

    let(:backend) { post.mobility_backends[:title] }

    include_accessor_examples 'ContainerPost'
    include_dup_examples 'ContainerPost'
    include_cache_key_examples 'ContainerPost'

    it 'correctly updates the cache when translations are changed' do
      post = ContainerPost.new

      post.title = "foo"
      expect(post.title).to eq("foo")

      post.title = "bar"
      expect(post.title).to eq("bar")

      post.save
    end
  end

which fails

 ORM=active_record DB=postgres rspec spec/mobility/backends/active_record/container_spec.rb:55                                                                                                                                        ✔  6s 
Skipping AllocationStats.
Run options:
  include {:focus=>true, :locations=>{"./spec/mobility/backends/active_record/container_spec.rb"=>[55]}}
  exclude {:orm=>#<Proc: ./spec/spec_helper.rb:95>, :db=>#<Proc: ./spec/spec_helper.rb:95>}

Randomized with seed 25927

Mobility::Backends::ActiveRecord::Container
  with cache plugin
    correctly updates the cache when translations are changed (FAILED - 1)
    behaves like model with translated attribute accessors
      sets translations in multiple locales when creating and saving model
      gets and sets translations in multiple locales
      sets translations in multiple locales when updating model
      gets and sets translations in one locale
    behaves like dupable model
      dups new record
      dups persisted model
    behaves like cache key
      changes cache key when translation updated

Failures:

  1) Mobility::Backends::ActiveRecord::Container with cache plugin correctly updates the cache when translations are changed
     Failure/Error: expect(post.title).to eq("bar")

       expected: "bar"
            got: "foo"

       (compared using ==)
     # ./spec/mobility/backends/active_record/container_spec.rb:72:in `block (3 levels) in <top (required)>'

Finished in 0.31598 seconds (files took 2.19 seconds to load)
8 examples, 1 failure

Failed examples:

rspec ./spec/mobility/backends/active_record/container_spec.rb:65 # Mobility::Backends::ActiveRecord::Container with cache plugin correctly updates the cache when translations are changed

The fix is - naively - easy

diff --git a/lib/mobility/backends/active_record/container.rb b/lib/mobility/backends/active_record/container.rb
index 5f14540..1fce61a 100644
--- a/lib/mobility/backends/active_record/container.rb
+++ b/lib/mobility/backends/active_record/container.rb
@@ -37,7 +37,8 @@ Implements the {Mobility::Backends::Container} backend for ActiveRecord models.
       # @return [String,Integer,Boolean] Updated value
       def write(locale, value, _ = nil)
         set_attribute_translation(locale, value)
-        read(locale)
+
+        value
       end
       # @!endgroup

The tests (for container) pass and I'm happy to open a PR with these two changes, but I wanted to run by you first :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant