From 2543c5444650009fdbd8fe0634852ca6b7acda1c Mon Sep 17 00:00:00 2001 From: James Date: Wed, 27 Dec 2023 23:47:16 -0600 Subject: [PATCH 01/17] Update readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 83f577f..1a574b7 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ This has puma reload with any file changes while you are working on the app, whe . -Basically, you require the app from the config.ru, and then within you app files, any "require_relative" will be "Unreloader.require" instead, plus you do need to include the file extension. +Basically, you require the app from the config.ru, and then within you app files, any "require_relative" will be "Unreloader.require" instead, plus you do need to include the file extension when using Unreloader.require. In config.ru, the `dev =` line stores true or false depending on the RACK_ENV environment variable (see above for changing from default development). Then the `:reload=>dev` uses the dev variable to tell Unreload to reload files or not. Lastly the `run` command uses the dev variable to choose between running Unreloader, or bypass Unreloader and run App like normal in production. From 1a3a4287b00acc054b1ef07526a569cd4b504939 Mon Sep 17 00:00:00 2001 From: James Dinkel Date: Thu, 28 Dec 2023 00:13:50 -0600 Subject: [PATCH 02/17] Getting ready to use database --- Gemfile | 6 ++++++ README.md | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 3606de8..6e8b26c 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,12 @@ gem 'roda' gem 'rack-unreloader' gem 'tilt' gem 'erubi' +gem 'sequel' + +# 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: gem 'puma' diff --git a/README.md b/README.md index 83f577f..e8d83d1 100644 --- a/README.md +++ b/README.md @@ -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. +## Database + ## 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. @@ -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. ``` -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 sudo gem install roda rack-unreloader ``` From bffbab8fbc8e0691f3d8375531a32ddb597d1edd Mon Sep 17 00:00:00 2001 From: James Dinkel Date: Thu, 28 Dec 2023 18:36:49 -0600 Subject: [PATCH 03/17] Added sequel with example setup --- Gemfile | 1 + README.md | 2 +- Rakefile | 33 +++++++++++++++++++++++++++++++ config.ru | 3 ++- db.rb | 4 ++++ migrate/001_create_users_table.rb | 11 +++++++++++ models.rb | 14 +++++++++++++ models/user.rb | 3 +++ views/index.erb | 2 +- 9 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 Rakefile create mode 100644 db.rb create mode 100644 migrate/001_create_users_table.rb create mode 100644 models.rb create mode 100644 models/user.rb diff --git a/Gemfile b/Gemfile index 6e8b26c..b668a39 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ gem 'rack-unreloader' gem 'tilt' gem 'erubi' gem 'sequel' +gem 'rake' # Change to whatever database you plan to use gem 'sqlite3' diff --git a/README.md b/README.md index e8d83d1..159cce8 100644 --- a/README.md +++ b/README.md @@ -23,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. ``` -sudo apt install ruby ruby-rack puma ruby-erubi ruby-tilt ruby-sequel ruby-sqlite3 +sudo apt install ruby ruby-rack puma ruby-erubi ruby-tilt ruby-sequel ruby-sqlite3 rake sudo gem install roda rack-unreloader ``` diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..4a4ce07 --- /dev/null +++ b/Rakefile @@ -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 diff --git a/config.ru b/config.ru index 4e24994..1af6d15 100644 --- a/config.ru +++ b/config.ru @@ -3,7 +3,8 @@ dev = ENV['RACK_ENV'] == 'development' 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' run(dev ? Unreloader : App) diff --git a/db.rb b/db.rb new file mode 100644 index 0000000..5c72eb9 --- /dev/null +++ b/db.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true +require 'sequel/core' + +DB = Sequel.connect('sqlite://temp.db') diff --git a/migrate/001_create_users_table.rb b/migrate/001_create_users_table.rb new file mode 100644 index 0000000..29d513d --- /dev/null +++ b/migrate/001_create_users_table.rb @@ -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 + diff --git a/models.rb b/models.rb new file mode 100644 index 0000000..33c6865 --- /dev/null +++ b/models.rb @@ -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/, ''))} diff --git a/models/user.rb b/models/user.rb new file mode 100644 index 0000000..a4f2fa1 --- /dev/null +++ b/models/user.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true +class User < Sequel::Model +end diff --git a/views/index.erb b/views/index.erb index 4762d4c..31b3b2d 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,3 +1,3 @@

