Module: RailsTranslateModels

Defined in:
lib/rails-translate-models.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#has_translations(*args) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails-translate-models.rb', line 2

def has_translations(*args)
  # store options
  cattr_accessor :has_translations_options
  self.has_translations_options = args

  # create translations class
  type = self.to_s.underscore
  translations_klass_name = "#{self}_translation".classify
  translations_table_name = translations_klass_name.pluralize.tableize.to_sym

  translations_klass = Class.new(ActiveRecord::Base) do
    self.table_name = translations_table_name
    belongs_to type.to_sym
    validates_presence_of type.to_sym, :language_code
    validates_uniqueness_of :language_code, :scope => "#{type}_id"
  end

  Object.const_set(translations_klass_name, translations_klass)

  # set translations association, scoping, and after_save
  has_many :translations, :class_name => translations_klass_name, :dependent => :destroy
  default_scope :include => :translations

  after_save :store_translated_attributes

  # include methods
  include InstanceMethods
end