Browse Source

Setup bootstrap basics

* Add sprockets.
* Add sass as processor.
* Ensure sinatra reloads on changes.
* Namespace the app better.
tags/0.3.0^2
Bèr Kessels 9 months ago
parent
commit
31f62f26af

+ 4
- 0
Gemfile View File

@@ -6,14 +6,18 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'event_sourcery'
gem 'event_sourcery-postgres'
gem 'bootstrap', '~> 4.3'
gem 'json_expressions'
gem 'jsonapi-rb'
gem 'nominatim', github: 'lukaszsliwa/nominatim'
gem 'opening_hours_converter'
gem 'rake'
gem 'sass', :require => 'sass'
gem 'semver'
gem 'sequel-postgis-georuby'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'sprockets'

group :development, :test do
gem 'dotenv', '~> 2.6'

+ 90
- 6
Gemfile.lock View File

@@ -9,14 +9,42 @@ GIT
GEM
remote: https://rubygems.org/
specs:
actionpack (5.2.3)
actionview (= 5.2.3)
activesupport (= 5.2.3)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.3)
activesupport (= 5.2.3)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activesupport (5.2.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
ast (2.4.0)
autoprefixer-rails (9.6.1.1)
execjs
awesome_print (1.8.0)
backports (3.15.0)
better_errors (2.5.0)
coderay (>= 1.0.0)
erubi (>= 1.0.0)
rack (>= 0.9.0)
bootstrap (4.3.1)
autoprefixer-rails (>= 9.1.0)
popper_js (>= 1.14.3, < 2)
sassc-rails (>= 2.0.0)
builder (3.2.3)
byebug (10.0.2)
coderay (1.1.2)
concurrent-ruby (1.1.5)
crass (1.0.4)
database_cleaner (1.7.0)
dotenv (2.6.0)
erubi (1.8.0)
@@ -25,11 +53,15 @@ GEM
event_sourcery (>= 0.14.0)
pg
sequel (>= 4.38)
execjs (2.7.0)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
ffi (1.11.1)
foreman (0.85.0)
thor (~> 0.19.1)
georuby (2.5.2)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.2)
json (2.1.0)
json_expressions (0.9.0)
@@ -40,6 +72,9 @@ GEM
jsonapi-renderer (0.2.0)
jsonapi-serializable (0.3.1)
jsonapi-renderer (~> 0.2.0)
loofah (2.2.3)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
method_source (0.9.2)
mini_portile2 (2.3.0)
minitest (5.11.3)
@@ -48,7 +83,7 @@ GEM
nokogiri
multi_json (1.13.1)
multipart-post (2.0.0)
mustermann (1.0.2)
mustermann (1.0.3)
nokogiri (1.8.5)
mini_portile2 (~> 2.3.0)
opening_hours_converter (1.7.20)
@@ -57,17 +92,32 @@ GEM
parser (2.5.0.5)
ast (~> 2.4.0)
pg (1.1.4)
popper_js (1.14.5)
powerpack (0.1.2)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rack (2.0.6)
rack-protection (2.0.5)
rack (2.0.7)
rack-protection (2.0.7)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.2.0)
loofah (~> 2.2, >= 2.2.2)
railties (5.2.3)
actionpack (= 5.2.3)
activesupport (= 5.2.3)
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rainbow (3.0.0)
rake (12.3.2)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
rubocop (0.64.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
@@ -77,19 +127,49 @@ GEM
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.4.0)
ruby-progressbar (1.10.0)
sass (3.7.4)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
sassc (2.2.0)
ffi (~> 1.9)
sassc-rails (2.1.2)
railties (>= 4.0.0)
sassc (>= 2.0)
sprockets (> 3.0)
sprockets-rails
tilt
semver (1.0.1)
sequel (5.21.0)
sequel-postgis-georuby (0.1.2)
georuby (~> 2.5.2)
sequel (~> 5.0)
sinatra (2.0.5)
sinatra (2.0.7)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.5)
rack-protection (= 2.0.7)
tilt (~> 2.0)
sinatra-contrib (2.0.7)
backports (>= 2.8.2)
multi_json
mustermann (~> 1.0)
rack-protection (= 2.0.7)
sinatra (= 2.0.7)
tilt (~> 2.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.19.4)
tilt (2.0.9)
thread_safe (0.3.6)
tilt (2.0.10)
timecop (0.9.1)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.4.1)

PLATFORMS
@@ -98,6 +178,7 @@ PLATFORMS
DEPENDENCIES
awesome_print
better_errors
bootstrap (~> 4.3)
byebug
database_cleaner
dotenv (~> 2.6)
@@ -115,9 +196,12 @@ DEPENDENCIES
rack-test
rake
rubocop
sass
semver
sequel-postgis-georuby
sinatra
sinatra-contrib
sprockets
timecop

BUNDLED WITH

+ 1
- 1
Rakefile View File

@@ -18,7 +18,7 @@ task setup_processors: :environment do
end