- Welcome to my new page! + Welcome to my new page! Running in <%= ENV['RACK_ENV'] %> mode!

From 14f1f3daa64dcdc56d6cdf2607da3857577f6055 Mon Sep 17 00:00:00 2001 From: James Dinkel Date: Thu, 28 Dec 2023 22:39:02 -0600 Subject: [PATCH 04/17] Added new views that use the database --- .gitignore | 2 ++ app.rb | 15 +++++++++++---- views/users.erb | 5 +++++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 views/users.erb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb44c0a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.rake_tasks~ +/temp.db diff --git a/app.rb b/app.rb index 76eeb17..8c321e0 100644 --- a/app.rb +++ b/app.rb @@ -14,10 +14,17 @@ class App < Roda view :index end - r.is String do |name| - @page_title = 'A Custom Greeting' - @name = name.capitalize - view :greeting + r.is 'users' do + @users = User.order(:id) + view :users + end + + r.on 'hello' do + r.is String do |name| + @page_title = 'A Custom Greeting' + @name = name.capitalize + view :greeting + end end end end diff --git a/views/users.erb b/views/users.erb new file mode 100644 index 0000000..13cc1e7 --- /dev/null +++ b/views/users.erb @@ -0,0 +1,5 @@ +<% for u in @users do %> +

+ Hello, <%= u.name %>! You're #<%= u.id %>! +

+<% end %> From 5a91e0d0ddc6a74bcac0f568f4946b599cbddcbb Mon Sep 17 00:00:00 2001 From: james Date: Fri, 1 Nov 2024 14:52:56 -0500 Subject: [PATCH 05/17] Expanded setup instructions in Readme. --- README.md | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 159cce8..ca3abc2 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,17 @@ Next Roda app template iterations will add database, then authentication. ## Setup +### Get git + +``` +sudo apt install git +git config --global user.email "myemail@gmail.com" +git config --global user.name "Full Name" +git config --global credential.helper "cache" + +git clone https://path/to/project.git +``` + ### Prereq installs Will need ruby; install it via package manager or a ruby manager like rbenv/ruby-build. Will need the roda gem, and then an application server such as puma (recommended), gunicorn, or passenger. My examples will use system ruby and puma. @@ -25,18 +36,41 @@ With this example, will basically just ignore the project's Gemfile. Debian 12 ``` sudo apt install ruby ruby-rack puma ruby-erubi ruby-tilt ruby-sequel ruby-sqlite3 rake sudo gem install roda rack-unreloader + +cd my-project ``` -#### Option 2: Bundler +#### Option 2a: Bundler system package -Run the bundle command from the project's root directory +Run the bundle install command from the project's root directory ``` -sudo apt install ruby ruby-bundler +sudo apt install ruby ruby-bundler ruby-dev gcc pkgconf make g++ bundle config set --local path 'vendor/bundle' + +cd my-project bundle install ``` +#### Option 2b: Bundler system gem (recommended) + +Similar to above but might as well use the gem install to get the latest bundler. The Debian apt packaged bundler is currently a bit outdated and missing some features compared to the latest. + +``` +sudo apt install ruby ruby-dev gcc pkgconf make g++ +echo 'gem: --no-document' >> ~/.gemrc +sudo cp ~/.gemrc /root/ +sudo gem install bundler +bundle config set --local path 'vendor/bundle' + +cd my-project +bundle install +``` + +#### Option 3: Rbenv Ruby + +Todo + ## Run it In the project root directory: From c05c3d3a89973d75e657aea520b065fa6901714d Mon Sep 17 00:00:00 2001 From: james Date: Fri, 1 Nov 2024 15:10:17 -0500 Subject: [PATCH 06/17] Update views/layout.erb for bulma 1.0.2 --- views/layout.erb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/views/layout.erb b/views/layout.erb index c5ec841..3ab9311 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -1,16 +1,16 @@ - + <%= @page_title || "My Website" %> - +
-

