Ruby on Rails
0
Вклад в тег
class Wow::Base < ActiveRecord::Base
self.abstract_class = true
class << self
def [](database_id)
raise "DB name was not set! Use set_db_name to set it in yor model!" unless db_name
self.abstract_class = true
class_name = "#{database_id.to_s.classify}_#{self.name}"
return Object.const_get(class_name) if Object.const_defined?(class_name)
db_info = connection_info(database_id)
raise ArgumentError, "There is no database with such ID!" unless db_info
klass = Object.const_set class_name, Class.new(self)
klass.establish_connection(db_info)
klass
end
def set_db_name name; @@db_name = name; end
def db_name; @@db_name; end
private
def connection_info database_id
# эту не поясняю т.к. длинно, должна возвращать то, что принимает establish_connection
WOW_CONFIG[database_id][:databases][db_name]
end
end
end
class Wow::CharactersDb < Wow::Base
self.abstract_class = true
set_db_name :characters
end
class Character < Wow::CharactersDb
set_primary_key :guid
belongs_to :account, :foreign_key => :account
scope :online, where(:online => 1)
scope :offline, where(:online => 0)
scope :alliance, where(:race => [1,3,4,7,11])
scope :horde, where(:race => [2,5,6,8,10])
end