Skip to content

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

@dramalho

Description

@dramalho

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions