Sha256: b9ad50368d452e7f87a8eb43e72879aa401d76ce00886209eeaf91844474aadf

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

module SimpleModelTranslations
  
  module Base
    # Configure translation model dependency.
    # Eg:
    # class PostTranslation < ActiveRecord::Base
    #   translation_for :post
    # end
    def translation_for(model)
      belongs_to model
      validates_presence_of :locale
      validates_uniqueness_of :locale, :scope => "#{model}_id"
    end

    # Configure translated attributes.
    # Eg:
    # class Post < ActiveRecord::Base
    #   translates :title, :description
    # end
    def translates(*attributes)
      configure_translations(attributes.extract_options!)

      attributes.each do |attribute|
        # attribute setter
        define_method "#{attribute}=" do |value|
          translation = find_or_build_translation_by_locale(I18n.locale)
          translation.send("#{attribute}=", value)
        end

        # attribute getter
        define_method attribute do
          translation = find_translation_by_locale(current_locale_for_translation) || 
                        find_translation_by_locale(default_locale_for_translation)

          translation ? translation.send(attribute) : nil
        end
      end
    end

    private
      # configure model
      def configure_translations(options)
        raise 'You can call #translates method only once per model!' if included_modules.include?(SimpleModelTranslations::InstanceMethods)
        
        (class << self; self; end).send :define_method, :translation_options do
          options
        end
        
        include SimpleModelTranslations::InstanceMethods
        include SimpleModelTranslations::Attributes
        extend SimpleModelTranslations::ClassMethods
        
        has_many :translations, :class_name => translation_class_name, :dependent => :destroy, :autosave => true
        
        if options[:accepts_nested_attributes]
          accepts_nested_attributes_for :translations, :allow_destroy => true
        end
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_model_translations-0.1.3 lib/simple_model_translations/base.rb
simple_model_translations-0.1.2 lib/simple_model_translations/base.rb