require 'erb' require 'fileutils' require 'rubygems' require_gem 'activerecord' class Setting < ActiveRecord::Base end class Typo def on_create(options) name = options[:name] spaces_tool_id = options[:spaces_tool_id] # Copy a directory. FileUtils::cp_r root_dir, tool_dir(name) # Create an initial database. conn = connection conn.create_database(tool_database(spaces_tool_id)) # Create a schema. system( script_path, root_dir, tool_database(spaces_tool_id), config_database['username'], config_database['password'].to_s, config_database['host'], config_database['adapter'] ) # Reconnect to a tool database and an initial settings. conn = connection(:database => tool_database(spaces_tool_id)) Setting.new(:name => 'breakout_uri', :value => options[:breakout_uri]).save! Setting.new(:name => 'spaces_tool_id', :value => spaces_tool_id).save! Setting.new(:name => 'secret_key', :value => options[:secret_key]).save! Setting.new(:name => 'breakout_login', :value => options[:breakout_login]).save! Setting.new(:name => 'spaces_tool_url', :value => options[:spaces_tool_url]).save! Setting.new(:name => 'space_name', :value => options[:space_name]).save! Setting.new(:name => 'breakout_url', :value => options[:breakout_url]).save! Setting.new(:name => 'menu_name', :value => options[:menu_name]).save! Setting.new(:name => 'name', :value => options[:name]).save! # Set up a configuration. assigns = {} assigns[:spaces_tool_id] = spaces_tool_id file_name = database_config_rhtml(name) File.open(database_config(name), 'w') do |f| f.write template(database_config_rhtml(name), assigns) end # Return an URL. base_url(options) end def on_destroy(options) name = options[:name] spaces_tool_id = options[:spaces_tool_id] # Remove a database. conn = connection conn.drop_database(tool_database(spaces_tool_id)) # Remove a directory. There apply the same comment as with copying a directory. FileUtils::rm_r tool_dir(name) end protected def connection(options={}) options[:adapter] ||= config_database['adapter'] options[:host] ||= config_database['host'] options[:username] ||= config_database['username'] options[:password] ||= config_database['password'] options[:database] ||= config_database['database'] options[:socket] ||= config_database['socket'] Setting.connection = options ActiveRecord::Base.establish_connection(options) ActiveRecord::Base.connection end def config $config['typo'].dup end def config_database $config['database'].dup end def tool_database(spaces_tool_id) (config['database_prefix'] + spaces_tool_id).gsub(/[0-9\-]/, '') end def root_dir config['base_dir'] + 'typo-' + config['version'] end def tool_dir(name) (config['base_dir'] + '/' + name).downcase end def script_path config['base_dir'] + config['script'] end def database_config(name) tool_dir(name) + '/config/database.yml' end def database_config_rhtml(name) database_config(name) + '.rhtml' end def template(file_name, assigns) file = File.open(file_name) vars = assigns || {} b = binding vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b } ERB.new(file.read, nil).result(b) end def base_url(options) assigns = {} assigns['SERVER'] = $config['server'].dup assigns['HOST'] = config['host'] or '' assigns['SPACES_TOOL_ID'] = options[:spaces_tool_id] assigns['NAME'] = options[:name] assigns['MENU_NAME'] = options[:menu_name] url = config['base_url'].dup assigns.each do |k,v| url.gsub!(k, v) end url end end