Module: Vapid::Controllers::ProjectController

Extended by:
Sinatra::Extension
Defined in:
lib/vapid/controllers/project_controller.rb

Overview

Project routes

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

rubocop:disable Metrics/AbcSize, MethodLength



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vapid/controllers/project_controller.rb', line 8

def self.registered(app)
  app.configure do
    root_path = Pathname.new(app.settings.root)
    tmp_path = Pathname.new(app.settings.project_cache)
    relative_path = tmp_path.relative_path_from(root_path)
    
    app.set :cache_enabled, false
    app.set :cache_path, File.join(relative_path, "server")
    app.set :layouts, File.join(app.settings.project_views, "layouts")
  end

  app.configure :production do
    app.set :cache_enabled, true
  end

  # rubocop:disable Lint/NestedMethodDefinition
  app.helpers do
    def asset_timestamp(*args)
      super(args) unless settings.deploy
    end

    def dynamic_layout(path)
      path_layout = path.split("/")[1]
      if path_layout && Dir.glob(File.join(settings.layouts, "#{path_layout}.*")).any?
        "layouts/#{path_layout}"
      else
        :default
      end
    end

    def render_or_index(path)
      render path, views: settings.project_views, layout: dynamic_layout(path)
    rescue
      render "#{path.chomp('/')}/index", views: settings.project_views, layout: dynamic_layout(path)
    end
  end
  # rubocop:enable Lint/NestedMethodDefinition

  app.get "*" do
    path = params[:splat].first.to_s

    cache_block "#{path}_" do
      html = render_or_index(path)
      if app.deploy
        headers["Content-Disposition"] = "inline"
        html
      else
        Template.new(html).render
      end
    end
  end
end