From b4ff3b959d4dd6a7561ac3be063a43619c0bd89c Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 12 Aug 2016 11:34:01 +0200 Subject: move temporary_user into lib - fix load issue We already did the same for other concerns. The way we load models for couchrest migrations does not work well with concerns in the model directory as they will be loaded twice. --- app/models/temporary_user.rb | 93 -------------------------------------------- app/models/user.rb | 1 + 2 files changed, 1 insertion(+), 93 deletions(-) delete mode 100644 app/models/temporary_user.rb (limited to 'app/models') diff --git a/app/models/temporary_user.rb b/app/models/temporary_user.rb deleted file mode 100644 index 2afae15..0000000 --- a/app/models/temporary_user.rb +++ /dev/null @@ -1,93 +0,0 @@ -# -# For users with login '*test_user*', we don't want to store these documents in -# the main users db. This is because we create and destroy a lot of test -# users. This weirdness of using a different db for some users breaks a lot of -# things, such as associations. However, this is OK for now since we don't need -# those for running the frequent nagios tests. -# -# This module is included in user.rb. This will only work if it is included -# after designs are defined, otherwise, the design definition will overwrite -# find_by_login(). -# - -module TemporaryUser - extend ActiveSupport::Concern - include CouchRest::Model::DatabaseMethod - - USER_DB = 'users' - TMP_USER_DB = 'tmp_users' - TMP_LOGIN = 'tmp_user' # created and deleted frequently - TEST_LOGIN = 'test_user' # created, rarely deleted - - included do - use_database_method :db_name - - # since the original find_by_login is dynamically created with - # instance_eval, it appears that we also need to use instance eval to - # override it. - instance_eval <<-EOS, __FILE__, __LINE__ + 1 - def find_by_login(*args) - if args.grep(/^#{TMP_LOGIN}/).any? - by_login.database(tmp_database).key(*args).first() - else - by_login.key(*args).first() - end - end - EOS - end - - module ClassMethods - def get(id, db = database) - super(id, db) || super(id, tmp_database) - end - alias :find :get - - # calls db_name(TMP_LOGIN), then creates a CouchRest::Database - # from the name - def tmp_database - choose_database(TMP_LOGIN) - end - - def db_name(login=nil) - if !login.nil? && login.include?(TMP_LOGIN) - TMP_USER_DB - else - USER_DB - end - end - - # create the tmp db if it doesn't exist. - # requires admin access. - def create_tmp_database! - design_doc.sync!(tmp_database.tap{|db|db.create!}) - end - - def is_tmp?(login) - !login.nil? && login =~ /^#{TMP_LOGIN}/ - end - - def is_test?(login) - !login.nil? && (login =~ /^#{TMP_LOGIN}/ || login =~ /^#{TEST_LOGIN}/) - end - end - - # - # this gets called each and every time a User object needs to - # access the database. - # - def db_name - self.class.db_name(self.login) - end - - # returns true if this User instance is stored in tmp db. - def is_tmp? - self.class.is_tmp?(self.login) - end - - # returns true if this user is used for testing purposes - # (either a temporary or long lived) - def is_test? - self.class.is_test?(self.login) - end - -end diff --git a/app/models/user.rb b/app/models/user.rb index 93830cc..1116e6c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,6 @@ require 'login_format_validation' require 'local_email' +require 'temporary_user' class User < CouchRest::Model::Base include LoginFormatValidation -- cgit v1.2.3 From 8fbe70729da1d308a118c930e8f938837484a61c Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 12 Aug 2016 17:26:51 +0200 Subject: [db] def database on users instead of use_database use_database affects all uses of prepare_database - so also the one in tmp_database. In order to avoid that we do not use_database but just overwrite the database method itself. --- app/models/user.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'app/models') diff --git a/app/models/user.rb b/app/models/user.rb index 1116e6c..9cebbca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,8 +5,6 @@ require 'temporary_user' class User < CouchRest::Model::Base include LoginFormatValidation - use_database :users - property :login, String, :accessible => true property :password_verifier, String, :accessible => true property :password_salt, String, :accessible => true -- cgit v1.2.3