<%= @page_title || 'Page Title Placeholder' %>

- <% # maybe put a flash section here some day %> +

<%= @page_title || 'Page Title Placeholder' %>

+ <% # maybe put a flash section here some day, what Bulma calls the Hero component I think %> <%== yield %>
From 57462f98fcfc8fd7f6154025ad0af824e45fceff Mon Sep 17 00:00:00 2001 From: james Date: Fri, 1 Nov 2024 15:43:52 -0500 Subject: [PATCH 07/17] Update README.md --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ca3abc2..8012f40 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,10 @@ Run the bundle install command from the project's root directory ``` sudo apt install ruby ruby-bundler ruby-dev gcc pkgconf make g++ -bundle config set --local path 'vendor/bundle' +echo 'gem: --no-document' >> ~/.gemrc +sudo cp ~/.gemrc /root/ +bundle config set --global path 'vendor/bundle' +sudo cp -r .bundle /root/ cd my-project bundle install @@ -61,7 +64,8 @@ sudo apt install ruby ruby-dev gcc pkgconf make g++ echo 'gem: --no-document' >> ~/.gemrc sudo cp ~/.gemrc /root/ sudo gem install bundler -bundle config set --local path 'vendor/bundle' +bundle config set --global path 'vendor/bundle' +sudo cp -r .bundle /root/ cd my-project bundle install From 10bcb49b56e4434b5df8d72bd0dd0f206123049f Mon Sep 17 00:00:00 2001 From: james Date: Fri, 1 Nov 2024 15:50:21 -0500 Subject: [PATCH 08/17] Update README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8012f40..c36f07e 100644 --- a/README.md +++ b/README.md @@ -80,12 +80,18 @@ Todo In the project root directory: ``` +bundle exec puma + +# or if you did not use bundler to install puma... puma ``` This default to development mode. Run it in production mode with: ``` +RACK_ENV=production bundle exec puma + +# or if you did not install puma RACK_ENV=production puma ``` @@ -93,7 +99,7 @@ For development, just run it like that. For production, probably want to set up ### Run it with systemd in production -Copy the example myapp.service file to `/etc/systemd/system/` and edit accordingly. The example assumes a user named "myapp" with a group name "myapp", the application files are in `/opt/myapp/`, and puma is the system puma. +Copy the example myapp.service file to `/etc/systemd/system/` and edit accordingly. The example assumes a user named "myapp" with a group name "myapp", the application files are in `/opt/myapp/`, and puma is the system puma. If you installed puma with bundler, the exe will be at `/opt/myapp/vendor/bundle/ruby/3.1.0/bin/puma`. ### Notes From 3d679586d80a3763f4fa6844ad9c2981fccf510f Mon Sep 17 00:00:00 2001 From: james Date: Fri, 1 Nov 2024 16:59:26 -0500 Subject: [PATCH 09/17] Update views/greeting.erb --- views/greeting.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/greeting.erb b/views/greeting.erb index 9de747b..15c8d7a 100644 --- a/views/greeting.erb +++ b/views/greeting.erb @@ -1,3 +1,3 @@ -

+

Hello, <%= @name %>!

From 15e6c8dff89f251974bb01bf87c83e3c14654287 Mon Sep 17 00:00:00 2001 From: james Date: Fri, 1 Nov 2024 17:00:55 -0500 Subject: [PATCH 10/17] Update views/index.erb --- views/index.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/index.erb b/views/index.erb index 31b3b2d..25704e6 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,3 +1,3 @@ -

+

Welcome to my new page! Running in <%= ENV['RACK_ENV'] %> mode!

From 9f00a34f7db27f465046ef3e62be01d2b41f5dad Mon Sep 17 00:00:00 2001 From: james Date: Fri, 1 Nov 2024 17:01:38 -0500 Subject: [PATCH 11/17] Update views/layout.erb --- views/layout.erb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/views/layout.erb b/views/layout.erb index 3ab9311..b18438a 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -9,8 +9,9 @@
-

<%= @page_title || 'Page Title Placeholder' %>

- <% # maybe put a flash section here some day, what Bulma calls the Hero component I think %> +

