Leadformance/hstore_translate

Mark the translation field as dirty, to enable lifecycle callbacks

Closed this issue · 4 comments

Like,

class MyModel < ActiveRecord::Base
  translates :name
  after_save :do_something, :if => :name_changed?

  def do_something
  # ...
  end
end

This can be achieved by just adding

send "#{attr_name}_will_change!" unless translations[locale.to_s] == value

in the write_hstore_translation method.

Thanks - looks good. Do you have time to contribute a pull request (with test) to close this issue?

Rails will also only persist the hstore field if it was marked dirty in the first place. I can unfortunately not make a test case since your test setup does not mimic rails completely. If you create a new rails instance it is very easy to test though. If you uncomment the dirty tracking code it will pass otherwise it won't. In your test setup it does actually persist the changes though. I will check ActiveRecord documentation to see how this can be turned on to work like vanilla rails.

p = Post.create(title_en: "First Text")
p.title_en = "Second Text"
#p.title_translations_will_change!
p.save
assert_equal("Second Text", Post.last.title_en)

I just found out the issue only exists in rails 4. My gem cutting skills are not good enough to allow me to add rails 4 as a separate environment option in the Gemfile. If you could update the whole shebang to allow a test run in rails 4, I can provide the test and fix. Currently I just substituted the Gemfile for the following and bundle install

gem 'pg', '~> 0.15.1'
gem 'rails', '4.0.0.rc1'

Now the aforementioned test also works in your test file. I forked your project and added the necessary code. Either test it with my method or a saner way that you hopefully know.

Closing with version 0.0.7 thanks to @nasmorn.