Sha256: 707b087427318e3bbf665ca56b103035f4c2c1c03d665f05b919dc4e29314471

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module SimpleModelTranslations
  module ClassMethods
    def with_translations(*args)
      current_scope = joins(:translations)
      current_scope = current_scope.where("#{translated_column_name(:locale)} IN (?)", args) unless args.empty?
      current_scope
    end
    alias with_translation with_translations
    
    def translation_class
      begin
        klass = const_get(translation_class_name.to_sym)
      rescue NameError => e
        klass = const_set(translation_class_name.to_sym, Class.new(ActiveRecord::Base))
        klass.translation_for(self.name.underscore.to_sym)
        klass.set_table_name(translation_class_name.underscore.pluralize)
      end
      klass
    end
    
    def translation_class_name
      class_name = translation_options[:class_name] || "#{self.name}Translation"
    end
    
    def translated_column_name(name)
      "#{translation_class.table_name}.#{name}".to_sym
    end
    
    def with_translated_attribute(name, value, locales = nil)
      with_translations.where(
          translated_column_name(name)    => value,
          translated_column_name(:locale) => Array(locales).map(&:to_s)
        )
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple_model_translations-0.2.7 lib/simple_model_translations/class_methods.rb
simple_model_translations-0.2.6 lib/simple_model_translations/class_methods.rb
simple_model_translations-0.2.5 lib/simple_model_translations/class_methods.rb
simple_model_translations-0.2.4 lib/simple_model_translations/class_methods.rb