+ <%= @page_title || 'Page Title Placeholder' %> +

<%== yield %>
From 4f3d003a35529d61c98d1f942dca0ebe7b478f66 Mon Sep 17 00:00:00 2001 From: james Date: Fri, 1 Nov 2024 17:03:48 -0500 Subject: [PATCH 12/17] Update views/users.erb --- views/users.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/users.erb b/views/users.erb index 13cc1e7..7fd2caf 100644 --- a/views/users.erb +++ b/views/users.erb @@ -1,5 +1,5 @@ <% for u in @users do %> -

+

Hello, <%= u.name %>! You're #<%= u.id %>!

<% end %> From 0cd5093976a73f793a182b12894d827385775fb5 Mon Sep 17 00:00:00 2001 From: james Date: Sat, 2 Nov 2024 22:36:20 -0500 Subject: [PATCH 13/17] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cb44c0a..4530e79 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.rake_tasks~ /temp.db +/vendor/ \ No newline at end of file From 2024a8367bc31fca7ae044ed063fce1c2a9ec074 Mon Sep 17 00:00:00 2001 From: james Date: Sat, 2 Nov 2024 23:08:12 -0500 Subject: [PATCH 14/17] link to latest bulma 1.x --- views/layout.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/layout.erb b/views/layout.erb index b18438a..4869da1 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -4,7 +4,7 @@ <%= @page_title || "My Website" %> - +
From a9f4b05b1625c0616b48d80ab652fd82757bb571 Mon Sep 17 00:00:00 2001 From: james Date: Mon, 4 Nov 2024 23:25:40 -0600 Subject: [PATCH 15/17] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c36f07e..aee5c23 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ cd my-project Run the bundle install command from the project's root directory ``` -sudo apt install ruby ruby-bundler ruby-dev gcc pkgconf make g++ +sudo apt install ruby ruby-bundler ruby-dev gcc pkgconf make g++ libyaml-dev echo 'gem: --no-document' >> ~/.gemrc sudo cp ~/.gemrc /root/ bundle config set --global path 'vendor/bundle' @@ -60,7 +60,7 @@ bundle install Similar to above but might as well use the gem install to get the latest bundler. The Debian apt packaged bundler is currently a bit outdated and missing some features compared to the latest. ``` -sudo apt install ruby ruby-dev gcc pkgconf make g++ +sudo apt install ruby ruby-dev gcc pkgconf make g++ libyaml-dev echo 'gem: --no-document' >> ~/.gemrc sudo cp ~/.gemrc /root/ sudo gem install bundler From 77bdebd0b1be71f8856ad9c59e780a920cde1097 Mon Sep 17 00:00:00 2001 From: james Date: Thu, 7 Nov 2024 17:46:21 -0600 Subject: [PATCH 16/17] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aee5c23..7994bbc 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ cd my-project Run the bundle install command from the project's root directory ``` -sudo apt install ruby ruby-bundler ruby-dev gcc pkgconf make g++ libyaml-dev +sudo apt install ruby ruby-bundler ruby-dev gcc pkgconf make g++ libyaml-dev libffi-dev echo 'gem: --no-document' >> ~/.gemrc sudo cp ~/.gemrc /root/ bundle config set --global path 'vendor/bundle' @@ -60,7 +60,7 @@ bundle install Similar to above but might as well use the gem install to get the latest bundler. The Debian apt packaged bundler is currently a bit outdated and missing some features compared to the latest. ``` -sudo apt install ruby ruby-dev gcc pkgconf make g++ libyaml-dev +sudo apt install ruby ruby-dev gcc pkgconf make g++ libyaml-dev # zlib1g-dev libffi-dev #(for rails stuff) echo 'gem: --no-document' >> ~/.gemrc sudo cp ~/.gemrc /root/ sudo gem install bundler From f4780106996c444622d36e2b8d41ca6a86f87643 Mon Sep 17 00:00:00 2001 From: james Date: Wed, 25 Jun 2025 22:42:56 -0500 Subject: [PATCH 17/17] Update .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4530e79..aa03a48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.rake_tasks~ /temp.db -/vendor/ \ No newline at end of file +/vendor/ +/bin/ \ No newline at end of file