Compare commits

..

3 commits

Author SHA1 Message Date
James 14f1f3daa6 Added new views that use the database 2023-12-28 22:39:02 -06:00
James bffbab8fbc Added sequel with example setup 2023-12-28 18:36:49 -06:00
James 1a3a4287b0 Getting ready to use database 2023-12-28 00:13:50 -06:00
12 changed files with 96 additions and 7 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/.rake_tasks~
/temp.db

View file

@ -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'

View file

@ -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
View 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
View file

@ -14,11 +14,18 @@ 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
view :greeting view :greeting
end end
end end
end
end end

View file

@ -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
View file

@ -0,0 +1,4 @@
# frozen_string_literal: true
require 'sequel/core'
DB = Sequel.connect('sqlite://temp.db')

View 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
View 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
View file

@ -0,0 +1,3 @@
# frozen_string_literal: true
class User < Sequel::Model
end

View file

@ -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
View file

@ -0,0 +1,5 @@
<% for u in @users do %>
<p class="subtitle is-3">
Hello, <%= u.name %>! You're #<%= u.id %>!
</p>
<% end %>