Make enterprise name unique

This commit is contained in:
Rohan Mitchell
2015-06-17 15:01:46 +10:00
parent bf953f8987
commit c5526c78d9
4 changed files with 57 additions and 1 deletions

View File

@@ -56,6 +56,7 @@ class Enterprise < ActiveRecord::Base
validates :name, presence: true
validate :name_is_unique
validates :sells, presence: true, inclusion: {in: SELLS}
validates :address, presence: true, associated: true
validates :email, presence: true
@@ -333,6 +334,15 @@ class Enterprise < ActiveRecord::Base
private
def name_is_unique
dups = Enterprise.where(name: name)
dups = dups.where('id != ?', id) unless new_record?
if dups.any?
errors.add :name, "has already been taken. If this is your enterprise and you would like to claim ownership, please contact the current manager of this profile at #{dups.first.owner.email}."
end
end
def email_is_known?
owner.enterprises.confirmed.map(&:email).include?(email)
end

View File

@@ -0,0 +1,20 @@
class MakeEnterprisesNameUnique < ActiveRecord::Migration
def up
dup_names = Enterprise.group('name').select('name, COUNT(*) AS num_enterprises')
dup_names.each do |data|
(data.num_enterprises.to_i - 1).times do |i|
e = Enterprise.find_by_name data.name
new_name = "#{data.name}-#{i+1}"
e.update_column :name, new_name
say "Renamed enterprise #{data.name} to #{new_name}"
end
end
add_index :enterprises, :name, unique: true
end
def down
remove_index :enterprises, :name
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20150605052516) do
ActiveRecord::Schema.define(:version => 20150612045544) do
create_table "adjustment_metadata", :force => true do |t|
t.integer "adjustment_id"
@@ -322,6 +322,7 @@ ActiveRecord::Schema.define(:version => 20150605052516) do
add_index "enterprises", ["address_id"], :name => "index_enterprises_on_address_id"
add_index "enterprises", ["confirmation_token"], :name => "index_enterprises_on_confirmation_token", :unique => true
add_index "enterprises", ["is_primary_producer", "sells"], :name => "index_enterprises_on_is_primary_producer_and_sells"
add_index "enterprises", ["name"], :name => "index_enterprises_on_name", :unique => true
add_index "enterprises", ["owner_id"], :name => "index_enterprises_on_owner_id"
add_index "enterprises", ["permalink"], :name => "index_enterprises_on_permalink", :unique => true
add_index "enterprises", ["sells"], :name => "index_enterprises_on_sells"

View File

@@ -179,6 +179,31 @@ describe Enterprise do
}.to raise_error ActiveRecord::RecordInvalid, "Validation failed: Owner can't be blank"
end
describe "name uniqueness" do
let(:owner) { create(:user, email: 'owner@example.com') }
let!(:enterprise) { create(:enterprise, name: 'Enterprise', owner: owner) }
it "prevents duplicate names for new records" do
e = Enterprise.new name: enterprise.name
e.should_not be_valid
e.errors[:name].first.should ==
"has already been taken. If this is your enterprise and you would like to claim ownership, please contact the current manager of this profile at owner@example.com."
end
it "prevents duplicate names for existing records" do
e = create(:enterprise, name: 'foo')
e.name = enterprise.name
e.should_not be_valid
e.errors[:name].first.should ==
"has already been taken. If this is your enterprise and you would like to claim ownership, please contact the current manager of this profile at owner@example.com."
end
it "does not prohibit the saving of an enterprise with no name clash" do
enterprise.email = 'new@email.com'
enterprise.should be_valid
end
end
describe "preferred_shopfront_taxon_order" do
it "empty strings are valid" do
enterprise = build(:enterprise, preferred_shopfront_taxon_order: "")