Module: Mongoid::LocalizedSlug
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid/localized_slug.rb,
lib/mongoid/localized_slug/version.rb
Overview
The Slug module helps you generate a URL slug or permalink based on one or more fields in a Mongoid model.
Defined Under Namespace
Modules: ClassMethods, Criterion
Constant Summary collapse
- VERSION =
'0.1.7'
Instance Method Summary collapse
-
#build_slug ⇒ true
Builds a new slug.
-
#find_unique_slug_for(desired_slug) ⇒ String
Finds a unique slug, were specified string used to generate a slug.
-
#slug_should_be_rebuilt? ⇒ Boolean
Whether the slug requires to be rebuilt.
- #slugged_attribute_changed? ⇒ Boolean
-
#to_param ⇒ String
to this record.
Instance Method Details
#build_slug ⇒ true
Builds a new slug.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mongoid/localized_slug.rb', line 157 def build_slug if self.respond_to?("#{slugged_attribute}_translations") translations = self.send("#{slugged_attribute}_translations") else translations = { I18n.locale.to_s => self.send(slugged_attribute) } end slugs = [] trans_hash = {} translations.each do |lang, text| trans_hash[lang] = text.present? ? find_unique_slug_for(text) : nil slugs << trans_hash[lang] end self.send("#{slug_name}_translations=", trans_hash) self.send("#{slug_name}=", slugs.uniq) true end |
#find_unique_slug_for(desired_slug) ⇒ String
Finds a unique slug, were specified string used to generate a slug.
Returned slug will the same as the specified string when there are no duplicates.
181 182 183 |
# File 'lib/mongoid/localized_slug.rb', line 181 def find_unique_slug_for(desired_slug) self.class.find_unique_slug_for desired_slug, :model => self end |
#slug_should_be_rebuilt? ⇒ Boolean
Returns Whether the slug requires to be rebuilt.
186 187 188 |
# File 'lib/mongoid/localized_slug.rb', line 186 def slug_should_be_rebuilt? new_record? or slug_changed? or slugged_attribute_changed? end |
#slugged_attribute_changed? ⇒ Boolean
190 191 192 |
# File 'lib/mongoid/localized_slug.rb', line 190 def slugged_attribute_changed? attribute_changed? slugged_attribute end |
#to_param ⇒ String
to this record.
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/mongoid/localized_slug.rb', line 196 def to_param if slug.empty? build_slug save end locale = I18n.locale.to_s slug_translations.each do |lang, localized_slug| return localized_slug || super if lang == locale end (slug.is_a?(Array) ? slug.first : slug) || super end |