diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 929fcd1a7e..9c516666a7 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -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 diff --git a/db/migrate/20141219034321_add_permalink_to_enterprises.rb b/db/migrate/20141219034321_add_permalink_to_enterprises.rb new file mode 100644 index 0000000000..d233a18d5d --- /dev/null +++ b/db/migrate/20141219034321_add_permalink_to_enterprises.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 3244916b3b..2d8f28fbdd 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 => 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" diff --git a/spec/factories.rb b/spec/factories.rb index 214c61962d..33d760ec15 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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 '

Hello, world!

This is a paragraph.

' diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 37535a2c3c..9edc1d2cee 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -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