Module: Spree::Core::Permalinks::InstanceMethods

Defined in:
lib/spree/core/permalinks.rb

Instance Method Summary collapse

Instance Method Details



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/spree/core/permalinks.rb', line 39

def save_permalink
  permalink_value = self.to_param
  field = self.class.permalink_field
  # Do other links exist with this permalink?
  other = self.class.first(
    :conditions => "#{field} LIKE '#{permalink_value}%'",
    :order => "LENGTH(#{field}) DESC, #{field} DESC"
  )
  if other
    # Find the number of that permalink and add one.
    if /-(\d+)$/.match(other.send(field))
      number = $1.to_i + 1
    # Otherwise default to suffixing it with a 1.
    else
      number = 1
    end

    permalink_value += "-#{number.to_s}"
  end
  write_attribute(field, permalink_value)
end