diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 4adfb05309..39dd73a968 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -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 diff --git a/db/migrate/20150612045544_make_enterprises_name_unique.rb b/db/migrate/20150612045544_make_enterprises_name_unique.rb new file mode 100644 index 0000000000..5722284d94 --- /dev/null +++ b/db/migrate/20150612045544_make_enterprises_name_unique.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index dd00ce3f4e..055f828e9d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 388d80258e..4fb6ac621a 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -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: "")