Module: TranslateColumns::InstanceMethods
- Defined in:
- lib/translate_columns.rb
Overview
Methods that are specific to the current class and only called when translate_columns is used
Instance Method Summary collapse
-
#attributes_with_locale=(new_attributes, guard_protected_attributes = true) ⇒ Object
Override the default mass assignment method so that the locale variable is always given preference.
-
#disable_translation ⇒ Object
Do not allow translations!.
- #enable_translation ⇒ Object
-
#has_locale_value? ⇒ Boolean
Important check to see if the parent has a locale method.
-
#save_with_translation(*args) ⇒ Object
As this is included in a mixin, a “super” call from inside the child (inheriting) class will infact look here before looking to ActiveRecord for the real ‘save’.
- #save_with_translation! ⇒ Object
-
#translation ⇒ Object
Provide a translation object based on the parent and the translation_locale current value.
-
#translation_enabled? ⇒ Boolean
determine if the conditions are set for a translation to be used.
-
#translation_locale ⇒ Object
Provide the locale which is currently in use with the object or the current global locale.
-
#translation_locale=(locale) ⇒ Object
Setting the locale will always enable translation.
Instance Method Details
#attributes_with_locale=(new_attributes, guard_protected_attributes = true) ⇒ Object
Override the default mass assignment method so that the locale variable is always given preference.
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/translate_columns.rb', line 216 def attributes_with_locale=(new_attributes, guard_protected_attributes = true) return if new_attributes.nil? attributes = new_attributes.dup attributes.stringify_keys! attributes = sanitize_for_mass_assignment(attributes) if guard_protected_attributes send(:locale=, attributes["locale"]) if attributes.has_key?("locale") and respond_to?(:locale=) send(:attributes_without_locale=, attributes, guard_protected_attributes) end |
#disable_translation ⇒ Object
Do not allow translations!
145 146 147 |
# File 'lib/translate_columns.rb', line 145 def disable_translation @disable_translation = true end |
#enable_translation ⇒ Object
148 149 150 |
# File 'lib/translate_columns.rb', line 148 def enable_translation @disable_translation = false end |
#has_locale_value? ⇒ Boolean
Important check to see if the parent has a locale method. If so, translations should be disabled if it is set to something!
154 155 156 |
# File 'lib/translate_columns.rb', line 154 def has_locale_value? respond_to?(:locale) && !self.locale.to_s.empty? end |
#save_with_translation(*args) ⇒ Object
As this is included in a mixin, a “super” call from inside the child (inheriting) class will infact look here before looking to ActiveRecord for the real ‘save’. This method should therefore be safely overridden if needed.
Assumes validation enabled in ActiveRecord and performs validation before saving. This means the base records validation checks will always be used.
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/translate_columns.rb', line 187 def save_with_translation(*args) perform_validation = args.is_a?(Hash) ? args[:validate] : args if perform_validation && valid? || !perform_validation translation.save(*args) if (translation) disable_translation save_without_translation(*args) enable_translation true else false end end |
#save_with_translation! ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/translate_columns.rb', line 200 def save_with_translation! if valid? translation.save! if (translation) disable_translation save_without_translation! enable_translation else raise ActiveRecord::RecordInvalid.new(self) end rescue enable_translation raise end |
#translation ⇒ Object
Provide a translation object based on the parent and the translation_locale current value.
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/translate_columns.rb', line 165 def translation if translation_enabled? if !@translation || (@translation.locale != translation_locale) raise MissingParent, "Cannot create translations without a stored parent" if new_record? # try to find translation or build a new one @translation = translations.where(:locale => translation_locale).first || translations.build(:locale => translation_locale) end @translation else nil end end |
#translation_enabled? ⇒ Boolean
determine if the conditions are set for a translation to be used
159 160 161 |
# File 'lib/translate_columns.rb', line 159 def translation_enabled? (!@disable_translation && translation_locale) and !has_locale_value? end |
#translation_locale ⇒ Object
Provide the locale which is currently in use with the object or the current global locale. If the default is in use, always return nil.
130 131 132 133 |
# File 'lib/translate_columns.rb', line 130 def translation_locale locale = @translation_locale || I18n.locale.to_s locale == I18n.default_locale.to_s ? nil : locale end |
#translation_locale=(locale) ⇒ Object
Setting the locale will always enable translation. If set to nil the global locale is used.
137 138 139 140 141 142 |
# File 'lib/translate_columns.rb', line 137 def translation_locale=(locale) enable_translation # TODO some checks for available translations would be nice. # I18n.available_locales only available as standard with rails 2.3 @translation_locale = locale.to_s.empty? ? nil : locale.to_s end |