Skip to content
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source 'https://rubygems.org'
source "https://git.uneek.eu/geminabox"

gemspec
15 changes: 11 additions & 4 deletions lib/globalize-accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def localized_attr_name_for(attr_name, locale)
private

def define_accessors(attr_name, locale)
if respond_to?(:attribute)
attribute localized_attr_name_for(attr_name, locale)
end
define_getter(attr_name, locale)
define_setter(attr_name, locale)
end
Expand All @@ -34,11 +37,15 @@ def define_getter(attr_name, locale)

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

define_method :"#{localized_attr_name}=" do |value|
attribute_will_change!(localized_attr_name) if value != send(localized_attr_name)
write_attribute(attr_name, value, :locale => locale)
translation_for(locale)[attr_name] = value
translation_for(locale)
if value != send(localized_attr_name)
super(value) # note: super is not defined if db/schema.rb is not loaded
attribute_will_change!(localized_attr_name)
write_attribute(attr_name, value, :locale => locale)
translation_for(locale)[attr_name] = value
end
value
end
if respond_to?(:accessible_attributes) && accessible_attributes.include?(attr_name)
attr_accessible :"#{localized_attr_name}"
Expand Down
2 changes: 1 addition & 1 deletion lib/globalize-accessors/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Globalize
module Accessors
VERSION = '0.3.0'
VERSION = '0.3.0-fix-dirty2'
end
end
21 changes: 21 additions & 0 deletions test/globalize_accessors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class UnitWithoutAccessors < ActiveRecord::Base
self.table_name = :units
end

class UnitWithFallback < ActiveRecord::Base
self.table_name = :units
translates :name, :title, fallbacks_for_empty_translations: true
globalize_accessors

def globalize_fallbacks(locale)
locale == :pl ? [:pl, :en] : [:en, :pl]
end
end

setup do
assert_equal :en, I18n.locale
end
Expand Down Expand Up @@ -196,4 +206,15 @@ def u.globalize_fallbacks(locale)
u.save!
assert ! u.changed.include?("name_en")
end

test "translated attribute with fallbacks_for_empty_translations should not change when accessors are assigned but not changed" do
attrs = {:name_en => "", :name_pl => "Name pl"} # name_en is empty in order to trigger fallback
u = UnitWithFallback.create!(attrs)
assert u.name == 'Name pl'
u = UnitWithFallback.find(u.id)
assert ! u.changed.include?("name")
u.assign_attributes(attrs)
assert ! u.changed.include?("name")
end

end