desc 'Run Event Stream Processors'
task run_processors: :environment do
task run_processors: [:environment, :event_sourcery] do
# Need to disconnect before starting the processors so
# that the forked processes have their own connection / fork safety.
Hours.projections_database.disconnect

+ 3
- 0
app/assets/stylesheets/application.scss View File

@@ -0,0 +1,3 @@
// Custom bootstrap variables must be set or imported *before* bootstrap.

@import "bootstrap";

+ 17
- 0
app/views/layout.erb View File

@@ -0,0 +1,17 @@
<!doctype html>
<html lang="nl">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<link rel="stylesheet" href="/assets/application.css" type="text/css" media="all">

<title>Openingstijden</title>
</head>
<body>
<div class="container">
<%= yield %>
</div>
</body>
</html>

+ 1
- 1
config.ru View File

@@ -4,4 +4,4 @@ $LOAD_PATH << '.'

require 'lib/app.rb'

run Sinatra::Application
run Hours::Web.new

+ 65
- 38
lib/app.rb View File

@@ -5,6 +5,9 @@ require_relative './hours.rb'
require_relative '../config/event_sourcery.rb'

require 'sinatra'
require 'sinatra/reloader'
require 'sprockets'
require 'bootstrap'

require 'jsonapi/renderer'
require 'jsonapi/serializable'
@@ -12,55 +15,79 @@ require 'jsonapi/serializable'
UnprocessableEntity = Class.new(StandardError)
BadRequest = Class.new(StandardError)

set :public_folder, 'public'
# Ensure our error handlers are triggered in development
set :show_exceptions, :after_handler

set :views, File.join(settings.root, '..', 'app', 'views')

configure :development do
require 'better_errors'
use BetterErrors::Middleware
BetterErrors.application_root = __dir__
end

error UnprocessableEntity do |error|
body "Unprocessable Entity: #{error.message}"
status 422
end
class Hours::Web < Sinatra::Base
# initialize new sprockets environment
set :sprockets_env, Sprockets::Environment.new

error BadRequest do |error|
body "Bad Request: #{error.message}"
status 400
end
configure :development do
register Sinatra::Reloader
end

def json_params
# Coerce this into a symbolised Hash so Sintra data structures
# don't leak into the command layer.
Hash[
params.merge(
JSON.parse(request.body.read)
).map { |k, v| [k.to_sym, v] }
]
end
# append assets paths
sprockets_env.append_path 'app/assets/stylesheets'
sprockets_env.append_path 'app/assets/javascripts'

get '/nodes' do
render = JSONAPI::Serializable::Renderer.new
body render.render(
Hours::Projections::Nodes::Query.handle,
class: { "Hours::Models::Node": Hours::Serializers::Node }
).to_json
status 200
end
# compress assets
sprockets_env.js_compressor = :uglify
sprockets_env.css_compressor = :scss

post '/nodes/:node_id' do
command = Hours::Commands::Node::Add::Command.build(json_params)
Hours::Commands::Node::Add::CommandHandler.new.handle(command)
status 201
end
# Public folder for additional assets
set :public_folder, 'public'
# Ensure our error handlers are triggered in development
set :show_exceptions, :after_handler

set :views, File.join(settings.root, '..', 'app', 'views')

error UnprocessableEntity do |error|
body "Unprocessable Entity: #{error.message}"
status 422
end

error BadRequest do |error|
body "Bad Request: #{error.message}"
status 400
end

def json_params
# Coerce this into a symbolised Hash so Sintra data structures
# don't leak into the command layer.
Hash[
params.merge(
JSON.parse(request.body.read)
).map { |k, v| [k.to_sym, v] }
]
end

# get assets
get '/assets/*' do
env['PATH_INFO'].sub!('/assets', '')
settings.sprockets_env.call(env)
end

get '/nodes' do
render = JSONAPI::Serializable::Renderer.new
body render.render(
Hours::Projections::Nodes::Query.handle,
class: { "Hours::Models::Node": Hours::Serializers::Node }
).to_json
status 200
end

post '/nodes/:node_id' do
command = Hours::Commands::Node::Add::Command.build(json_params)
Hours::Commands::Node::Add::CommandHandler.new.handle(command)
status 201
end

get '/nodes/:node_id' do
erb :node
get '/nodes/:node_id' do
erb :node
end
end

require_relative '../app/serializers/node.rb'

+ 3
- 0
test/integration/web/view_node_test.rb View File

@@ -22,6 +22,9 @@ describe 'web view nodes' do
assert_status(200)
assert_includes last_response.content_type, 'text/html'

assert_have_tag last_response.body, 'html[lang=nl]'

assert_have_tag last_response.body, 'title', 'Openingstijden'
assert_have_tag last_response.body, 'h1.title', 'Openingstijden'
end
end

+ 1
- 1
test/support/request_helpers.rb View File

@@ -8,7 +8,7 @@ module RequestHelpers
include Rack::Test::Methods

def app
Sinatra::Application
Hours::Web.new
end

def post_json(url, body, headers = {})

Loading…
Cancel
Save