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

Track changed attributes #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions lib/globalize-accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,28 @@ def define_getter(attr_name, locale)
define_method localized_attr_name_for(attr_name, locale) do
globalize.stash.contains?(locale, attr_name) ? globalize.send(:fetch_stash, locale, attr_name) : globalize.send(:fetch_attribute, locale, attr_name)
end

define_method "#{localized_attr_name_for(attr_name, locale)}_was" do
self.attribute_was(localized_attr_name_for(attr_name, locale))
end
end

def define_setter(attr_name, locale)
localized_attr_name = localized_attr_name_for(attr_name, locale)

define_method :"#{localized_attr_name}=" do |value|
# custom dirty tracking to enable #{attr_name}_was, taken from globalize/rails
if attribute_changed?(localized_attr_name)
# If there's already a change, delete it if this undoes the change.
old = changed_attributes[localized_attr_name]
@changed_attributes.delete(localized_attr_name) if value == old
else
# If there's not a change yet, record it.
old = globalize.fetch(locale, attr_name)
old = old.dup if old.duplicable?
@changed_attributes[localized_attr_name] = old if value != old
end

write_attribute(attr_name, value, :locale => locale)
translation_for(locale)[attr_name] = value
end
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Product
end
````

Gives you access to methods: `title_pl`, `title_en`, `title_pl=`, `title_en=`. These work seamlessly with Globalize (not even touching the "core" `title`, `title=` methods used by Globalize itself).
Gives you access to methods: `title_pl`, `title_en`, `title_pl=`, `title_en=`, `title_en_was`, `title_pl_was`. These work seamlessly with Globalize (not even touching the "core" `title`, `title=` methods used by Globalize itself).

The `:locales` and `:attributes` options are optional. Their default values are:

Expand Down
7 changes: 7 additions & 0 deletions test/globalize_accessors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class UnitWithoutAccessors < ActiveRecord::Base
assert_equal "Name en", u.name_en
assert_equal "Title pl", u.title_pl

assert_equal nil, u.name_en_was
assert_equal nil, u.title_pl_was

assert_nil u.name_pl
assert_nil u.title_en
end
Expand Down Expand Up @@ -98,6 +101,10 @@ class UnitWithoutAccessors < ActiveRecord::Base

u.name_pl = "Name pl"
u.name_en = "Name en2"

assert_equal "Name en", u.name_en_was
assert_equal nil, u.name_pl_was

u.save!

assert_equal "Name en2", u.name
Expand Down