Skip to content

Commit a886674

Browse files
committed
Add basic form for example post with delegated type
1 parent 2ef37e1 commit a886674

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Examples::PostsController < ApplicationController
2+
def index
3+
postable = case params[:type]
4+
when "link"
5+
Examples::Posts::Link.new
6+
when "image"
7+
Examples::Posts::Image.new
8+
else
9+
Examples::Posts::Markdown.new
10+
end
11+
12+
post = Examples::Post.new(postable: postable)
13+
14+
render Examples::Posts::IndexView.new(post: post)
15+
end
16+
17+
def create
18+
raise params.inspect
19+
end
20+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
class Examples::Posts::IndexView < ApplicationView
4+
include Phlex::Rails::Helpers::FormWith
5+
include Phlex::Rails::Helpers::LinkTo
6+
7+
def initialize(post:)
8+
@post = post
9+
end
10+
11+
def view_template
12+
render Pages::Header.new(
13+
title: "Examples: Posts",
14+
description: "This is an example for creating a post of different types, like text, link, or image."
15+
)
16+
17+
div(class: "section-content container py-gap") do
18+
div(class: "tabs") do
19+
link_to "Text", examples_posts_path
20+
link_to "Link", examples_posts_path(type: "link")
21+
link_to "Image", examples_posts_path(type: "image")
22+
end
23+
24+
form_with model: @post, url: examples_posts_path, method: :post do |form|
25+
fieldset do
26+
form.label :title
27+
form.text_field :title, placeholder: "My Post 🎸", required: true
28+
end
29+
30+
form.fields_for :postable, @post.postable do |postable_form|
31+
case @post.postable
32+
when Examples::Posts::Markdown
33+
fieldset do
34+
postable_form.label :body, "Text (Markdown)"
35+
postable_form.text_area :body, placeholder: "# This is a post about guitars 🎸", required: true
36+
end
37+
when Examples::Posts::Link
38+
fieldset do
39+
postable_form.label :url, "Link URL"
40+
postable_form.text_field :url, placeholder: "https://example.com", required: true
41+
end
42+
when Examples::Posts::Image
43+
fieldset do
44+
postable_form.label :url, "Image URL"
45+
postable_form.text_field :url, placeholder: "https://example.com/image.jpg", required: true
46+
end
47+
end
48+
end
49+
50+
fieldset do
51+
form.submit "Save", class: "button primary"
52+
end
53+
end
54+
end
55+
end
56+
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace :examples do
1414
resource :counters, only: [:show, :update, :destroy]
1515
resource :hello, only: [:show]
16+
resources :posts, only: [:index, :create]
1617
end
1718
resources :examples, only: [:index, :show]
1819

spec/requests/examples/posts_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Examples::Posts", type: :request do
4+
describe "GET /index" do
5+
it "is successful" do
6+
get examples_posts_path
7+
expect(response).to have_http_status(:success)
8+
end
9+
end
10+
end

spec/system/examples/posts_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Examples: Posts", type: :system do
4+
it "renders the form" do
5+
visit examples_posts_path
6+
7+
within "form" do
8+
expect(page).to have_content("Title")
9+
expect(page).to have_content("Markdown")
10+
end
11+
12+
click_link "Link"
13+
14+
within "form" do
15+
expect(page).to have_content("Title")
16+
expect(page).to have_content("Link URL")
17+
18+
expect(page).not_to have_content("Markdown")
19+
end
20+
21+
click_link "Image"
22+
23+
within "form" do
24+
expect(page).to have_content("Title")
25+
expect(page).to have_content("Image URL")
26+
27+
expect(page).not_to have_content("Link URL")
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)