Alpha Chen 1 year ago
parent 019a31f483
commit 0cf17ab5ac
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -5,9 +5,14 @@ if dev
logger = Logger.new($stdout)
end
require "rack/unreloader"
Unreloader = Rack::Unreloader.new(subclasses: %w[Roda Sequel::Model], logger: logger, reload: dev, autoload: dev){App}
require_relative "lib/models"
Unreloader.require("lib/app.rb"){'App'}
run(dev ? Unreloader : App.freeze.app)
Unreloader = Rack::Unreloader.new(
subclasses: %w[Roda Sequel::Model Phlex::HTML],
logger: logger,
reload: dev,
autoload: dev,
) { RankKing::App }
# require_relative("lib/models")
Unreloader.require("lib/views.rb")
Unreloader.require("lib/app.rb") { "RankKing::App" }
run(dev ? Unreloader : RankKing::App.freeze.app)

@ -1,8 +1,10 @@
# frozen_string_literal: true
require_relative "models"
require_relative "views"
require "roda"
module RankKing
class App < Roda
opts[:check_dynamic_arity] = false
opts[:check_arity] = :warn
@ -29,18 +31,18 @@ class App < Roda
# if ENV["RACK_ENV"] == 'development'
# css_opts.merge!(source_map_embed: true, source_map_contents: true, source_map_file: ".")
# end
plugin :render_coverage if defined?(SimpleCov)
# plugin :render_coverage if defined?(SimpleCov)
# :nocov:
plugin :route_csrf
plugin :flash
# plugin :assets, css: "app.scss", css_opts: css_opts, timestamp_paths: true
# plugin :render, escape: true, layout: "./layout", :template_opts=>{chain_appends: !defined?(SimpleCov), freeze: true, skip_compiled_encoding_detection: true}
plugin :render, engine: :phlex
# plugin :public
plugin :Integer_matcher_max
plugin :typecast_params_sized_integers, sizes: [64], default_size: 64
plugin :hash_branch_view_subdir
# plugin :hash_branch_view_subdir
plugin :custom_block_results
logger = if ENV["RACK_ENV"] == "test"
Class.new{def write(_) end}.new
@ -80,8 +82,8 @@ class App < Roda
$stderr.print "#{e.class}: #{e.message}\n"
$stderr.puts e.backtrace
next exception_page(e, assets: true) if ENV["RACK_ENV"] == "development"
@page_title = "Internal Server Error"
view(content: "")
# @page_title = "Internal Server Error"
# view(content: "")
end
end
@ -96,64 +98,24 @@ class App < Roda
# end
# Unreloader.autoload("routes", delete_hook: proc{|f| hash_branch(File.basename(f).delete_suffix(".rb"))}){}
# def render(view, layout=Views::Layout.new)
# component.call
# end
route do |r|
# r.public
# r.assets
check_csrf!
r.root do
@page_title = "Foo"
# render("hello", locals: {name: "Alice"})
view("hello", locals: {name: "Bob"})
end
Views::Layout.new(Views::Hello.new(name: "Bob")).call
end
end
require "tilt/template"
require "phlex"
module Tilt
class PhlexTemplate < Template
def prepare
r.on "pools" do
r.is "new" do
Views::Layout.new(Views::NewPool.new).call
end
# I have no idea how this works - it's just copied pretty much blindly from here:
#
# https://github.com/phlex-ruby/phlex-rails/blob/main/lib/phlex/rails/layout.rb#L18
def evaluate(scope, locals, &block)
klass = Class.new(Phlex::HTML)
klass.class_eval(data, __FILE__, __LINE__)
component = klass.new
scope.instance_variables
.reject { _2.start_with?("@_") }
.each do |k|
component.instance_variable_set(k, scope.instance_variable_get(k))
end
locals.each do |k,v|
component.instance_variable_set("@#{k}", v)
end
component.call do |yielded|
output = yield
component.unsafe_raw(output)
end
end
# Not sure if this is better...?
# def precompiled_preamble(*) = "klass = Class.new(Phlex::HTML) do"
# def precompiled_template(*) = data
# def precompiled_postamble(locals) = <<~RUBY
# end
# component = klass.new(**locals)
# component.call do |yielded|
# output = yield
# component.unsafe_raw(output)
# end
# RUBY
end
end
Tilt.register(Tilt::PhlexTemplate, "phlex")

@ -16,7 +16,7 @@ unless defined?(Unreloader)
Unreloader = Rack::Unreloader.new(reload: false, autoload: !ENV["NO_AUTOLOAD"])
end
Unreloader.autoload("models"){|f| Sequel::Model.send(:camelize, File.basename(f).sub(/\.rb\z/, ''))}
# Unreloader.autoload("models"){|f| Sequel::Model.send(:camelize, File.basename(f).sub(/\.rb\z/, ''))}
if ENV["RACK_ENV"] == "development" || ENV["RACK_ENV"] == "test"
require "logger"

@ -0,0 +1,43 @@
require "phlex"
module RankKing
module Views
class Layout < Phlex::HTML
def initialize(view)
@view = view
end
def template
doctype
html(lang: "en") do
head do
meta(charset: "utf-8")
meta(name: "viewport", content: "width=device-width, initial-scale=1")
title { "Rank King" }
end
body do
header do
h1 { "Rank King" }
end
nav
main do
render @view
end
footer
end
end
end
end
class NewPool < Phlex::HTML
def template
plain "ohai"
end
end
end
end

@ -1,3 +0,0 @@
def template
h1 { "👋 Hello #@name!" }
end

@ -1,5 +0,0 @@
def template
article do
yield
end
end
Loading…
Cancel
Save