Module: Mongoid::Globalize::Methods
- Included in:
- Mongoid::Globalize
- Defined in:
- lib/mongoid_globalize/methods.rb
Instance Method Summary collapse
-
#attribute_names ⇒ Object
Mongoid documents haven’t attribute_names method, so I replace
super
with @attributes.keys.sort. -
#attributes ⇒ Object
The most trouble method of Mongoid::Globalize :-( Extends reader for @attributes. Mixes translated attributes to Mongoid Return: Hash.
-
#clone ⇒ Object
Extends Mongoid::Document’s method
clone
. -
#globalize ⇒ Object
Reader for adapter, where translations stashing during lifecicle.
-
#process(attributes, *args) ⇒ Object
Extends Mongoid::Document’s method
process
. -
#read_attribute(name, options = {}) ⇒ Object
Extends Mongoid::Document’s method
read_attribute
. -
#reload ⇒ Object
Extends Mongoid::Document’s method
reload
. -
#remove_attribute(name) ⇒ Object
TODO.
-
#set_translations(options) ⇒ Object
Updates fields separately for each given locale post.set_translations( :en => { :title => “updated title” }, :de => { :content => “geänderter Inhalt” } ) Param: Hash, where keys are locales and values are Hashes of name-value pairs for fields.
-
#translated?(name) ⇒ Boolean
Checks whether field with given name is translated field.
-
#translated_attributes ⇒ Object
Returns translations for current locale.
-
#translation ⇒ Object
Returns instance of Translation for current locale.
-
#translation_for(locale) ⇒ Object
Returns instance of Translation for given locale.
-
#untranslated_attributes ⇒ Object
TODO:.
-
#write_attribute(name, value, options = {}) ⇒ Object
Extends Mongoid::Document’s method
write_attribute
.
Instance Method Details
#attribute_names ⇒ Object
Mongoid documents haven’t attribute_names method, so I replace super
with @attributes.keys.sort. So this method returns only translated and existing attribute names (but not all available names as in AR or G3)
76 77 78 |
# File 'lib/mongoid_globalize/methods.rb', line 76 def attribute_names translated_attribute_names.map(&:to_s) + @attributes.keys.sort end |
#attributes ⇒ Object
The most trouble method of Mongoid::Globalize :-( Extends reader for @attributes. Mixes translated attributes to Mongoid Return: Hash
16 17 18 19 20 21 |
# File 'lib/mongoid_globalize/methods.rb', line 16 def attributes unless @stop_merging_translated_attributes || @attributes.frozen? @attributes.merge! translated_attributes end super end |
#clone ⇒ Object
Extends Mongoid::Document’s method clone
. Adds to cloned object all translations from original object.
129 130 131 132 133 134 135 136 137 |
# File 'lib/mongoid_globalize/methods.rb', line 129 def clone obj = super return obj unless respond_to?(:translated_attribute_names) obj.instance_variable_set(:@globalize, nil ) each_locale_and_translated_attribute do |locale, name| obj.globalize.write(locale, name, globalize.fetch(locale, name) ) end return obj end |
#globalize ⇒ Object
Reader for adapter, where translations stashing during lifecicle. At first use creates new one. Return: Mongoid::Globalize::Adapter
8 9 10 |
# File 'lib/mongoid_globalize/methods.rb', line 8 def globalize @globalize ||= Adapter.new(self) end |
#process(attributes, *args) ⇒ Object
Extends Mongoid::Document’s method process
. Pocesses given attributes in consideration of possible :locale key. Used by Mongoid for all attribute- related operations, such as create
, update
etc. Param: Hash of attributes Other params will be transmitted into Mongoid::Document’s method process
as is.
29 30 31 |
# File 'lib/mongoid_globalize/methods.rb', line 29 def process(attributes, *args) with_given_locale(attributes) { super } end |
#read_attribute(name, options = {}) ⇒ Object
Extends Mongoid::Document’s method read_attribute
. If writed attribute is translateble, it is readed from adapter’s stash. Param: String or Symbol - name of attribute Param: Hash of options Return: Object - value of attribute
59 60 61 62 63 64 65 66 |
# File 'lib/mongoid_globalize/methods.rb', line 59 def read_attribute(name, = {}) = {:translated => true, :locale => nil}.merge() if translated?(name) and [:translated] globalize.fetch([:locale] || Mongoid::Globalize.locale, name) else super(name) end end |
#reload ⇒ Object
Extends Mongoid::Document’s method reload
. Resets all translation changes.
121 122 123 124 125 |
# File 'lib/mongoid_globalize/methods.rb', line 121 def reload translated_attribute_names.each { |name| @attributes.delete(name.to_s) } globalize.reset super end |
#remove_attribute(name) ⇒ Object
TODO
69 70 71 |
# File 'lib/mongoid_globalize/methods.rb', line 69 def remove_attribute(name) super name end |
#set_translations(options) ⇒ Object
Updates fields separately for each given locale
post.set_translations(
:en => { :title => "updated title" },
:de => { :content => "geänderter Inhalt" }
)
Param: Hash, where keys are locales and values are Hashes of name-value pairs for fields.
112 113 114 115 116 117 |
# File 'lib/mongoid_globalize/methods.rb', line 112 def set_translations() .keys.each do |locale| translation = translation_for(locale) || translations.build(:locale => locale.to_s) translation.update_attributes!([locale]) end end |
#translated?(name) ⇒ Boolean
Checks whether field with given name is translated field. Param String or Symbol Returns true or false
83 84 85 |
# File 'lib/mongoid_globalize/methods.rb', line 83 def translated?(name) self.class.translated?(name) end |
#translated_attributes ⇒ Object
Returns translations for current locale. Is used for initial mixing into Return Hash
90 91 92 93 94 |
# File 'lib/mongoid_globalize/methods.rb', line 90 def translated_attributes @translated_attributes ||= translated_attribute_names.inject({}) do |attrs, name| attrs.merge(name.to_s => translation.send(name)) end end |
#translation ⇒ Object
Returns instance of Translation for current locale.
140 141 142 |
# File 'lib/mongoid_globalize/methods.rb', line 140 def translation translation_for(Mongoid::Globalize.locale) end |
#translation_for(locale) ⇒ Object
Returns instance of Translation for given locale. Param String or Symbol
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mongoid_globalize/methods.rb', line 146 def translation_for(locale) @translation_caches ||= {} # Need to temporary switch of merging, because #translations uses # #attributes method too, to avoid stack level too deep error. @stop_merging_translated_attributes = true unless @translation_caches[locale] _translation = translations.find_by_locale(locale) _translation ||= translations.build(:locale => locale) @translation_caches[locale] = _translation end @stop_merging_translated_attributes = false @translation_caches[locale] end |
#untranslated_attributes ⇒ Object
TODO:
97 98 99 100 101 102 103 |
# File 'lib/mongoid_globalize/methods.rb', line 97 def untranslated_attributes attrs = {} attribute_names.each do |name| attrs[name] = read_attribute(name, {:translated => false}) end attrs end |
#write_attribute(name, value, options = {}) ⇒ Object
Extends Mongoid::Document’s method write_attribute
. If writed attribute is translateble, it is placed into adapter’s stash. Param: String or Symbol - name of attribute Param: Object - value of attribute Param: Hash of options
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mongoid_globalize/methods.rb', line 38 def write_attribute(name, value, = {}) if translated?(name) = {:locale => nil}.merge() access = name.to_s unless attributes[access] == value || attribute_changed?(access) attribute_will_change! access end @translated_attributes[access] = value the_locale = [:locale] || Mongoid::Globalize.locale self.translations.reject!{ |t| t.new_record? && t.locale != the_locale } if self.class.fallbacks_for_empty_translations globalize.write(the_locale, name, value) else super(name, value) end end |