Skip to content

Commit 9dcee95

Browse files
committed
Add PGHero
1 parent b4ca80e commit 9dcee95

11 files changed

+38
-18
lines changed

Gemfile

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
source 'https://rubygems.org'
2-
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
32

43
ruby '~> 3.1.4'
54

@@ -12,7 +11,7 @@ gem 'puma', '~> 5.0'
1211
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
1312
# gem 'jbuilder', '~> 2.7'
1413
# Use Redis adapter to run Action Cable in production
15-
# gem 'redis', '~> 4.0'
14+
gem 'redis', '~> 4.0'
1615
# Use Active Model has_secure_password
1716
# gem 'bcrypt', '~> 3.1.7'
1817

@@ -33,19 +32,23 @@ gem "sass-rails"
3332
gem "uglifier"
3433
gem "geocoder"
3534

35+
gem "pghero"
36+
3637
source "https://rails-assets.org" do
3738
gem "rails-assets-bootswatch-sass", "< 4.0.0"
3839
gem "rails-assets-jquery"
3940
end
4041

4142
group :development do
4243
gem 'listen', '~> 3.3'
43-
gem "debug", ">= 1.0.0"
4444
end
4545

4646
group :test do
4747
gem "rspec-rails", "~> 5.0.0"
4848
gem "factory_bot_rails", "~> 6.2.0"
49+
end
50+
51+
group :development, :test do
4952
gem "debug", ">= 1.0.0"
5053
end
5154

Gemfile.lock

+5
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ GEM
168168
non-stupid-digest-assets (1.0.10)
169169
sprockets (>= 2.0)
170170
pg (1.5.4)
171+
pghero (3.4.0)
172+
activerecord (>= 6)
171173
psych (5.1.2)
172174
stringio
173175
puma (5.6.8)
@@ -216,6 +218,7 @@ GEM
216218
execjs
217219
railties (>= 3.2)
218220
tilt
221+
redis (4.8.1)
219222
reline (0.4.2)
220223
io-console (~> 0.5)
221224
rspec-core (3.12.2)
@@ -284,11 +287,13 @@ DEPENDENCIES
284287
listen (~> 3.3)
285288
non-stupid-digest-assets
286289
pg (~> 1.1)
290+
pghero
287291
puma (~> 5.0)
288292
rails (~> 6.1.7, >= 6.1.7.6)
289293
rails-assets-bootswatch-sass (< 4.0.0)!
290294
rails-assets-jquery!
291295
react-rails
296+
redis (~> 4.0)
292297
rspec-rails (~> 5.0.0)
293298
sass-rails
294299
uglifier

app/controllers/home_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ def index
33
list_size = 4
44

55
@pets = {
6-
recently_added: Pet.all.order(created_at: :desc).limit(list_size),
6+
recently_added: Pet.recent.limit(list_size),
77
recently_adopted: Pet.all.order(adoption_date: :desc).limit(list_size),
8-
random: Pet.all.order(Arel.sql('RANDOM()')).limit(list_size),
8+
random: Pet.random.limit(list_size),
99
most_viewed: Pet.all.most_viewed.limit(list_size),
1010
}
1111
end

app/controllers/pets_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def cats
3232
end
3333

3434
def random
35-
@pet = Pet.order("random()").first
35+
@pet = Pet.random.first
3636
redirect_to pet_path(@pet)
3737
end
3838

app/models/pet.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Pet < ApplicationRecord
1717

1818
scope :recent, -> { order("created_at DESC") }
1919
scope :available, -> { where(adoption_date: nil) }
20+
scope :random, -> { order("RANDOM()") }
2021
scope :search, ->(search) { where("name ILIKE :search OR breed ILIKE :search", { search: "%#{search}%" }) }
2122

2223
scope :dogs, -> { where(pet_type: "dog") }

config/application.rb

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class Application < Rails::Application
2525
controller_specs: false
2626
)
2727

28-
2928
def database_is_not_empty(override = false)
3029
return false if override
3130
@database_is_not_empty ||= (Pet.count != 0)

config/database.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
default: &default
1818
adapter: postgresql
1919
encoding: unicode
20+
host: db
21+
username: postgres
22+
password: postgres
2023
# For details on connection pooling, see Rails configuration guide
2124
# https://guides.rubyonrails.org/configuring.html#database-pooling
2225
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
@@ -36,5 +39,3 @@ production:
3639
<<: *default
3740
host: db
3841
database: adopter_production
39-
username: postgres
40-
password: postgres

config/routes.rb

+12-8
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616
resources :pets, only: [:index]
1717
end
1818

19+
20+
21+
# workshop helper routes
22+
23+
post '/workshop/generate_data', to: -> (env) do
24+
GenerateDataJob.perform_later(
25+
(env['rack.request.form_hash']['data_generation_loop_size'] || 100).to_i
26+
)
27+
[303, { 'Location' => '/' }, ['Redirecting...']]
28+
end
29+
1930
namespace :mock do
2031
get '/slow-service', to: '/mock#slow_service'
2132
get '/outlier/:id', to: '/mock#outlier'
2233
end
2334

24-
# workshop helper routes
25-
26-
post '/workshop/generate_data', to: -> (env) do
27-
GenerateDataJob.perform_later(
28-
(env['rack.request.form_hash']['data_generation_loop_size'] || 100).to_i
29-
)
30-
[303, { 'Location' => '/' }, ['Redirecting...']]
31-
end
35+
mount PgHero::Engine, at: "pghero"
3236
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class EnablePgStat < ActiveRecord::Migration[6.1]
2+
def change
3+
enable_extension 'pg_stat_statements'
4+
end
5+
end

db/schema.rb

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.stack.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
POSTGRES_USER: postgres
88
POSTGRES_PASSWORD: postgres
99
POSTGRES_DB: adopter_production
10+
command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all
1011
ports:
1112
- 5432:5432
1213

0 commit comments

Comments
 (0)