mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
PermalinkGenerator for products
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
require 'open_food_network/locking'
|
||||
require 'open_food_network/permalink_generator'
|
||||
|
||||
class EnterpriseGroup < ActiveRecord::Base
|
||||
include PermalinkGenerator
|
||||
acts_as_list
|
||||
|
||||
has_and_belongs_to_many :enterprises
|
||||
@@ -81,25 +83,10 @@ class EnterpriseGroup < ActiveRecord::Base
|
||||
|
||||
private
|
||||
|
||||
def self.find_available_value(existing, requested)
|
||||
return requested unless existing.include?(requested)
|
||||
used_indices = existing.map do |p|
|
||||
p.slice!(/^#{requested}/)
|
||||
p.match(/^\d+$/).to_s.to_i
|
||||
end
|
||||
options = (1..used_indices.length + 1).to_a - used_indices
|
||||
requested + options.first.to_s
|
||||
end
|
||||
|
||||
def find_available_permalink(requested)
|
||||
existing = self.class.where(id: !id).where("permalink LIKE ?", "#{requested}%").pluck(:permalink)
|
||||
self.class.find_available_value(existing, requested)
|
||||
end
|
||||
|
||||
def sanitize_permalink
|
||||
if permalink.blank? || permalink_changed?
|
||||
requested = permalink.presence || permalink_was.presence || name.presence || 'group'
|
||||
self.permalink = find_available_permalink(requested.parameterize)
|
||||
self.permalink = create_unique_permalink(requested.parameterize)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
require 'open_food_network/permalink_generator'
|
||||
|
||||
Spree::Product.class_eval do
|
||||
include PermalinkGenerator
|
||||
# We have an after_destroy callback on Spree::ProductOptionType. However, if we
|
||||
# don't specify dependent => destroy on this association, it is not called. See:
|
||||
# https://github.com/rails/rails/issues/7618
|
||||
@@ -19,6 +22,8 @@ Spree::Product.class_eval do
|
||||
attr_accessible :variant_unit, :variant_unit_scale, :variant_unit_name, :unit_value
|
||||
attr_accessible :inherits_properties, :sku
|
||||
|
||||
before_validation :sanitize_permalink
|
||||
|
||||
# validates_presence_of :variants, unless: :new_record?, message: "Product must have at least one variant"
|
||||
validates_presence_of :supplier
|
||||
validates :primary_taxon, presence: { message: "^Product Category can't be blank" }
|
||||
@@ -239,4 +244,11 @@ Spree::Product.class_eval do
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
def sanitize_permalink
|
||||
if permalink.blank? || permalink_changed?
|
||||
requested = permalink.presence || permalink_was.presence || name.presence || 'product'
|
||||
self.permalink = create_unique_permalink(requested.parameterize)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
22
lib/open_food_network/permalink_generator.rb
Normal file
22
lib/open_food_network/permalink_generator.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
module PermalinkGenerator
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def find_available_value(existing, requested)
|
||||
return requested unless existing.include?(requested)
|
||||
used_indices = existing.map do |p|
|
||||
p.slice!(/^#{requested}/)
|
||||
p.match(/^\d+$/).to_s.to_i
|
||||
end
|
||||
options = (1..used_indices.length + 1).to_a - used_indices
|
||||
requested + options.first.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def create_unique_permalink(requested)
|
||||
existing = self.class.where('id != ?', id).where("permalink LIKE ?", "#{requested}%").pluck(:permalink)
|
||||
self.class.find_available_value(existing, requested)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user