Module: Spree::Core::Permalinks
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/spree/core/permalinks.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
Instance Method Details
#generate_permalink ⇒ Object
46 47 48 |
# File 'lib/spree/core/permalinks.rb', line 46 def generate_permalink "#{self.class.permalink_prefix}#{Array.new(self.class.permalink_length){rand(9)}.join}" end |
#save_permalink(permalink_value = self.to_param) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/spree/core/permalinks.rb', line 50 def save_permalink(permalink_value=self.to_param) self.with_lock do permalink_value ||= generate_permalink field = self.class.permalink_field # Do other links exist with this permalink? other = self.class.where("#{self.class.table_name}.#{field} LIKE ?", "#{permalink_value}%") if other.any? # Find the existing permalink with the highest number, and increment that number. # (If none of the existing permalinks have a number, this will evaluate to 1.) number = other.map { |o| o.send(field)[/-(\d+)$/, 1].to_i }.max + 1 permalink_value += "-#{number.to_s}" end write_attribute(field, permalink_value) end end |