Sha256: cf7e086b659834aa950d3a75fc7a14909f53b3d579301debc52d59bd10af7487

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

module Globalize
  module ActiveRecord
    module ActMacro
      def translates(*attr_names)
        return if translates?

        options = attr_names.extract_options!
        options[:table_name] ||= "#{table_name.singularize}_translations"

        class_inheritable_accessor :translated_attribute_names, :translation_options
        self.translated_attribute_names = attr_names.map(&:to_sym)
        self.translation_options        = options

        include InstanceMethods
        extend  ClassMethods, Migration

        has_many :translations, :class_name  => translation_class.name,
                                :foreign_key => class_name.foreign_key,
                                :dependent   => :delete_all,
                                :extend      => HasManyExtensions

        after_save :save_translations!

        attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
      end

      def class_name
        class_name = table_name[table_name_prefix.length..-(table_name_suffix.length + 1)].camelize
        pluralize_table_names ? class_name.singularize : class_name
      end

      def translates?
        included_modules.include?(InstanceMethods)
      end
    end

    module HasManyExtensions
      def find_or_initialize_by_locale(locale)
        with_locale(locale.to_s).first || build(:locale => locale.to_s)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
globalize3-0.0.10 lib/globalize/active_record/act_macro.rb
globalize3-0.0.9 lib/globalize/active_record/act_macro.rb
globalize3-0.0.7 lib/globalize/active_record/act_macro.rb
globalize3-0.0.5 lib/globalize/active_record/act_macro.rb