Module: Spree::Core::Permalinks

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/spree/core/permalinks.rb

Instance Method Summary collapse

Instance Method Details



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


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spree/core/permalinks.rb', line 50

def save_permalink(permalink_value = to_param)
  permalink_value ||= generate_permalink
  permalink_field = self.class.permalink_field

  loop do
    other = self.class.where(permalink_field => permalink_value)
    break unless other.exists?

    # Try again with a new value
    permalink_value = generate_permalink
  end
  write_attribute(permalink_field, permalink_value)
end