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



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/spree/core/permalinks.rb', line 46

def save_permalink
  permalink_value = self.to_param
  field = self.class.permalink_field
    # Do other links exist with this permalink?
    other = self.class.all(:conditions => "#{field} LIKE '#{permalink_value}%'")
    unless other.empty?
      # 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