From 4f1c1931789daf812a484a38d94cea809ae803d8 Mon Sep 17 00:00:00 2001 From: Jeremy Rice Date: Wed, 25 May 2011 11:55:27 -0600 Subject: [PATCH] WEB-2264 fixed. Migration changes, mostly. --- app/models/community.rb | 9 ++++++++- app/models/privilege.rb | 9 ++++++++- app/models/role.rb | 9 ++++++++- ...0101213002426_create_starting_community_privileges.rb | 2 +- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/models/community.rb b/app/models/community.rb index c49812576..d9e37a62b 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -40,7 +40,14 @@ def self.create_special special_roles.keys.each do |key| role = Role.find(:first, :conditions => ['title = ? and community_id = ?', key, special.id]) role ||= Role.create(:community_id => special.id, :title => key) - role.privileges = Privilege.find(:all, :conditions => ["level <= ? and special = ?", special_roles[key], true]) + # TODO - we really should change Language#english to Language#default and have it set in a config file. + # TODO - this is stupid, but migrations break because Language (the model) doesn't point to the right DB when + # this runs, so it is undefined after recreating the DB (in which case we hardly need it anyway): + begin + role.privileges = Privilege.find(:all, :conditions => ["level <= ? and special = ?", special_roles[key], true]) + rescue ActiveRecord::StatementInvalid => e + # Do nothing; this should only happen when developers recreate the databases and thus it hardly matters now. + end end end diff --git a/app/models/privilege.rb b/app/models/privilege.rb index 151cdf6be..9c1eb55e7 100644 --- a/app/models/privilege.rb +++ b/app/models/privilege.rb @@ -49,7 +49,14 @@ def self.create_all_for_type(hash, special = false) hash.keys.each do |key| priv = Privilege.create(:level => hash[key], :special => special) # TODO - we really should change Language#english to Language#default and have it set in a config file. - TranslatedPrivilege.create(:name => key, :privilege_id => priv.id, :language_id => Language.english.id) + # TODO - this is stupid, but migrations break because Language (the model) doesn't point to the right DB when + # this runs, so it is undefined after recreating the DB (in which case we hardly need it anyway): + begin + TranslatedPrivilege.create(:name => key, :privilege_id => priv.id, :language_id => Language.english.id) + rescue ActiveRecord::StatementInvalid => e + priv.name = key + priv.save! + end end end diff --git a/app/models/role.rb b/app/models/role.rb index 9160bad70..8ffc31ad6 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -38,7 +38,14 @@ def self.add_defaults_to_community(community) default_roles.keys.each do |key| unless self.exists?(['title = ? and community_id = ?', key, community.id]) new_roles << self.create(:community_id => community.id, :title => key) - new_roles.last.privileges = Privilege.find(:all, :conditions => ["special != 1 and level <= ?", default_roles[key]]) + # TODO - we really should change Language#english to Language#default and have it set in a config file. + # TODO - this is stupid, but migrations break because Language (the model) doesn't point to the right DB when + # this runs, so it is undefined after recreating the DB (in which case we hardly need it anyway): + begin + new_roles.last.privileges = Privilege.find(:all, :conditions => ["special != 1 and level <= ?", default_roles[key]]) + rescue ActiveRecord::StatementInvalid => e + # Do nothing; this only happens when devs recreate databases and hardly matters, then. + end new_roles.last.save! end end diff --git a/db/migrate/20101213002426_create_starting_community_privileges.rb b/db/migrate/20101213002426_create_starting_community_privileges.rb index 5df4caca9..490a94f29 100644 --- a/db/migrate/20101213002426_create_starting_community_privileges.rb +++ b/db/migrate/20101213002426_create_starting_community_privileges.rb @@ -1,7 +1,7 @@ class CreateStartingCommunityPrivileges < ActiveRecord::Migration def self.up - Privilege.create_all + Privilege.create_defaults end def self.down