1 class TaskSupport
2 class << self
3 def establish_connection
4 unless ActiveRecord::Base.connected?
5 connection_hash = YAML.load_file("#{Rails.root}/config/database.yml").to_hash
6 env_connection = connection_hash[RAILS_ENV]
7 ActiveRecord::Base.establish_connection(env_connection)
8 end
9 end
10 def config_export(path = "#{Rails.root}/config/radiant_config.yml")
11 self.establish_connection
12 FileUtils.mkdir_p(File.dirname(path))
13 if File.open(File.expand_path(path), 'w') { |f| YAML.dump(Radiant::Config.to_hash.to_yaml,f) }
14 puts "Radiant::Config saved to #{path}"
15 end
16 end
17 def config_import(path = "#{Rails.root}/config/radiant_config.yml", clear = nil)
18 self.establish_connection
19 if File.exist?(path)
20 begin
21 Radiant::Config.transaction do
22 Radiant::Config.delete_all if clear
23 configs = YAML.load(YAML.load_file(path))
24 configs.each do |key, value|
25 c = Radiant::Config.find_or_initialize_by_key(key)
26 c.value = value
27 c.save
28 end
29 end
30 puts "Radiant::Config updated from #{path}"
31 rescue ActiveRecord::RecordInvalid => e
32 puts "IMPORT FAILED and rolled back. #{e}"
33 end
34 else
35 puts "No file exists at #{path}"
36 end
37 end
38
39 # Write the combined content of files in dir into cache_file in the same dir.
40 #
41 def cache_files(dir, files, cache_file)
42 cache_content = files.collect { |f|
43 File.read(File.join(dir, f)) }.join("\n\n")
44
45 cache_path = File.join(dir, cache_file)
46 File.delete(cache_path) if File.exists?(cache_path)
47 File.open(cache_path, "w+") { |f| f.write(cache_content) }
48 end
49
50 # Reads through the layout file and returns an array of JS filenames
51 #
52 def find_admin_js
53 layout = "#{RADIANT_ROOT}/app/views/layouts/application.html.haml"
54 js_regexp = /javascript_include_tag %w\((.*)\), :cache => 'admin\/all/
55 files = File.open(layout) { |f| f.read.match(js_regexp)[1].split }
56 files.collect { |f| f.split('/').last + '.js' }
57 end
58
59 def cache_admin_js
60 dir = "#{Rails.root}/public/javascripts/admin"
61 cache_files(dir, find_admin_js, 'all.js')
62 end
63 end
64 end