Module: Gluttonberg::Content::Localization
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/gluttonberg/content/localization.rb
Overview
A mixin which allows for any arbitrary model to be localized. It will generate the localization models and add methods for creating and retrieving localized versions of a record.
Defined Under Namespace
Modules: ClassMethods, ModelLocalization
Instance Method Summary collapse
-
#create_localization(locale) ⇒ Object
create localization for given locale (locale id or object both are acceptable) if it does not exist.
-
#current_localization ⇒ Object
returns current localization if current localization does not exist then init it with default localization.
- #current_localization=(localization) ⇒ Object
-
#load_localization(locale, fallback = true) ⇒ Object
load locaization for given locale (locale id or locale objects both are acceptable) if localization for given locale does not exist then create localization for it and if creation of localization failed then return default localization.
-
#localized? ⇒ Boolean
InstanceMethods.
Instance Method Details
#create_localization(locale) ⇒ Object
create localization for given locale (locale id or object both are acceptable) if it does not exist
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/gluttonberg/content/localization.rb', line 170 def create_localization(locale) locale_id = locale.kind_of?(Gluttonberg::Locale) ? locale.id: locale unless locale.blank? loc = self.class.localized_model.where(:locale_id => locale_id, self.parent_key => self.id).first if loc.blank? tmp_attributes = {} unless self.current_localization.blank? tmp_attributes = self.current_localization.attributes end tmp_attributes[:locale_id] = locale_id loc = self.class.localized_model.new(:locale_id => locale_id) loc.attributes = tmp_attributes if loc.save loc else nil end end end end |
#current_localization ⇒ Object
returns current localization if current localization does not exist then init it with default localization
134 135 136 137 138 139 140 |
# File 'lib/gluttonberg/content/localization.rb', line 134 def current_localization if @current_localization.blank? @current_localization = self.default_localization @current_localization.parent = self unless @current_localization.blank? end @current_localization end |
#current_localization=(localization) ⇒ Object
142 143 144 |
# File 'lib/gluttonberg/content/localization.rb', line 142 def current_localization=(localization) self.instance_variable_set(:@current_localization, localization) end |
#load_localization(locale, fallback = true) ⇒ Object
load locaization for given locale (locale id or locale objects both are acceptable) if localization for given locale does not exist then create localization for it and if creation of localization failed then return default localization
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/gluttonberg/content/localization.rb', line 149 def load_localization(locale, fallback = true) opts = {} opts[:locale_id] = locale.kind_of?(Gluttonberg::Locale) ? locale.id: locale opts[self.parent_key] = self.id # Go and find the localization self.current_localization = nil self.current_localization = self.class.localized_model.where(opts).first if @current_localization.blank? && !locale.blank? self.current_localization = self.create_localization(locale) end # Check to see if we missed the load and if we also need the fallback if self.current_localization.blank? && fallback self.current_localization = self.default_localization end self.current_localization.parent = self unless self.current_localization.blank? self.current_localization end |
#localized? ⇒ Boolean
InstanceMethods
129 130 131 |
# File 'lib/gluttonberg/content/localization.rb', line 129 def localized? self.class.localized? end |