Sha256: 95c828f18ba34ab43c761c0eb6f4f54365eb15180b545036988c040289905f67

Contents?: true

Size: 1.84 KB

Versions: 5

Compression:

Stored size: 1.84 KB

Contents

require 'volt/server/rack/asset_files'
require 'volt/router/routes'

# Serves the main pages
module Volt
  class IndexFiles
    def initialize(rack_app, volt_app, component_paths, opal_files)
      @rack_app        = rack_app
      @volt_app        = volt_app
      @component_paths = component_paths
      @opal_files      = opal_files

      @@router = volt_app.router

      @@router.define do
        # Load routes for each component
        component_paths.components.values.flatten.uniq.each do |component_path|
          routes_path = "#{component_path}/config/routes.rb"

          if File.exist?(routes_path)
            route_file = File.read(routes_path)
            instance_eval(route_file, routes_path, 0)
          end
        end
      end
    end

    def route_match?(path)
      params = @@router.url_to_params(path)

      return params if params

      false
    end

    def call(env)
      if route_match?(env['PATH_INFO'])
        [200, { 'Content-Type' => 'text/html; charset=utf-8' }, [html]]
      else
        @rack_app.call env
      end
    end

    def html
      index_path = File.expand_path(File.join(Volt.root, 'config/base/index.html'))
      html       = File.read(index_path)

      ERB.new(html, nil, '-').result(binding)
    end

    def javascript_files(*args)
      fail "Deprecation: #javascript_files is deprecated in config/base/index.html, opal 0.8 required a new format."
    end

    def css_files(*args)
      fail "Deprecation: #css_files is deprecated in config/base/index.html, opal 0.8 required a new format."
    end

    def javascript_tags
      # TODO: Cache somehow, this is being loaded every time
      AssetFiles.from_cache(@volt_app.app_url, 'main', @component_paths).javascript_tags(@volt_app)
    end

    def css_tags
      AssetFiles.from_cache(@volt_app.app_url, 'main', @component_paths).css_tags
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
volt-0.9.5 lib/volt/server/rack/index_files.rb
volt-0.9.5.pre12 lib/volt/server/rack/index_files.rb
volt-0.9.5.pre11 lib/volt/server/rack/index_files.rb
volt-0.9.5.pre9 lib/volt/server/rack/index_files.rb
volt-0.9.5.pre8 lib/volt/server/rack/index_files.rb