Class: CabooseHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/caboose/caboose_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(app_path, force = false) ⇒ CabooseHelper

Returns a new instance of CabooseHelper.



4
5
6
7
# File 'lib/caboose/caboose_helper.rb', line 4

def initialize(app_path, force = false)
  @app_path = app_path
  @force = force
end

Instance Method Details

#init_allObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/caboose/caboose_helper.rb', line 9

def init_all
  init_gem
  init_app_config    
  init_initializer
  init_routes
  init_assets
  init_tinymce
  init_session
  remove_public_index
end

#init_app_configObject

Require caboose in the application config



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/caboose/caboose_helper.rb', line 53

def init_app_config
  puts "Requiring caboose in the application config..."
  
  filename = File.join(@app_path,'config','application.rb')
  return if !File.exists?(filename)    
        
  file = File.open(filename, 'rb')
  contents = file.read
  file.close    
  if (contents.index("require 'caboose'").nil?)
    arr = contents.split("require 'rails/all'", -1)
    str = arr[0] + "\nrequire 'rails/all'\nrequire 'caboose'\n" + arr[1]
    File.open(filename, 'w') { |file| file.write(str) }
  end
end

#init_assetsObject



136
137
138
139
140
141
# File 'lib/caboose/caboose_helper.rb', line 136

def init_assets
  puts "Adding the layout files..."
  init_file('app/views/layouts/layout_default.html.erb')
  puts "Adding the layout files..."
  init_file('app/assets/stylesheets/login.css')
end

#init_file(filename) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/caboose/caboose_helper.rb', line 20

def init_file(filename)
  gem_root = Gem::Specification.find_by_name('caboose-cms').gem_dir
  filename = File.join(@app_path, filename)  
  copy_from = File.join(gem_root,'lib','sample_files', Pathname.new(filename).basename)
  
  if (!File.exists?(filename) || @force)
    FileUtils.cp(copy_from, filename)
  end  
end

#init_gemObject

Add the gem to the Gemfile



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/caboose/caboose_helper.rb', line 31

def init_gem
  puts "Adding the caboose gem to the Gemfile... "
  filename = File.join(@app_path,'Gemfile')
  return if !File.exists?(filename)    
  return if !@force
  
  file = File.open(filename, 'rb')
  str = file.read
  file.close
  str2 = ""
  str.each_line do |line|
    if (!line.strip.start_with?('#') && (!line.index("gem 'caboose-cms'").nil? || !line.index('gem "caboose-cms"').nil?))
      str2 << "##{line}"
    else
      str2 << line
    end
  end
  str2 << "gem 'caboose-cms', '= #{Caboose::VERSION}'\n"
  File.open(filename, 'w') {|file| file.write(str2) }
end

#init_initializerObject

Adds the caboose initializer file



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
# File 'lib/caboose/caboose_helper.rb', line 79

def init_initializer
  puts "Adding the caboose initializer file..."
  
  filename = File.join(@app_path,'config','initializers','caboose.rb')
  return if File.exists?(filename) && !@force
  
  Caboose::salt = Digest::SHA1.hexdigest(DateTime.now.to_s)
  str = ""
  str << "# Salt to ensure passwords are encrypted securely\n"
  str << "Caboose::salt = '#{Caboose::salt}'\n\n"
  str << "# Where page asset files will be uploaded\n"
  str << "Caboose::assets_path = Rails.root.join('app', 'assets', 'caboose')\n\n"
  str << "# Register any caboose plugins\n"
  str << "#Caboose::plugins + ['MyCaboosePlugin']\n\n"
  str << "# Tell the host app about the caboose assets\n"
  str << "Rails.application.config.assets.paths << Rails.root.join('vendor','gems','caboose-cms','app','assets','javascripts')\n"
  str << "Rails.application.config.assets.paths << Rails.root.join('vendor','gems','caboose-cms','app','assets','stylesheets')\n"
  str << "Rails.application.config.assets.precompile += [\n"
  str << "  'login.css',\n"
  str << "  'caboose/admin.js',\n"
  str << "  'caboose/application.js',\n"
  str << "  'caboose/login.js',\n"
  str << "  'caboose/model.form.page.js',\n"
  str << "  'caboose/station.js',\n"
  str << "  'caboose/admin.css',\n"
  str << "  'caboose/application.css',\n"
  str << "  'caboose/caboose.css',\n"
  str << "  'caboose/fonts.css',\n"
  str << "  'caboose/tinymce.css'\n"
  str << "]\n\n"

  File.open(filename, 'w') {|file| file.write(str) }
end

#init_routesObject

Adds the routes to the host app to point everything to caboose



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/caboose/caboose_helper.rb', line 114

def init_routes
  puts "Adding the caboose routes..."
  
  filename = File.join(@app_path,'config','routes.rb')
  return if !File.exists?(filename)
  return if !@force
  
  str = "" 
  str << "\t# Catch everything with caboose\n"  
  str << "\tmount Caboose::Engine => '/'\n"
  str << "\tmatch '*path' => 'caboose/pages#show'\n"
  str << "\troot :to      => 'caboose/pages#show'\n"    
  file = File.open(filename, 'rb')
  contents = file.read
  file.close    
  if (contents.index(str).nil?)
    arr = contents.split('end', -1)
    str2 = arr[0] + "\n" + str + "\nend" + arr[1]
    File.open(filename, 'w') {|file| file.write(str2) }
  end    
end

#init_sessionObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/caboose/caboose_helper.rb', line 148

def init_session
  puts "Setting the session config..."    
  
  lines = []
  str = File.open(File.join(@app_path,'config','initializers','session_store.rb')).read 
  str.gsub!(/\r\n?/, "\n")
  str.each_line do |line|
    line = '#' + line if !line.index(':cookie_store').nil?        && !line.start_with?('#')
    line[0] = ''      if !line.index(':active_record_store').nil? && line.start_with?('#')
    lines << line.strip
  end
  str = lines.join("\n")
  File.open(File.join(@app_path,'config','initializers','session_store.rb'), 'w') {|file| file.write(str) }
end

#init_tinymceObject



143
144
145
146
# File 'lib/caboose/caboose_helper.rb', line 143

def init_tinymce
  puts "Adding the tinymce config file..."
  init_file('config/tinymce.yml')
end

#remove_public_indexObject

Removes the public/index.html file from the rails app



70
71
72
73
74
75
76
# File 'lib/caboose/caboose_helper.rb', line 70

def remove_public_index
  puts "Removing the public/index.html file... "
  
  filename = File.join(@app_path,'public','index.html')
  return if !File.exists?(filename)
  File.delete(filename)
end