module RailsTranslateModels

Public Instance Methods

has_translations(*args) click to toggle source
# 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