lib/translated_attributes.rb in translated_attributes-0.5.5 vs lib/translated_attributes.rb in translated_attributes-0.6.0
- old
+ new
@@ -1,12 +1,15 @@
+require 'active_record'
+
module TranslatedAttributes
module ClassMethods
# translated_attributes :title, :description, :table_name=>'my_translations'
def translated_attributes(*args)
#store options
cattr_accessor :translated_attributes_options
- options = args.extract_options! || {}
+ options = (args.extract_options! || {}).dup
+ options[:attribute_column] ||= :translated_attribute
self.translated_attributes_options = options.merge(:fields=>args.map(&:to_sym))
#create translations class
table_name = options[:table_name] || :translations
class_name = table_name.to_s.classify
@@ -115,22 +118,23 @@
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!(:attribute=>attribute, :text=>value, :language=>locale)
+ translations.create!(translated_attributes_options[:attribute_column] => attribute, :text=>value, :language=>locale)
end
end
@translated_attributes_changed = false
+ true
end
private
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.attribute] = t.text
+ translated_attributes_for(t.language)[t[translated_attributes_options[:attribute_column]]] = t.text
end
end
def parse_translated_attribute_method(name)
return false if name.to_s !~ /^([a-zA-Z_]+)_in_([a-z]{2})[=]?$/