lib/translated_attributes.rb in teonimesic-translated_attributes-0.5.7 vs lib/translated_attributes.rb in teonimesic-translated_attributes-0.5.8

- old
+ new

@@ -50,29 +50,17 @@ base.after_save :store_translated_attributes end def attributes=(new_attributes, *args) - if I18n.available_locales.any?{|locale| new_attributes.stringify_keys.include? locale.to_s } - non_serialized_attributes = new_attributes - I18n.available_locales.each do |l| - new_attributes = get_language_attributes(l,non_serialized_attributes) - self.class.language(l){ super } - end - else - super + unless ( translations_hash = new_attributes.select{|k,v| is_translated_attribute_hash?(k) } ).blank? + @translated_attributes = translations_hash.with_indifferent_access + new_attributes.delete_if{|k,v| is_translated_attribute_hash?(k) } end + super end - def get_language_attributes(lang, params) - local_params = {} - if (params.include?(lang) || params.include?(lang.to_s) ) && I18n.available_locales.include?(lang.to_sym) - local_params = params.stringify_keys[lang.to_s] - end - params.reject{|k,v| I18n.available_locales.include? k.to_sym }.merge(local_params) - end - def get_translated_attribute(locale, field) text = if locale translated_attributes_for(locale)[field] else #try to find anything... @@ -93,11 +81,11 @@ end end def set_translated_attribute(locale, field, value) old_value = translated_attributes_for(locale)[field] - return if old_value == value + return if old_value.to_s == value.to_s changed_attributes.merge!("#{field}_in_#{locale}" => old_value) translated_attributes_for(locale)[field] = value @translated_attributes_changed = true end @@ -135,11 +123,11 @@ translations.delete_all @translated_attributes.each do |locale, attributes| attributes.each do |attribute, value| next if value.blank? next unless self.class.translated_attributes_options[:fields].include? attribute.to_sym - translations.create!(:attr=>attribute, :text=>value, :language=>locale) + translations.create!(:translated_attribute=>attribute, :text=>value, :language=>locale) end end @translated_attributes_changed = false end @@ -147,11 +135,11 @@ def merge_db_translations_with_instance_variable return if new_record? or @db_translations_merged @db_translations_merged = true translations.each do |t| - translated_attributes_for(t.language)[t.attr] = t.text + translated_attributes_for(t.language)[t.translated_attribute] = t.text end end def parse_translated_attribute_method(name) return false if name.to_s !~ /^([a-zA-Z_]+)_in_([a-z]{2})[=]?$/ @@ -161,9 +149,13 @@ end def is_translated_attribute?(method_name) fields = self.class.translated_attributes_options[:fields] fields.include? method_name.sub('=','').to_sym + end + + def is_translated_attribute_hash?(key) + I18n.available_locales.include?(key.to_s) || I18n.available_locales.include?(key.to_sym) end def translated_attributes_for(locale) translated_attributes[locale] ||= {}.with_indifferent_access translated_attributes[locale]