Adding unique permalink to enterprises

This commit is contained in:
Rob Harrington
2014-12-19 16:07:51 +11:00
parent 41e42c78c4
commit 9f0aeb5adf
5 changed files with 30 additions and 3 deletions

View File

@@ -56,6 +56,7 @@ class Enterprise < ActiveRecord::Base
validates :address, presence: true, associated: true
validates :email, presence: true
validates_presence_of :owner
validates :permalink, uniqueness: true, presence: true
validate :shopfront_taxons
validate :enforce_ownership_limit, if: lambda { owner_id_changed? && !owner_id.nil? }
validates_length_of :description, :maximum => 255
@@ -332,7 +333,7 @@ class Enterprise < ActiveRecord::Base
end
def geocode_address
address.geocode if address.changed?
address.geocode if address.andand.changed?
end
def ensure_owner_is_manager

View File

@@ -0,0 +1,22 @@
class AddPermalinkToEnterprises < ActiveRecord::Migration
def up
add_column :enterprises, :permalink, :string
Enterprise.all.each do |enterprise|
counter = 1
permalink = enterprise.name.parameterize
while Enterprise.find_by_permalink(permalink) do
permalink = enterprise.name.parameterize + counter.to_s
counter += 1
end
enterprise.update_attributes!(permalink: permalink)
end
change_column :enterprises, :permalink, :string, null: false
end
def down
add_column :enterprises, :permalink
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 => 20141210233407) do
ActiveRecord::Schema.define(:version => 20141219034321) do
create_table "adjustment_metadata", :force => true do |t|
t.integer "adjustment_id"
@@ -271,6 +271,7 @@ ActiveRecord::Schema.define(:version => 20141210233407) do
t.string "unconfirmed_email"
t.datetime "shop_trial_start_date"
t.boolean "producer_profile_only", :default => false
t.string "permalink", :null => false
end
add_index "enterprises", ["address_id"], :name => "index_enterprises_on_address_id"

View File

@@ -97,6 +97,7 @@ 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>'

View File

@@ -165,9 +165,11 @@ describe Enterprise do
end
describe "validations" do
subject { FactoryGirl.create(:distributor_enterprise, :address => FactoryGirl.create(:address)) }
subject { FactoryGirl.create(:distributor_enterprise) }
it { should validate_presence_of(:name) }
it { should validate_presence_of(:email) }
it { should validate_presence_of(:permalink) }
it { should validate_uniqueness_of(:permalink) }
it { should ensure_length_of(:description).is_at_most(255) }
it "requires an owner" do