Compare commits
3 commits
971c5f2233
...
14f1f3daa6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14f1f3daa6 | ||
|
|
bffbab8fbc | ||
|
|
1a3a4287b0 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
/.rake_tasks~
|
||||||
|
/temp.db
|
||||||
7
Gemfile
7
Gemfile
|
|
@ -4,6 +4,13 @@ gem 'roda'
|
||||||
gem 'rack-unreloader'
|
gem 'rack-unreloader'
|
||||||
gem 'tilt'
|
gem 'tilt'
|
||||||
gem 'erubi'
|
gem 'erubi'
|
||||||
|
gem 'sequel'
|
||||||
|
gem 'rake'
|
||||||
|
|
||||||
|
# Change to whatever database you plan to use
|
||||||
|
gem 'sqlite3'
|
||||||
|
# gem 'pg' # will also want gem sequel-pg
|
||||||
|
# gem 'mysql2'
|
||||||
|
|
||||||
# change to gunicorn or passenger if you prefer:
|
# change to gunicorn or passenger if you prefer:
|
||||||
gem 'puma'
|
gem 'puma'
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
For the single file app template, really the only file you need is the config.ru and run it with puma.
|
For the single file app template, really the only file you need is the config.ru and run it with puma.
|
||||||
|
|
||||||
|
## Database
|
||||||
|
|
||||||
## Views
|
## Views
|
||||||
|
|
||||||
In this template, I've taken the single-file template and split up the config.ru to put routes and app logic in app.rb and call on templates to render proper html pages. `tilt` and `erubi` gems are added to do the rendering, and `rack-unreloader` gem is added to have puma automatically load file changes while running in development mode.
|
In this template, I've taken the single-file template and split up the config.ru to put routes and app logic in app.rb and call on templates to render proper html pages. `tilt` and `erubi` gems are added to do the rendering, and `rack-unreloader` gem is added to have puma automatically load file changes while running in development mode.
|
||||||
|
|
@ -21,7 +23,7 @@ Will need ruby; install it via package manager or a ruby manager like rbenv/ruby
|
||||||
With this example, will basically just ignore the project's Gemfile. Debian 12 has a pretty current ruby version so just using it.
|
With this example, will basically just ignore the project's Gemfile. Debian 12 has a pretty current ruby version so just using it.
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo apt install ruby ruby-rack puma ruby-erubi ruby-tilt
|
sudo apt install ruby ruby-rack puma ruby-erubi ruby-tilt ruby-sequel ruby-sqlite3 rake
|
||||||
sudo gem install roda rack-unreloader
|
sudo gem install roda rack-unreloader
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
33
Rakefile
Normal file
33
Rakefile
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
namespace :db do
|
||||||
|
|
||||||
|
migrator = lambda do |version|
|
||||||
|
require_relative 'db'
|
||||||
|
Sequel.extension :migration
|
||||||
|
Sequel::Migrator.apply(DB, 'migrate', version)
|
||||||
|
end
|
||||||
|
|
||||||
|
namespace :migrate do
|
||||||
|
|
||||||
|
desc "Perform migration up to latest migration available"
|
||||||
|
task :up do
|
||||||
|
migrator.call(nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Perform migration all the way down (erase all data)"
|
||||||
|
task :down do
|
||||||
|
migrator.call(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Erase all data, then bring back to latest migration"
|
||||||
|
task :reset do
|
||||||
|
migrator.call(0)
|
||||||
|
Sequel::Migrator.apply(DB, 'migrate')
|
||||||
|
end
|
||||||
|
|
||||||
|
#desc "Migrate to a specific version"
|
||||||
|
#task :migrate do |version|
|
||||||
|
#end
|
||||||
|
|
||||||
|
end
|
||||||
7
app.rb
7
app.rb
|
|
@ -14,6 +14,12 @@ class App < Roda
|
||||||
view :index
|
view :index
|
||||||
end
|
end
|
||||||
|
|
||||||
|
r.is 'users' do
|
||||||
|
@users = User.order(:id)
|
||||||
|
view :users
|
||||||
|
end
|
||||||
|
|
||||||
|
r.on 'hello' do
|
||||||
r.is String do |name|
|
r.is String do |name|
|
||||||
@page_title = 'A Custom Greeting'
|
@page_title = 'A Custom Greeting'
|
||||||
@name = name.capitalize
|
@name = name.capitalize
|
||||||
|
|
@ -21,4 +27,5 @@ class App < Roda
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@ dev = ENV['RACK_ENV'] == 'development'
|
||||||
|
|
||||||
require 'rack/unreloader'
|
require 'rack/unreloader'
|
||||||
|
|
||||||
Unreloader = Rack::Unreloader.new(:subclasses=>%w'Roda', :reload=>dev){App}
|
Unreloader = Rack::Unreloader.new(:subclasses=>%w'Roda Sequel::Model', reload: dev, autoload: dev){App}
|
||||||
|
Unreloader.require './models.rb'
|
||||||
Unreloader.require './app.rb'
|
Unreloader.require './app.rb'
|
||||||
|
|
||||||
run(dev ? Unreloader : App)
|
run(dev ? Unreloader : App)
|
||||||
|
|
|
||||||
4
db.rb
Normal file
4
db.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
require 'sequel/core'
|
||||||
|
|
||||||
|
DB = Sequel.connect('sqlite://temp.db')
|
||||||
11
migrate/001_create_users_table.rb
Normal file
11
migrate/001_create_users_table.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
Sequel.migration do
|
||||||
|
change do
|
||||||
|
create_table(:users) do
|
||||||
|
primary_key :id
|
||||||
|
String :name, unique: true, null: false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
14
models.rb
Normal file
14
models.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
require_relative 'db'
|
||||||
|
require 'sequel/model'
|
||||||
|
|
||||||
|
if ENV['RACK_ENV'] == 'development'
|
||||||
|
Sequel::Model.cache_associations = false
|
||||||
|
end
|
||||||
|
|
||||||
|
unless defined?(Unreloader)
|
||||||
|
require 'rack/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/, ''))}
|
||||||
3
models/user.rb
Normal file
3
models/user.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
class User < Sequel::Model
|
||||||
|
end
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
<p class="subtitle is-3">
|
<p class="subtitle is-3">
|
||||||
Welcome to my new page!
|
Welcome to my new page! Running in <%= ENV['RACK_ENV'] %> mode!
|
||||||
</p>
|
</p>
|
||||||
|
|
|
||||||
5
views/users.erb
Normal file
5
views/users.erb
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
<% for u in @users do %>
|
||||||
|
<p class="subtitle is-3">
|
||||||
|
Hello, <%= u.name %>! You're #<%= u.id %>!
|
||||||
|
</p>
|
||||||
|
<% end %>
|
||||||
Loading…
Reference in a new issue