Class: TrustyCms::TaskSupport

Inherits:
Object
  • Object
show all
Defined in:
lib/trusty_cms/task_support.rb

Class Method Summary collapse

Class Method Details

.cache_admin_jsObject



63
64
65
66
# File 'lib/trusty_cms/task_support.rb', line 63

def cache_admin_js
  dir = "#{Rails.root}/public/javascripts/admin"
  cache_files(dir, find_admin_js, 'all.js')
end

.cache_files(dir, files, cache_file) ⇒ Object

Write the combined content of files in dir into cache_file in the same dir.



44
45
46
47
48
49
50
51
52
# File 'lib/trusty_cms/task_support.rb', line 44

def cache_files(dir, files, cache_file)
  cache_content = files.collect do |f|
    File.read(File.join(dir, f))
  end .join("\n\n")

  cache_path = File.join(dir, cache_file)
  File.delete(cache_path) if File.exists?(cache_path)
  File.open(cache_path, 'w+') { |f| f.write(cache_content) }
end

.config_export(path = "#{Rails.root}/config/trusty_config.yml") ⇒ Object



12
13
14
15
16
17
18
# File 'lib/trusty_cms/task_support.rb', line 12

def config_export(path = "#{Rails.root}/config/trusty_config.yml")
  establish_connection
  FileUtils.mkdir_p(File.dirname(path))
  if File.open(File.expand_path(path), 'w') { |f| YAML.dump(TrustyCms::Config.to_hash.to_yaml, f) }
    puts "TrustyCms::Config saved to #{path}"
  end
end

.config_import(path = "#{Rails.root}/config/trusty_config.yml", clear = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/trusty_cms/task_support.rb', line 20

def config_import(path = "#{Rails.root}/config/trusty_config.yml", clear = nil)
  establish_connection
  if File.exist?(path)
    begin
      TrustyCms::Config.transaction do
        TrustyCms::Config.delete_all if clear
        configs = YAML.load(YAML.load_file(path))
        configs.each do |key, value|
          c = TrustyCms::Config.find_or_initialize_by_key(key)
          c.value = value
          c.save
        end
      end
      puts "TrustyCms::Config updated from #{path}"
    rescue ActiveRecord::RecordInvalid => e
      puts "IMPORT FAILED and rolled back. #{e}"
    end
  else
    puts "No file exists at #{path}"
  end
end

.establish_connectionObject



4
5
6
7
8
9
10
# File 'lib/trusty_cms/task_support.rb', line 4

def establish_connection
  unless ActiveRecord::Base.connected?
    connection_hash = YAML.load_file("#{Rails.root}/config/database.yml").to_hash
    env_connection = connection_hash[Rails.env]
    ActiveRecord::Base.establish_connection(env_connection)
  end
end

.find_admin_jsObject

Reads through the layout file and returns an array of JS filenames



56
57
58
59
60
61
# File 'lib/trusty_cms/task_support.rb', line 56

def find_admin_js
  layout = "#{TRUSTY_CMS_ROOT}/app/views/layouts/application.html.haml"
  js_regexp = /javascript_include_tag %w\((.*)\), :cache => 'admin\/all/
  files = File.open(layout) { |f| f.read.match(js_regexp)[1].split }
  files.collect { |f| f.split('/').last + '.js' }
end