Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active February 1, 2018 16:57
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save JoshCheek/f6ab92e7d5bf66692b2a to your computer and use it in GitHub Desktop.
Save JoshCheek/f6ab92e7d5bf66692b2a to your computer and use it in GitHub Desktop.
Rails app in 1 file, running with Seeing Is Believing
gem 'rails', '4.2.1' # prob works on others, too, but this is the one I figured it out on
require "rails"
require 'active_record'
require 'action_controller/railtie'
require 'action_view/railtie'
# ===== Configuration =====
Rails.logger = ActiveRecord::Base.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
class MahApp < Rails::Application
config.cache_classes = true
config.eager_load = false
config.consider_all_requests_local = true
config.serve_static_files = false
config.assets.debug = true
config.session_store :cookie_store, key: '_mah_app_session'
secrets.secret_key_base = "ffaaf61864e14edf3496df94a3ec3a24050495a0dcc8727dba1ebc7cf6594616479c741329a066ce5387fdf9d0205bfebb8a9549cc4d314c6738e62ad3fda676"
initialize!
end
# ===== Schema =====
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = true
create_table :users do |t|
t.string :name
end
end
# ===== Models =====
class User < ActiveRecord::Base
validates_presence_of :name
end
# ===== Routes =====
Rails.application.routes.draw do
resources :users
root 'site#index'
end
ActionDispatch::Routing::RoutesInspector.new(Rails.application.routes.routes).format(ActionDispatch::Routing::ConsoleFormatter.new).lines.map(&:chomp)
# => [" Prefix Verb URI Pattern Controller#Action",
# " users GET /users(.:format) users#index",
# " POST /users(.:format) users#create",
# " new_user GET /users/new(.:format) users#new",
# "edit_user GET /users/:id/edit(.:format) users#edit",
# " user GET /users/:id(.:format) users#show",
# " PATCH /users/:id(.:format) users#update",
# " PUT /users/:id(.:format) users#update",
# " DELETE /users/:id(.:format) users#destroy",
# " root GET / site#index"]
# ===== Hook Into Views At End Of File =====
class EndOfFileTemplates
TEMPLATES = DATA.slice_before { |line| line.start_with? '@@' }
.each_with_object({}) { |(declaration, *rest), templates|
declaration =~ /^@@\s*(.*?)\.html\.(.*?)\s*$/
[$1, $2]
# => ["layouts/application", "erb"]
# ,["users/_form", "erb"]
# ,["users/show", "erb"]
# ,["users/index", "erb"]
# ,["users/new", "erb"]
# ,["users/show", "erb"]
handler = ActionView::Template.handler_for_extension $2
templates[$1] = ActionView::Template.new(rest.join, declaration.strip, handler, {})
}
def find_all(name, prefixes, partial, *)
name = "_#{name}" if partial
[prefixes, name] # => [["users", "application"], "index"], [["layouts"], "users"], [["layouts"], "application"], [["users", "application"], "new"], [["layouts"], "users"], [["layouts"], "application"], [["users", "application"], "_form"], [["users", "application"], "show"], [["layouts"], "users"], [["layouts"], "application"]
prefixes.map { |prefix| TEMPLATES[File.join(prefix, name)] }.compact
end
def find(*args)
find_all(*args).first || raise(MissingTemplate.new(self, *args))
end
def exists?(path, prefixes, *args)
find_all(path, prefixes, *args).any?
end
end
class ActionView::LookupContext
def view_paths=(paths)
@view_paths = EndOfFileTemplates.new
end
end
# ===== Controllers =====
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
class SiteController < ApplicationController
def index
redirect_to users_path
end
end
class UsersController < ApplicationController
def index
@users = User.all
end
def show
@user = User.find params[:id]
end
def new
@user = User.new
end
def edit
@user = User.find params[:id]
end
def create
params
# => {"utf8"=>"✓",
# "authenticity_token"=>
# "AvnCiAoKpyMwlujn99Q1XvJIGcBnEao9zxsisl6pyhV7xkhYDvlNFZmgk2s4oj/sEnmtGbPBm8opsDthBV2tmA==",
# "user"=>{"name"=>"Lilith"},
# "commit"=>"Create User",
# "controller"=>"users",
# "action"=>"create"}
user_params # => {"name"=>"Lilith"}
@user = User.new user_params
if @user.save
redirect_to @user, notice: 'User was successfully created.'
else
render :new
end
end
def update
@user = User.find params[:id]
if @user.update user_params
redirect_to @user, notice: 'User was successfully updated.'
else
render :edit
end
end
def destroy
@user = User.find params[:id]
@user.destroy
redirect_to users_url, notice: 'User was successfully destroyed.'
end
private
def user_params
params.require(:user).permit(:name)
end
end
# ===== Seed Data =====
User.create! name: 'Kirsty'
User.create! name: 'Savannah'
# ===== Use The App =====
require 'capybara'
Capybara.app = Rails.application
session = Capybara.current_session
# visit root, follow redirect to users path
session.visit('/')
session.current_url # => "http://www.example.com/users"
session.status_code # => 200
# click link to get new user form
session.click_on 'New User'
session.current_url # => "http://www.example.com/users/new"
# create the new user
session.fill_in 'Name', with: 'Lilith'
User.pluck :name # => ["Kirsty", "Savannah"]
session.click_on 'Create User'
User.pluck :name # => ["Kirsty", "Savannah", "Lilith"]
# follow redirect to the show
session.current_url # => "http://www.example.com/users/3"
# >> -- create_table(:users)
# >> D, [2015-04-26T06:19:56.079868 #74266] DEBUG -- : (0.3ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar)
# >> -> 0.0024s
# >> D, [2015-04-26T06:19:56.349568 #74266] DEBUG -- : (0.0ms) begin transaction
# >> D, [2015-04-26T06:19:56.352867 #74266] DEBUG -- : SQL (0.1ms) INSERT INTO "users" ("name") VALUES (?) [["name", "Kirsty"]]
# >> D, [2015-04-26T06:19:56.353145 #74266] DEBUG -- : (0.0ms) commit transaction
# >> D, [2015-04-26T06:19:56.353350 #74266] DEBUG -- : (0.0ms) begin transaction
# >> D, [2015-04-26T06:19:56.353705 #74266] DEBUG -- : SQL (0.0ms) INSERT INTO "users" ("name") VALUES (?) [["name", "Savannah"]]
# >> D, [2015-04-26T06:19:56.353898 #74266] DEBUG -- : (0.0ms) commit transaction
# >> D, [2015-04-26T06:19:56.648619 #74266] DEBUG -- :
# >> D, [2015-04-26T06:19:56.648747 #74266] DEBUG -- :
# >> I, [2015-04-26T06:19:56.649303 #74266] INFO -- : Started GET "/" for 127.0.0.1 at 2015-04-26 06:19:56 -0600
# >> I, [2015-04-26T06:19:56.654987 #74266] INFO -- : Processing by SiteController#index as HTML
# >> I, [2015-04-26T06:19:56.655784 #74266] INFO -- : Redirected to http://www.example.com/users
# >> I, [2015-04-26T06:19:56.655898 #74266] INFO -- : Completed 302 Found in 1ms
# >> D, [2015-04-26T06:19:56.657069 #74266] DEBUG -- :
# >> D, [2015-04-26T06:19:56.657122 #74266] DEBUG -- :
# >> I, [2015-04-26T06:19:56.657196 #74266] INFO -- : Started GET "/users" for 127.0.0.1 at 2015-04-26 06:19:56 -0600
# >> I, [2015-04-26T06:19:56.659345 #74266] INFO -- : Processing by UsersController#index as HTML
# >> D, [2015-04-26T06:19:56.684346 #74266] DEBUG -- : User Load (0.2ms) SELECT "users".* FROM "users"
# >> I, [2015-04-26T06:19:56.688422 #74266] INFO -- : Rendered @@ users/index.html.erb (22.1ms)
# >> I, [2015-04-26T06:19:56.689851 #74266] INFO -- : Completed 200 OK in 30ms (Views: 29.7ms)
# >> D, [2015-04-26T06:19:56.694186 #74266] DEBUG -- :
# >> D, [2015-04-26T06:19:56.694244 #74266] DEBUG -- :
# >> I, [2015-04-26T06:19:56.694322 #74266] INFO -- : Started GET "/users/new" for 127.0.0.1 at 2015-04-26 06:19:56 -0600
# >> I, [2015-04-26T06:19:56.695209 #74266] INFO -- : Processing by UsersController#new as HTML
# >> I, [2015-04-26T06:19:56.712803 #74266] INFO -- : Rendered @@ users/_form.html.erb (13.5ms)
# >> I, [2015-04-26T06:19:56.712984 #74266] INFO -- : Rendered @@ users/new.html.erb (17.2ms)
# >> I, [2015-04-26T06:19:56.713353 #74266] INFO -- : Completed 200 OK in 18ms (Views: 17.8ms)
# >> D, [2015-04-26T06:19:56.715458 #74266] DEBUG -- : (0.1ms) SELECT "users"."name" FROM "users"
# >> D, [2015-04-26T06:19:56.719941 #74266] DEBUG -- :
# >> D, [2015-04-26T06:19:56.719987 #74266] DEBUG -- :
# >> I, [2015-04-26T06:19:56.720098 #74266] INFO -- : Started POST "/users" for 127.0.0.1 at 2015-04-26 06:19:56 -0600
# >> I, [2015-04-26T06:19:56.720767 #74266] INFO -- : Processing by UsersController#create as HTML
# >> I, [2015-04-26T06:19:56.720833 #74266] INFO -- : Parameters: {"utf8"=>"✓", "authenticity_token"=>"AvnCiAoKpyMwlujn99Q1XvJIGcBnEao9zxsisl6pyhV7xkhYDvlNFZmgk2s4oj/sEnmtGbPBm8opsDthBV2tmA==", "user"=>{"name"=>"Lilith"}, "commit"=>"Create User"}
# >> D, [2015-04-26T06:19:56.722157 #74266] DEBUG -- : (0.1ms) begin transaction
# >> D, [2015-04-26T06:19:56.722826 #74266] DEBUG -- : SQL (0.1ms) INSERT INTO "users" ("name") VALUES (?) [["name", "Lilith"]]
# >> D, [2015-04-26T06:19:56.723103 #74266] DEBUG -- : (0.0ms) commit transaction
# >> I, [2015-04-26T06:19:56.723725 #74266] INFO -- : Redirected to http://www.example.com/users/3
# >> I, [2015-04-26T06:19:56.723826 #74266] INFO -- : Completed 302 Found in 3ms
# >> D, [2015-04-26T06:19:56.725100 #74266] DEBUG -- :
# >> D, [2015-04-26T06:19:56.725161 #74266] DEBUG -- :
# >> I, [2015-04-26T06:19:56.725276 #74266] INFO -- : Started GET "/users/3" for 127.0.0.1 at 2015-04-26 06:19:56 -0600
# >> I, [2015-04-26T06:19:56.726342 #74266] INFO -- : Processing by UsersController#show as HTML
# >> I, [2015-04-26T06:19:56.726395 #74266] INFO -- : Parameters: {"id"=>"3"}
# >> D, [2015-04-26T06:19:56.744432 #74266] DEBUG -- : User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 3]]
# >> I, [2015-04-26T06:19:56.746130 #74266] INFO -- : Rendered @@ users/show.html.erb (1.1ms)
# >> I, [2015-04-26T06:19:56.747018 #74266] INFO -- : Completed 200 OK in 21ms (Views: 2.3ms)
# >> D, [2015-04-26T06:19:56.748011 #74266] DEBUG -- : (0.1ms) SELECT "users"."name" FROM "users"
__END__
@@ layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>MahApp</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
@@ users/_form.html.erb
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
@@ users/show.html.erb
<h1>Editing User</h1>
<%= render 'form' %>
<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>
@@ users/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Users</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
@@ users/new.html.erb
<h1>New User</h1>
<%= render 'form' %>
<%= link_to 'Back', users_path %>
@@ users/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @user.name %>
</p>
<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment