Add permalink field to enterprise groups

This commit is contained in:
Maikel Linke
2015-05-27 11:54:13 +10:00
parent 7f43dbf9bb
commit 19448a182e
4 changed files with 32 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ class EnterpriseGroup < ActiveRecord::Base
validates :name, presence: true
validates :description, presence: true
validates :permalink, uniqueness: true, presence: true
attr_accessible :name, :description, :long_description, :on_front_page, :enterprise_ids
attr_accessible :owner_id

View File

@@ -0,0 +1,26 @@
class AddPermalinkToGroups < ActiveRecord::Migration
def up
add_column :enterprise_groups, :permalink, :string
EnterpriseGroup.reset_column_information
EnterpriseGroup.all.each do |group|
counter = 1
permalink = group.name.parameterize
permalink = "my-group-name" if permalink == ""
while EnterpriseGroup.find_by_permalink(permalink) do
permalink = group.name.parameterize + counter.to_s
counter += 1
end
group.update_column :permalink, permalink
end
change_column :enterprise_groups, :permalink, :string, null: false
add_index :enterprise_groups, :permalink, :unique => true
end
def down
remove_column :enterprise_groups, :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 => 20150424151117) do
ActiveRecord::Schema.define(:version => 20150527004427) do
create_table "adjustment_metadata", :force => true do |t|
t.integer "adjustment_id"
@@ -236,10 +236,12 @@ ActiveRecord::Schema.define(:version => 20150424151117) do
t.string "linkedin", :default => "", :null => false
t.string "twitter", :default => "", :null => false
t.integer "owner_id"
t.string "permalink", :null => false
end
add_index "enterprise_groups", ["address_id"], :name => "index_enterprise_groups_on_address_id"
add_index "enterprise_groups", ["owner_id"], :name => "index_enterprise_groups_on_owner_id"
add_index "enterprise_groups", ["permalink"], :name => "index_enterprise_groups_on_permalink", :unique => true
create_table "enterprise_groups_enterprises", :id => false, :force => true do |t|
t.integer "enterprise_group_id"
@@ -619,9 +621,9 @@ ActiveRecord::Schema.define(:version => 20150424151117) do
t.string "email"
t.text "special_instructions"
t.integer "distributor_id"
t.integer "order_cycle_id"
t.string "currency"
t.string "last_ip_address"
t.integer "order_cycle_id"
t.integer "cart_id"
end

View File

@@ -135,6 +135,7 @@ FactoryGirl.define do
factory :enterprise_group, :class => EnterpriseGroup do
name 'Enterprise group'
sequence(:permalink) { |n| "group#{n}" }
description 'this is a group'
on_front_page false
address { FactoryGirl.build(:address) }