Class: Susanoo::Application::Views

Inherits:
Controller show all
Defined in:
lib/susanoo/controllers/views.rb

Overview

This controller is responsible to serving/building angularjs templates.

Instance Attribute Summary

Attributes inherited from Controller

#debug, #environment, #project_root

Instance Method Summary collapse

Methods inherited from Controller

#initialize, #static_compile?

Constructor Details

This class inherits a constructor from Susanoo::Controller

Instance Method Details

#build(generator, options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/susanoo/controllers/views.rb', line 18

def build(generator, options)
  extensions = ['erb', 'slim', 'haml', 'md', 'org', 'liquid', 'rdoc']
  file_pattern = File.join(project_root, "src/views/**/*.{html,#{extensions.join(',')}}")
  dest_path = File.join(project_root, 'www')
  src_path = File.join(project_root, 'src')

  Dir.glob(file_pattern) do |file|
    template = Tilt.new file

    dest_file = File.join(dest_path,
                          file.gsub(src_path, ''))

    # Create missing directories in destination path
    FileUtils.mkpath dest_path

    if extensions.include? File.extname(dest_file)[1..-1]
      # Remove erb extension name from destination path
      dest_file.gsub!(File.extname(dest_file), '')
      dest_file = "#{dest_file}.html" if File.extname(dest_file).empty?
    end
    # Create the destination file
    generator.create_file dest_file, template.render(self)
  end
end

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/susanoo/controllers/views.rb', line 6

def call(env)
  path =  env['PATH_INFO']
  if File.exist?(File.join(project_root, "src/views#{path}.erb"))
    template = Tilt.new(File.join(project_root, "src/views#{path}.erb"))
  elsif File.exist?(File.join(project_root, "src/views#{path}"))
    template = Tilt.new(File.join(project_root, "src/views#{path}"))
  else
    fail "There is no '#{path}' in 'src/views' directory."
  end
  [200, {'Content-Type' => 'text/html'}, [template.render(self)]]
end