Module: Mongoid::Globalize::ClassMethods
- Defined in:
- lib/mongoid_globalize/class_methods.rb
Instance Method Summary collapse
-
#required_attributes ⇒ Object
Return Array of attribute names with presence validations.
-
#required_fields_criteria ⇒ Object
Returns structures hash of attributes with presence validations for using in
with_translations
. -
#required_translated_attributes ⇒ Object
Return Array of translated attribute names with presence validations.
-
#translated?(name) ⇒ Boolean
Checks whether field with given name is translated field.
-
#translated_attr_accessor(name) ⇒ Object
Generates accessor methods for translated attributes.
-
#translated_locales ⇒ Object
Returns all locales used for translation all of documents of this class.
-
#translation_class ⇒ Object
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.
-
#with_translations(*locales) ⇒ Object
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.
Instance Method Details
#required_attributes ⇒ Object
Return Array of attribute names with presence validations
47 48 49 |
# 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 ⇒ Object
Returns structures hash of attributes with presence validations for using in with_translations
20 21 22 23 24 |
# 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 ⇒ Object
Return Array of translated attribute names with presence validations
52 53 54 |
# File 'lib/mongoid_globalize/class_methods.rb', line 52 def required_translated_attributes translated_attribute_names & required_attributes end |
#translated?(name) ⇒ Boolean
Checks whether field with given name is translated field. Param String or Symbol Returns true or false
42 43 44 |
# File 'lib/mongoid_globalize/class_methods.rb', line 42 def translated?(name) translated_attribute_names.include?(name.to_sym) end |
#translated_attr_accessor(name) ⇒ Object
Generates accessor methods for translated attributes
73 74 75 76 77 78 79 80 81 |
# 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 ⇒ Object
Returns all locales used for translation all of documents of this class. Return Array of Symbols
5 6 7 |
# File 'lib/mongoid_globalize/class_methods.rb', line 5 def translated_locales all.distinct("translations.locale").sort.map &:to_sym end |
#translation_class ⇒ Object
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.
60 61 62 63 64 65 66 67 68 69 70 |
# 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. name.underscore.gsub('/', '_') klass.translated_klass = self klass end end |
#with_translations(*locales) ⇒ Object
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
13 14 15 16 |
# 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 |