feat(example): add example app and blog app with demos
This commit is contained in:
1 parent
28e6bc3cf2
commit
7d2312b94b
13 files changed
+234
No files matched your search
@@ -0,0 +1,23 @@
|
||||
require 'prism'
|
||||
|
||||
APP_ROOT = __dir__
|
||||
|
||||
Prism::Orm.connect(File.join(APP_ROOT, 'db', 'development.pstore'))
|
||||
Prism::Orm.migrator(File.join(APP_ROOT, 'db', 'migrate')).migrate!
|
||||
|
||||
Prism::Controller.views_root(File.join(APP_ROOT, 'app', 'views'))
|
||||
Prism::Controller.layout(:application)
|
||||
|
||||
Dir[File.join(APP_ROOT, 'app', 'models', '*.rb')].sort.each { |file| require file }
|
||||
Dir[File.join(APP_ROOT, 'app', 'controllers', '*.rb')].sort.each { |file| require file }
|
||||
|
||||
class Blog < Prism::Application
|
||||
set :views, File.join(APP_ROOT, 'app', 'views')
|
||||
enable_sessions(secret: 'blog-demo-secret')
|
||||
use Prism::Middleware::Logger
|
||||
use Prism::Middleware::ParamsParser
|
||||
use Prism::Middleware::Static, root: File.join(APP_ROOT, 'public')
|
||||
|
||||
root(to: 'posts#index')
|
||||
resources :posts, only: %i[index show create]
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
class PostsController < Prism::Controller
|
||||
layout :application
|
||||
|
||||
def index
|
||||
render(:index, locals: { posts: Post.order(:created_at, :desc), notice: flash[:notice], errors: [] })
|
||||
end
|
||||
|
||||
def show
|
||||
render(:show, locals: { post: Post.find(params['id']) })
|
||||
rescue Prism::Orm::RecordNotFound
|
||||
halt(404, 'Post not found')
|
||||
end
|
||||
|
||||
def create
|
||||
post = Post.new(title: params['title'], body: params['body'])
|
||||
if post.save
|
||||
flash[:notice] = "Post ##{post.id} created"
|
||||
redirect url_for(:posts)
|
||||
else
|
||||
status(422)
|
||||
render(:index, locals: { posts: Post.order(:created_at, :desc), notice: nil, errors: post.errors.full_messages })
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
class Post < Prism::Orm::Model
|
||||
validates :title, presence: true, length: { minimum: 3 }
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head><title><%= defined?(title) ? title : 'Prism App' %></title></head>
|
||||
<body><%== content %></body>
|
||||
</html>
|
||||
@@ -0,0 +1,4 @@
|
||||
<li class="post" id="post-<%= post.id %>">
|
||||
<h2><%= post.title %></h2>
|
||||
<p><%= post.body %></p>
|
||||
</li>
|
||||
@@ -0,0 +1,21 @@
|
||||
<h1>Blog</h1>
|
||||
<% if notice %>
|
||||
<p class="notice"><%= notice %></p>
|
||||
<% end %>
|
||||
<% unless errors.empty? %>
|
||||
<ul class="errors">
|
||||
<% errors.each do |message| %>
|
||||
<li><%= message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
<form method="post" action="/posts">
|
||||
<input type="text" name="title" placeholder="Title">
|
||||
<textarea name="body" placeholder="Body"></textarea>
|
||||
<button type="submit">Create post</button>
|
||||
</form>
|
||||
<ul class="posts">
|
||||
<% posts.each do |post| %>
|
||||
<%== partial('post', locals: { post: post }) %>
|
||||
<% end %>
|
||||
</ul>
|
||||
@@ -0,0 +1,2 @@
|
||||
<h1><%= post.title %></h1>
|
||||
<p><%= post.body %></p>
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
require_relative '../app'
|
||||
|
||||
server = Prism::Server.new(Blog, port: (ARGV[0] || 3000).to_i).start
|
||||
puts "listening on #{server.url}"
|
||||
trap('INT') { server.stop; exit }
|
||||
sleep
|
||||
@@ -0,0 +1,3 @@
|
||||
require_relative 'app'
|
||||
|
||||
run Blog
|
||||
@@ -0,0 +1,13 @@
|
||||
class CreatePosts < Prism::Orm::Migration
|
||||
def up
|
||||
create_table :posts do |t|
|
||||
t.string :title
|
||||
t.text :body
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
drop_table :posts
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user