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



44
45
46
# File 'lib/spree/core/permalinks.rb', line 44

def generate_permalink
  "#{self.class.permalink_prefix}#{Array.new(self.class.permalink_length){ rand(9) }.join}"
end


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

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