Module: TranslatedAttributes::ClassMethods

Defined in:
lib/translated_attributes.rb

Instance Method Summary collapse

Instance Method Details

#translated_attributes(*args) ⇒ Object

translated_attributes :title, :description, :table_name=>‘my_translations’



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/translated_attributes.rb', line 6

def translated_attributes(*args)
  #store options
  cattr_accessor :translated_attributes_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

  associated = options[:translatable_name] || :translatable
  begin
    klass = Object.const_get(class_name)
  rescue
    klass = Class.new(ActiveRecord::Base)
    Object.const_set(class_name, klass)
    klass.set_table_name table_name
    klass.belongs_to associated, :polymorphic => true
  end

  #set translations
  has_many :translations, :as => associated, :dependent => :delete_all, :class_name=>klass.name

  #include methods
  include TranslatedAttributes::InstanceMethods
end