Sha256: 7e93a7144ec331ab53e382afdc39716d43b52bb42928354da0eb3b7b4cc36223

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

module Vapid
  module Controllers
    # Project routes
    module ProjectController
      extend Sinatra::Extension

      # rubocop:disable Metrics/AbcSize, MethodLength
      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
      # rubocop:enable Metrics/AbcSize, MethodLength
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vapid-0.1.3 lib/vapid/controllers/project_controller.rb