/
wagn_initializer.rb
164 lines (142 loc) · 5.18 KB
/
wagn_initializer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
module Wagn
class Initializer
class << self
def set_default_config config
config.available_modules = Dir["#{RAILS_ROOT}/modules/*.rb"]
end
def set_default_rails_config config
#config.active_record.observers = :card_observer
config.cache_store = :file_store, "#{RAILS_ROOT}/tmp/cache"
config.frameworks -= [ :action_web_service ]
config.gem "uuid"
config.gem "json"
config.gem "htmlentities"
unless ENV['RUN_CODE_RUN']
config.gem "hoptoad_notifier"
end
require 'yaml'
require 'erb'
database_configuration_file = "#{RAILS_ROOT}/config/database.yml"
db = YAML::load(ERB.new(IO.read(database_configuration_file)).result)
config.action_controller.session = {
:session_key => db[RAILS_ENV]['session_key'],
:secret => db[RAILS_ENV]['secret']
}
set_default_config Wagn.config
end
def run
ActionController::Dispatcher.prepare_dispatch do
Wagn::Initializer.load
end
end
def pre_schema?
begin
@@schema_initialized ||= ActiveRecord::Base.connection.select_value("select count(*) from cards").to_i > 2
!@@schema_initialized
rescue
ActiveRecord::Base.logger.info("\n----------- Schema Not Initialized -----------\n\n")
true
end
end
def load
load_config
load_cardlib
setup_multihost
load_cardtypes
return if pre_schema?
load_modules
Wagn::Cache.initialize_on_startup
initialize_builtin_cards
ActiveRecord::Base.logger.info("\n----------- Wagn Initialization Complete -----------\n\n")
end
def load_config
System
# FIXME: this has to be here because System is both a config store and a model-- which means
# in development mode it gets reloaded so we lose the config settings. The whole config situation
# needs an overhaul
if File.exists? "#{RAILS_ROOT}/config/sample_wagn.rb"
require_dependency "#{RAILS_ROOT}/config/sample_wagn.rb"
end
if File.exists? "#{RAILS_ROOT}/config/wagn.rb"
require_dependency "#{RAILS_ROOT}/config/wagn.rb"
end
# Configuration cleanup: Make sure System.base_url doesn't end with a /
System.base_url.gsub!(/\/$/,'')
end
def load_cardlib
Cardname
Wagn.send :include, Wagn::Exceptions
Card.send :include, Cardlib::Exceptions
ActiveRecord::Base.class_eval do
include Cardlib::ActsAsCardExtension
include Cardlib::AttributeTracking
end
Cardlib::ModuleMethods #load
Card::Base.class_eval do
include Cardlib::TrackedAttributes
include Cardlib::Templating
include Cardlib::Defaults
include Cardlib::Permissions
include Cardlib::Search
include Cardlib::References
include Cardlib::Cacheable
include Cardlib::Settings
include Cardlib::Settings::ClassMethods
extend Cardlib::CardAttachment::ActMethods
end
Cardlib::Fetch # trigger autoload
end
def setup_multihost
# set schema for multihost wagns (make sure this is AFTER loading wagn.rb duh)
#ActiveRecord::Base.logger.info("------- multihost = #{System.multihost} and WAGN_NAME= #{ENV['WAGN']} -------")
if System.multihost and ENV['WAGN']
if mapping = MultihostMapping.find_by_wagn_name(ENV['WAGN'])
System.base_url = "http://" + mapping.canonical_host
System.wagn_name = mapping.wagn_name
end
ActiveRecord::Base.connection.schema_search_path = ENV['WAGN']
#Card.cache.system_prefix = Wagn::Cache.system_prefix
end
end
def load_cardtypes
Dir["#{RAILS_ROOT}/app/models/card/*.rb"].sort.each do |cardtype|
cardtype.gsub!(/.*\/([^\/]*)$/, '\1')
begin
require_dependency "card/#{cardtype}"
rescue Exception=>e
raise "Error loading card/#{cardtype}: #{e.message}"
end
end
end
def load_modules
Wagn::Module.load_all
end
def initialize_builtin_cards
## DEBUG
File.open("#{RAILS_ROOT}/log/wagn.log","w") do |f|
f.puts "Wagn::Initializer.initialize_builtin_cards"
end
%w{ *head *alert *foot *navbox *version *account_link *now }.each do |key|
Card.add_builtin( Card.new(:name=>key, :builtin=>true, :skip_defaults=>true))
end
end
end
end
# oof, this is not polished
class Config
def initialize
@data = {}
end
def method_missing(meth, *args)
if meth.to_s =~ /^(.*)\=$/
@data[$~[1]] = args[0]
else
@data[meth.to_s]
end
end
end
@@config = Config.new
def self.config
@@config
end
end