Skip to content

Commit 01ed4c9

Browse files
committed
Add example rails project
1 parent f62e747 commit 01ed4c9

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

server/components/form_card.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
class FormCard < Phlex::HTML
4+
def initialize(form_class:, index:)
5+
@form_class = form_class
6+
@index = index
7+
end
8+
9+
def view_template
10+
h2 { a(href: "/forms/#{@index}") { @form_class.name_text } }
11+
p { @form_class.description.html_safe }
12+
end
13+
end

server/components/layout.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
class Layout < Phlex::HTML
4+
def initialize(title: "Superform Examples")
5+
@title = title
6+
end
7+
8+
def view_template(&)
9+
doctype
10+
html do
11+
head do
12+
title { @title }
13+
link rel: "stylesheet", href: "/pico.min.css"
14+
end
15+
body do
16+
main class: "container" do
17+
header do
18+
h1 { a(href: "/") { "Superform Examples" } }
19+
end
20+
yield
21+
end
22+
end
23+
end
24+
end
25+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
class FormsController < ActionController::Base
4+
class IndexPage < Phlex::HTML
5+
def view_template
6+
render Layout.new do
7+
form_classes.each_with_index do |form_class, index|
8+
render FormCard.new(form_class: form_class, index: index)
9+
end
10+
end
11+
end
12+
end
13+
14+
class ShowPage < Phlex::HTML
15+
def initialize(form_class:)
16+
@form_class = form_class
17+
@form = form_class.new(Example.new, action: "#")
18+
end
19+
20+
def view_template
21+
render Layout.new(title: "#{@form_class.name_text} - Superform Examples") do
22+
p { a(href: "/") { "Back to all forms" } }
23+
24+
h2 { @form_class.name_text }
25+
p { @form_class.description.html_safe }
26+
render @form
27+
28+
hr
29+
30+
h3 { "Source Code" }
31+
pre do
32+
code { source_code }
33+
end
34+
end
35+
end
36+
37+
private
38+
39+
def source_code
40+
file = File.join(EXAMPLES_DIR, "#{underscore(@form_class.name)}.rb")
41+
File.exist?(file) ? File.read(file) : "# Source not found"
42+
end
43+
44+
def underscore(name)
45+
name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
46+
end
47+
end
48+
49+
def index
50+
render IndexPage.new, layout: false
51+
end
52+
53+
def show
54+
index = params[:id].to_i
55+
form_class = form_classes[index]
56+
render ShowPage.new(form_class: form_class), layout: false
57+
end
58+
end

server/models/example.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
class Example
4+
include ActiveModel::Model
5+
include ActiveModel::Attributes
6+
7+
attribute :name, :string
8+
attribute :email, :string
9+
attribute :password, :string
10+
attribute :age, :integer
11+
attribute :title, :string
12+
attribute :body, :string
13+
attribute :country, :string
14+
attribute :priority, :string
15+
attribute :quantity, :integer
16+
attribute :terms_accepted, :boolean
17+
attribute :subscribe, :boolean
18+
attribute :featured, :boolean
19+
attribute :birth_date, :date
20+
attribute :appointment_time, :time
21+
attribute :event_datetime, :datetime
22+
attribute :favorite_color, :string
23+
attribute :volume, :integer
24+
attribute :search_query, :string
25+
attribute :avatar, :string
26+
attribute :address, :string
27+
28+
def self.model_name
29+
ActiveModel::Name.new(self, nil, "Example")
30+
end
31+
end

0 commit comments

Comments
 (0)