Last active
May 16, 2023 15:16
-
-
Save westonganger/e149b610c7985a3ee2420efa41ed2754 to your computer and use it in GitHub Desktop.
Single File Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### BUNDLER | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
ruby '2.4.1' | |
gem "rails" | |
gem "sqlite3" | |
#gem 'responders' | |
#gem "sassc-rails" | |
#gem "uglifier" | |
#gem 'nokogiri' | |
#gem 'slim' | |
#gem 'simple_form' | |
group :development do | |
gem "byebug" | |
#gem 'binding_of_caller' | |
#gem 'better_errors' | |
end | |
group :test do | |
gem 'rails-controller-testing' | |
gem 'minitest-reporters', require: false | |
#gem 'connection_pool', require: false | |
#gem 'database_cleaner', require: false | |
end | |
end | |
require "rails/all" | |
# OR individually choosing specific needed modules only | |
# [ | |
# 'active_record/railtie', | |
# 'action_controller/railtie', | |
# 'action_view/railtie', | |
# 'action_mailer/railtie', | |
# 'active_job/railtie', | |
# 'action_cable/engine', | |
# 'rails/test_unit/railtie', | |
# 'sprockets/railtie' | |
# ].each do |railtie| | |
# begin | |
# require railtie | |
# rescue LoadError | |
# end | |
# end | |
### DATABASE / SCHEMA | |
db = "database.sqlite3" | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: db) | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
start_new = !File.exist?(db) | |
start_new = true # comment out if you dont always want a fresh db | |
if start_new | |
ActiveRecord::Schema.define do | |
if start_new || !ActiveRecord::Base.connection.table_exists?('books') | |
create_table :books, force: true do |t| | |
t.string :name | |
t.references :category | |
t.boolean :active, default: false, null: false | |
t.timestamps | |
end | |
end | |
if start_new || !ActiveRecord::Base.connection.table_exists?('categories') | |
create_table :categories, force: true do |t| | |
t.string :name | |
t.timestamps | |
end | |
end | |
end | |
end | |
### CONFIG / ROUTES | |
class SingleFileApp < Rails::Application | |
secrets.secret_token = "secret_token" | |
secrets.secret_key_base = "secret_key_base" | |
config.logger = Logger.new($stdout) | |
Rails.logger = config.logger | |
routes.draw do | |
resources :books | |
root to: "books#index" | |
end | |
end | |
### MODELS | |
class Book < ActiveRecord::Base | |
belongs_to :category | |
validates :name, presence: true, uniqueness: {case_sensitive: false} | |
end | |
class Category < ActiveRecord::Base | |
has_many :categories | |
validates :name, presence: true, uniqueness: {case_sensitive: false} | |
end | |
### CONTROLLERS | |
class BooksController < ActionController::Base | |
include Rails.application.routes.url_helpers | |
def index | |
@books = Book.all | |
render inline: "# of books: <%= @books.count %>" | |
# TODO how to render slim files | |
end | |
end | |
### TESTING | |
#require "minitest/autorun" # manually calling run below | |
require 'minitest/reporters' | |
Minitest::Reporters.use! | |
class BookTest < Minitest::Test | |
def test_books | |
Book.create!(name: 'foo', category_id: 1) | |
assert_equal Book.count, 1 | |
end | |
end | |
class BooksControllerTest < Minitest::Test | |
include Rack::Test::Methods | |
def test_index | |
get "/books" | |
assert last_response.ok? | |
assert last_response.body.include?("# of books") | |
end | |
private | |
def app | |
Rails.application | |
end | |
end | |
exit_code = Minitest.run | |
if !exit_code ### this if logic looks backwards, to be confirmed? | |
exit(exit_code) | |
end | |
### SERVE REQUESTS | |
require "rails/command" | |
require "rails/commands/server/server_command" | |
Rails::Server.new({ | |
app: SingleFileApp, | |
Host: (ENV["HOST"] || "0.0.0.0"), | |
Port: (ENV["PORT"] || 3000), | |
}).start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment