mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Finding unique permalink before validation.
This commit is contained in:
@@ -299,7 +299,7 @@ class Enterprise < ActiveRecord::Base
|
||||
test_permalink = test_permalink.parameterize
|
||||
test_permalink = "my-enterprise" if test_permalink.blank?
|
||||
existing = Enterprise.select(:permalink).order(:permalink).where("permalink LIKE ?", "#{test_permalink}%").map(&:permalink)
|
||||
if existing.empty?
|
||||
unless existing.include?(test_permalink)
|
||||
test_permalink
|
||||
else
|
||||
used_indices = existing.map do |p|
|
||||
|
||||
@@ -15,6 +15,8 @@ class EnterpriseGroup < ActiveRecord::Base
|
||||
|
||||
validates :name, presence: true
|
||||
validates :description, presence: true
|
||||
|
||||
before_validation :sanitize_permalink
|
||||
validates :permalink, uniqueness: true, presence: true
|
||||
|
||||
attr_accessible :name, :description, :long_description, :on_front_page, :enterprise_ids
|
||||
@@ -73,4 +75,27 @@ class EnterpriseGroup < ActiveRecord::Base
|
||||
address.zipcode.sub!(/^undefined$/, '')
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -97,7 +97,6 @@ FactoryGirl.define do
|
||||
factory :enterprise, :class => Enterprise do
|
||||
owner { FactoryGirl.create :user }
|
||||
sequence(:name) { |n| "Enterprise #{n}" }
|
||||
sequence(:permalink) { |n| "enterprise#{n}" }
|
||||
sells 'any'
|
||||
description 'enterprise'
|
||||
long_description '<p>Hello, world!</p><p>This is a paragraph.</p>'
|
||||
|
||||
@@ -2,11 +2,32 @@ require 'spec_helper'
|
||||
|
||||
describe EnterpriseGroup do
|
||||
describe "validations" do
|
||||
it "pass with name, description and address" do
|
||||
e = EnterpriseGroup.new
|
||||
e.name = 'Test Group'
|
||||
e.description = 'A valid test group.'
|
||||
e.address = build(:address)
|
||||
e.should be_valid
|
||||
end
|
||||
|
||||
it "is valid when built from factory" do
|
||||
e = build(:enterprise_group)
|
||||
e.should be_valid
|
||||
end
|
||||
|
||||
it "replace empty permalink and pass" do
|
||||
e = build(:enterprise_group, permalink: '')
|
||||
e.should be_valid
|
||||
e.permalink.should == e.name.parameterize
|
||||
end
|
||||
|
||||
it "restores permalink and pass" do
|
||||
e = create(:enterprise_group, permalink: 'p')
|
||||
e.permalink = ''
|
||||
e.should be_valid
|
||||
e.permalink.should == 'p'
|
||||
end
|
||||
|
||||
it "requires a name" do
|
||||
e = build(:enterprise_group, name: '')
|
||||
e.should_not be_valid
|
||||
@@ -60,5 +81,39 @@ describe EnterpriseGroup do
|
||||
|
||||
EnterpriseGroup.managed_by(user).should == [eg1]
|
||||
end
|
||||
|
||||
describe "finding a permalink" do
|
||||
it "finds available permalink" do
|
||||
existing = []
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink"
|
||||
end
|
||||
|
||||
it "finds available permalink similar to existing" do
|
||||
existing = ["permalink1"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink"
|
||||
end
|
||||
|
||||
it "adds unique number to existing permalinks" do
|
||||
existing = ["permalink"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink1"
|
||||
existing = ["permalink", "permalink1"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "ignores permalinks with characters after the index value" do
|
||||
existing = ["permalink", "permalink1", "permalink2xxx"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "finds gaps in the indices of existing permalinks" do
|
||||
existing = ["permalink", "permalink1", "permalink3"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "finds available indexed permalink" do
|
||||
existing = ["permalink", "permalink1"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink1")).to eq "permalink11"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -845,6 +845,11 @@ describe Enterprise do
|
||||
expect(Enterprise.find_available_permalink("permalink")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "finds available permalink similar to existing" do
|
||||
create(:enterprise, permalink: "permalink2xxx")
|
||||
expect(Enterprise.find_available_permalink("permalink2")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "finds gaps in the indices of existing permalinks" do
|
||||
create(:enterprise, permalink: "permalink3")
|
||||
expect(Enterprise.find_available_permalink("permalink")).to eq "permalink2"
|
||||
|
||||
Reference in New Issue
Block a user