Seed categories in transactions

This commit is contained in:
Neil Lalonde 2014-03-19 11:59:53 -04:00
parent 51e3d72461
commit 7780013c3a
3 changed files with 78 additions and 72 deletions

View File

@ -5,36 +5,38 @@ unless Rails.env.test?
# The category for users with trust level 3 has been created.
# Add permissions and a description to it.
lounge.group_names = ['trust_level_3']
unless lounge.save
puts lounge.errors.full_messages
raise "Failed to set permissions on trust level 3 lounge category!"
end
if lounge.topic_id.nil?
creator = PostCreator.new(Discourse.system_user,
raw: I18n.t('vip_category_description'),
title: I18n.t('category.topic_prefix', category: lounge.name),
category: lounge.name,
archetype: Archetype.default,
skip_validations: true
)
post = creator.create
unless post && post.id
puts post.errors.full_messages if post
puts creator.errors.inspect
raise "Failed to create description for trust level 3 lounge!"
end
lounge.topic_id = post.topic.id
Category.transaction do
lounge.group_names = ['trust_level_3']
unless lounge.save
puts lounge.errors.full_messages
puts "Failed to set the lounge description topic!"
raise "Failed to set permissions on trust level 3 lounge category!"
end
# Reset topic count because we don't count the description topic
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{lounge.id}"
if lounge.topic_id.nil?
creator = PostCreator.new(Discourse.system_user,
raw: I18n.t('vip_category_description'),
title: I18n.t('category.topic_prefix', category: lounge.name),
category: lounge.name,
archetype: Archetype.default,
skip_validations: true
)
post = creator.create
unless post && post.id
puts post.errors.full_messages if post
puts creator.errors.inspect
raise "Failed to create description for trust level 3 lounge!"
end
lounge.topic_id = post.topic.id
unless lounge.save
puts lounge.errors.full_messages
puts "Failed to set the lounge description topic!"
end
# Reset topic count because we don't count the description topic
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{lounge.id}"
end
end
end
end

View File

@ -2,28 +2,30 @@ unless Rails.env.test?
meta = Category.where(id: SiteSetting.meta_category_id).first
if meta && !meta.topic_id
creator = PostCreator.new(Discourse.system_user,
raw: I18n.t('meta_category_description'),
title: I18n.t('category.topic_prefix', category: meta.name),
category: meta.name,
archetype: Archetype.default
)
post = creator.create
Category.transaction do
creator = PostCreator.new(Discourse.system_user,
raw: I18n.t('meta_category_description'),
title: I18n.t('category.topic_prefix', category: meta.name),
category: meta.name,
archetype: Archetype.default
)
post = creator.create
unless post && post.id
puts post.errors.full_messages if post
puts creator.errors.inspect
raise "Failed meta topic"
unless post && post.id
puts post.errors.full_messages if post
puts creator.errors.inspect
raise "Failed meta topic"
end
meta.set_permissions(:everyone => :full)
meta.topic_id = post.topic.id
unless meta.save
puts meta.errors.full_messages
puts "Failed to set the meta description and permission!"
end
# Reset topic count because we don't count the description topic
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{meta.id}"
end
meta.set_permissions(:everyone => :full)
meta.topic_id = post.topic.id
unless meta.save
puts meta.errors.full_messages
puts "Failed to set the meta description and permission!"
end
# Reset topic count because we don't count the description topic
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{meta.id}"
end
end

View File

@ -4,35 +4,37 @@ unless Rails.env.test?
# Add permissions and a description to the Staff category.
staff.group_names = ['staff']
unless staff.save
puts staff.errors.full_messages
raise "Failed to set permissions on the Staff category!"
end
if staff.topic_id.nil?
creator = PostCreator.new(Discourse.system_user,
raw: I18n.t('staff_category_description'),
title: I18n.t('category.topic_prefix', category: staff.name),
category: staff.name,
archetype: Archetype.default
)
post = creator.create
unless post && post.id
puts post.errors.full_messages if post
puts creator.errors.inspect
raise "Failed to create description for Staff category!"
end
staff.topic_id = post.topic.id
Category.transaction do
staff.group_names = ['staff']
unless staff.save
puts staff.errors.full_messages
puts "Failed to set the Staff category description topic!"
raise "Failed to set permissions on the Staff category!"
end
# Reset topic count because we don't count the description topic
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{staff.id}"
if staff.topic_id.nil?
creator = PostCreator.new(Discourse.system_user,
raw: I18n.t('staff_category_description'),
title: I18n.t('category.topic_prefix', category: staff.name),
category: staff.name,
archetype: Archetype.default
)
post = creator.create
unless post && post.id
puts post.errors.full_messages if post
puts creator.errors.inspect
raise "Failed to create description for Staff category!"
end
staff.topic_id = post.topic.id
unless staff.save
puts staff.errors.full_messages
puts "Failed to set the Staff category description topic!"
end
# Reset topic count because we don't count the description topic
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{staff.id}"
end
end
end
end