Skip to content

Commit f06801f

Browse files
committed
Update forms view
1 parent 01ed4c9 commit f06801f

2 files changed

Lines changed: 18 additions & 150 deletions

File tree

server/controllers/forms_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
class FormsController < ActionController::Base
4+
before_action { Rails.autoloaders.main.reload }
45
class IndexPage < Phlex::HTML
56
def view_template
67
render Layout.new do

server/rails.rb

Lines changed: 17 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -11,155 +11,8 @@
1111
require_relative "../lib/superform/rails"
1212

1313
# Directories
14-
SERVER_DIR = File.expand_path(__dir__)
15-
EXAMPLES_DIR = File.expand_path("../examples", __dir__)
16-
17-
# Load base class first (before Zeitwerk loads example forms)
18-
require File.join(EXAMPLES_DIR, "example_form.rb")
19-
20-
# Load examples with Zeitwerk (no namespace, with reloading)
21-
EXAMPLES_LOADER = Zeitwerk::Loader.new
22-
EXAMPLES_LOADER.push_dir(EXAMPLES_DIR, namespace: Object)
23-
EXAMPLES_LOADER.ignore(File.join(EXAMPLES_DIR, "example_form.rb"))
24-
EXAMPLES_LOADER.enable_reloading
25-
EXAMPLES_LOADER.setup
26-
27-
# Collect form classes dynamically (reloads on each request)
28-
def form_classes
29-
EXAMPLES_LOADER.reload
30-
Dir[File.join(EXAMPLES_DIR, "*.rb")].sort.map do |file|
31-
class_name = File.basename(file, ".rb").split("_").map(&:capitalize).join
32-
Object.const_get(class_name)
33-
end
34-
end
35-
36-
# Simple model for forms to bind to
37-
class Example
38-
include ActiveModel::Model
39-
include ActiveModel::Attributes
40-
41-
attribute :name, :string
42-
attribute :email, :string
43-
attribute :password, :string
44-
attribute :age, :integer
45-
attribute :title, :string
46-
attribute :body, :string
47-
attribute :country, :string
48-
attribute :priority, :string
49-
attribute :quantity, :integer
50-
attribute :terms_accepted, :boolean
51-
attribute :subscribe, :boolean
52-
attribute :featured, :boolean
53-
attribute :birth_date, :date
54-
attribute :appointment_time, :time
55-
attribute :event_datetime, :datetime
56-
attribute :favorite_color, :string
57-
attribute :volume, :integer
58-
attribute :search_query, :string
59-
attribute :avatar, :string
60-
attribute :address, :string
61-
62-
def self.model_name
63-
ActiveModel::Name.new(self, nil, "Example")
64-
end
65-
end
66-
67-
# Layout component
68-
class Layout < Phlex::HTML
69-
def initialize(title: "Superform Examples")
70-
@title = title
71-
end
72-
73-
def view_template(&)
74-
doctype
75-
html do
76-
head do
77-
title { @title }
78-
link rel: "stylesheet", href: "/pico.min.css"
79-
end
80-
body do
81-
main class: "container" do
82-
header do
83-
h1 { a(href: "/") { "Superform Examples" } }
84-
end
85-
yield
86-
end
87-
end
88-
end
89-
end
90-
end
91-
92-
# Index page
93-
class IndexPage < Phlex::HTML
94-
def view_template
95-
render Layout.new do
96-
form_classes.each_with_index do |form_class, index|
97-
render FormCard.new(form_class: form_class, index: index)
98-
end
99-
end
100-
end
101-
end
102-
103-
class FormCard < Phlex::HTML
104-
def initialize(form_class:, index:)
105-
@form_class = form_class
106-
@index = index
107-
end
108-
109-
def view_template
110-
h2 { a(href: "/forms/#{@index}") { @form_class.name_text } }
111-
p { @form_class.description.html_safe }
112-
end
113-
end
114-
115-
# Show page
116-
class ShowPage < Phlex::HTML
117-
def initialize(form_class:)
118-
@form_class = form_class
119-
@form = form_class.new(Example.new, action: "#")
120-
end
121-
122-
def view_template
123-
render Layout.new(title: "#{@form_class.name_text} - Superform Examples") do
124-
p { a(href: "/") { "Back to all forms" } }
125-
126-
h2 { @form_class.name_text }
127-
p { @form_class.description.html_safe }
128-
render @form
129-
130-
hr
131-
132-
h3 { "Source Code" }
133-
pre do
134-
code { source_code }
135-
end
136-
end
137-
end
138-
139-
private
140-
141-
def source_code
142-
file = File.join(EXAMPLES_DIR, "#{underscore(@form_class.name)}.rb")
143-
File.exist?(file) ? File.read(file) : "# Source not found"
144-
end
145-
146-
def underscore(name)
147-
name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
148-
end
149-
end
150-
151-
# Controller
152-
class FormsController < ActionController::Base
153-
def index
154-
render IndexPage.new, layout: false
155-
end
156-
157-
def show
158-
index = params[:id].to_i
159-
form_class = form_classes[index]
160-
render ShowPage.new(form_class: form_class), layout: false
161-
end
162-
end
14+
SERVER_DIR = Pathname.new(__dir__)
15+
EXAMPLES_DIR = SERVER_DIR.join("../examples").expand_path
16316

16417
# Minimal Rails app
16518
class SuperformApp < Rails::Application
@@ -169,9 +22,23 @@ class SuperformApp < Rails::Application
16922
config.secret_key_base = "superform-demo-secret-key-base-for-development-only"
17023
config.hosts.clear
17124
config.public_file_server.enabled = true
25+
26+
config.autoload_paths << root.join("components")
27+
config.autoload_paths << root.join("models")
28+
config.autoload_paths << root.join("controllers")
29+
30+
config.autoload_paths << EXAMPLES_DIR
31+
end
32+
33+
# Collect form classes dynamically
34+
def form_classes
35+
EXAMPLES_DIR.glob("*.rb").sort.filter_map do |path|
36+
next if path.basename.to_s == "example_form.rb"
37+
cpath = Rails.autoloaders.main.cpath_expected_at(path)
38+
Object.const_get(cpath)
39+
end
17240
end
17341

174-
# Initialize and run
17542
SuperformApp.initialize!
17643

17744
SuperformApp.routes.draw do

0 commit comments

Comments
 (0)