module Mongoid::Globalize::ClassMethods

Public Instance Methods

required_attributes() click to toggle source

Return Array of attribute names with presence validations

# File lib/mongoid_globalize/class_methods.rb, line 47
def required_attributes
  validators.map{ |v| v.attributes if v.is_a?(Mongoid::Validations::PresenceValidator) }.flatten.compact
end
required_fields_criteria() click to toggle source

Returns structures hash of attributes with presence validations for using in with_translations

# File lib/mongoid_globalize/class_methods.rb, line 20
def required_fields_criteria
  required_translated_attributes.inject({}) do |criteria, name|
    criteria.merge name => {"$ne" => nil}
  end
end
required_translated_attributes() click to toggle source

Return Array of translated attribute names with presence validations

# File lib/mongoid_globalize/class_methods.rb, line 52
def required_translated_attributes
  translated_attribute_names & required_attributes
end
translated?(name) click to toggle source

Checks whether field with given name is translated field. Param String or Symbol Returns true or false

# File lib/mongoid_globalize/class_methods.rb, line 42
def translated?(name)
  translated_attribute_names.include?(name.to_sym)
end
translated_attr_accessor(name) click to toggle source

Generates accessor methods for translated attributes

# File lib/mongoid_globalize/class_methods.rb, line 73
def translated_attr_accessor(name)
  define_method(:"#{name}=") do |value|
    write_attribute(name, value)
  end
  define_method(name) do |*args|
    read_attribute(name, {:locale => args.first})
  end
  alias_method :"#{name}_before_type_cast", name
end
translated_locales() click to toggle source

Returns all locales used for translation all of documents of this class. Return Array of Symbols

# File lib/mongoid_globalize/class_methods.rb, line 5
def translated_locales
  all.distinct("translations.locale").sort.map &:to_sym
end
translation_class() click to toggle source

Returns translation class First use creates this class as subclass of document’s class based on Mongoid::Globalize::DocumentTranslation, creates other side for embeded relationship.

# File lib/mongoid_globalize/class_methods.rb, line 60
def translation_class
  @translation_class ||= begin
    klass = self.const_get(:Translation) rescue nil
    if klass.nil?
      klass = self.const_set(:Translation, Class.new(Mongoid::Globalize::DocumentTranslation))
    end
    klass.embedded_in name.underscore.gsub('/', '_')
    klass.translated_klass = self
    klass
  end
end
with_translations(*locales) click to toggle source

Finds documents where translations for given locales are present and where attributes with presence validations aren’t nil Params String or Symbol or Array of Strings or Symbols Returns Mongoid::Criteria

# File lib/mongoid_globalize/class_methods.rb, line 13
def with_translations(*locales)
  locales = translated_locales if locales.empty?
  where :translations.matches => {:locale => {"$in" => locales.flatten}}.merge(required_fields_criteria)
end