Skip to content

Commit b02cba5

Browse files
authored
Merge pull request #236 from psu-libraries/229-rails-admin
set up rails admin dashboard
2 parents 6a4491b + 9ff7c03 commit b02cba5

File tree

14 files changed

+243
-17
lines changed

14 files changed

+243
-17
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ inherit_gem:
44
Metrics/BlockLength:
55
Exclude:
66
- 'config/environments/development.rb'
7+
- 'app/models/api_user.rb'
8+
- 'app/models/job.rb'
9+
- 'config/routes.rb'
710

811
Metrics/CyclomaticComplexity:
912
Exclude:

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ gem 'rails', '~> 7.2.2', '>= 7.2.2.1'
99

1010
gem 'shakapacker'
1111
# Use mysql as the database for Active Record
12-
gem 'mysql2'
12+
gem 'mysql2', '~> 0.5.7'
1313
# Use the Puma web server [https://github.com/puma/puma]
1414
gem 'puma', '>= 5.0'
1515
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
@@ -78,6 +78,8 @@ group :test do
7878
gem 'shoulda-matchers'
7979
end
8080

81+
gem 'rails_admin'
8182
gem 'rails_warden', '~> 0.6.0'
8283

8384
gem 'bugsnag', '~> 6.28'
85+
gem 'sassc-rails'

Gemfile.lock

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ GEM
5050
globalid (>= 0.3.6)
5151
activemodel (7.2.2.2)
5252
activesupport (= 7.2.2.2)
53+
activemodel-serializers-xml (1.0.3)
54+
activemodel (>= 5.0.0.a)
55+
activesupport (>= 5.0.0.a)
56+
builder (~> 3.1)
5357
activerecord (7.2.2.2)
5458
activemodel (= 7.2.2.2)
5559
activesupport (= 7.2.2.2)
@@ -131,6 +135,7 @@ GEM
131135
concurrent-ruby (1.3.5)
132136
connection_pool (2.5.4)
133137
crass (1.0.6)
138+
csv (3.3.5)
134139
date (3.4.1)
135140
debug (1.11.0)
136141
irb (~> 1.10)
@@ -185,6 +190,18 @@ GEM
185190
json-schema (5.2.2)
186191
addressable (~> 2.8)
187192
bigdecimal (~> 3.1)
193+
kaminari (1.2.2)
194+
activesupport (>= 4.1.0)
195+
kaminari-actionview (= 1.2.2)
196+
kaminari-activerecord (= 1.2.2)
197+
kaminari-core (= 1.2.2)
198+
kaminari-actionview (1.2.2)
199+
actionview
200+
kaminari-core (= 1.2.2)
201+
kaminari-activerecord (1.2.2)
202+
activerecord
203+
kaminari-core (= 1.2.2)
204+
kaminari-core (1.2.2)
188205
language_server-protocol (3.17.0.5)
189206
lint_roller (1.1.0)
190207
logger (1.7.0)
@@ -205,6 +222,7 @@ GEM
205222
msgpack (1.8.0)
206223
mysql2 (0.5.7)
207224
bigdecimal
225+
nested_form (0.3.2)
208226
net-http (0.6.0)
209227
uri
210228
net-imap (0.5.10)
@@ -283,6 +301,13 @@ GEM
283301
rails-html-sanitizer (1.6.2)
284302
loofah (~> 2.21)
285303
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
304+
rails_admin (3.3.0)
305+
activemodel-serializers-xml (>= 1.0)
306+
csv
307+
kaminari (>= 0.14, < 2.0)
308+
nested_form (~> 0.3)
309+
rails (>= 6.0, < 9)
310+
turbo-rails (>= 1.0, < 3)
286311
rails_warden (0.6.0)
287312
warden (>= 1.2.0)
288313
railties (7.2.2.2)
@@ -388,6 +413,14 @@ GEM
388413
sass-listen (4.0.0)
389414
rb-fsevent (~> 0.9, >= 0.9.4)
390415
rb-inotify (~> 0.9, >= 0.9.7)
416+
sassc (2.4.0)
417+
ffi (~> 1.9)
418+
sassc-rails (2.1.2)
419+
railties (>= 4.0.0)
420+
sassc (>= 2.0)
421+
sprockets (> 3.0)
422+
sprockets-rails
423+
tilt
391424
scss_lint (0.60.0)
392425
sass (~> 3.5, >= 3.5.5)
393426
securerandom (0.4.1)
@@ -413,11 +446,23 @@ GEM
413446
rack (>= 3.1.0)
414447
redis-client (>= 0.23.2)
415448
smart_properties (1.17.0)
449+
sprockets (4.2.2)
450+
concurrent-ruby (~> 1.0)
451+
logger
452+
rack (>= 2.2.4, < 4)
453+
sprockets-rails (3.5.2)
454+
actionpack (>= 6.1)
455+
activesupport (>= 6.1)
456+
sprockets (>= 3.0.0)
416457
stimulus-rails (1.3.4)
417458
railties (>= 6.0.0)
418459
stringio (3.1.7)
419460
thor (1.4.0)
461+
tilt (2.7.0)
420462
timeout (0.4.3)
463+
turbo-rails (2.0.23)
464+
actionpack (>= 7.1.0)
465+
railties (>= 7.1.0)
421466
tzinfo (2.0.6)
422467
concurrent-ruby (~> 1.0)
423468
unicode-display_width (3.2.0)
@@ -462,17 +507,19 @@ DEPENDENCIES
462507
faraday
463508
image_processing
464509
jbuilder
465-
mysql2
510+
mysql2 (~> 0.5.7)
466511
niftany
467512
puma (>= 5.0)
468513
rails (~> 7.2.2, >= 7.2.2.1)
514+
rails_admin
469515
rails_warden (~> 0.6.0)
470516
redis (>= 4.0.1)
471517
rspec-rails
472518
rswag-api
473519
rswag-specs
474520
rswag-ui
475521
rubocop-rails-omakase
522+
sassc-rails
476523
selenium-webdriver
477524
shakapacker
478525
shoulda-matchers

app/assets/config/manifest.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
//= link_tree ../images
2-
//= link_directory ../stylesheets .css
1+
// No additional asset directories to link

app/models/api_user.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,54 @@ class APIUser < ApplicationRecord
1111
validates :webhook_endpoint, presence: true
1212
validates :webhook_endpoint, format: { with: URI::RFC2396_PARSER.make_regexp('https') }
1313

14+
RailsAdmin.config do |config|
15+
config.model 'ApiUser' do
16+
list do
17+
field :id
18+
field :name
19+
field :email
20+
field :created_at
21+
field :updated_at
22+
end
23+
24+
show do
25+
field :id
26+
field :name
27+
field :email
28+
field :api_key
29+
field :webhook_key
30+
field :webhook_endpoint
31+
field :created_at
32+
field :updated_at
33+
end
34+
35+
edit do
36+
field :name
37+
field :email
38+
39+
field :api_key do
40+
read_only true
41+
help 'API keys are generated automatically'
42+
end
43+
44+
field :webhook_key do
45+
read_only true
46+
help 'Webhook keys are generated automatically'
47+
end
48+
49+
field :webhook_endpoint
50+
51+
field :created_at do
52+
read_only true
53+
end
54+
55+
field :updated_at do
56+
read_only true
57+
end
58+
end
59+
end
60+
end
61+
1462
private
1563

1664
def set_keys

app/models/gui_user.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,24 @@ class GUIUser < ApplicationRecord
66
has_many :jobs, as: :owner, dependent: :restrict_with_exception
77
has_many :pdf_jobs, as: :owner, dependent: :restrict_with_exception
88
has_many :image_jobs, as: :owner, dependent: :restrict_with_exception
9+
10+
RailsAdmin.config do |config|
11+
config.model 'GuiUser' do
12+
list do
13+
field :id
14+
field :email
15+
field :created_at
16+
end
17+
18+
edit do
19+
field :email
20+
field :created_at do
21+
read_only true
22+
end
23+
field :updated_at do
24+
read_only true
25+
end
26+
end
27+
end
28+
end
929
end

app/models/job.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,39 @@ def completed?
1414
status == 'completed'
1515
end
1616

17+
RailsAdmin.config do |config|
18+
config.model 'Job' do
19+
list do
20+
field :uuid
21+
field :type
22+
field :status
23+
field :owner_type
24+
field :owner_id
25+
field :created_at
26+
field :finished_at
27+
end
28+
29+
show do
30+
field :uuid
31+
field :type
32+
field :status
33+
field :llm_model
34+
field :prompt
35+
field :alt_text
36+
field :source_url
37+
field :output_url
38+
field :output_object_key
39+
field :output_url_expires_at
40+
field :processing_error_message
41+
field :owner_type
42+
field :owner_id
43+
field :created_at
44+
field :updated_at
45+
field :finished_at
46+
end
47+
end
48+
end
49+
1750
private
1851

1952
def broadcast_to_job_channel

app/models/rails_admin.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
RailsAdmin.config do |config|
4+
config.included_models = [
5+
'GuiUser',
6+
'ApiUser',
7+
'Job'
8+
]
9+
end

config/initializers/rails_admin.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
RailsAdmin.config do |config|
4+
config.authenticate_with do
5+
unless AdminUserChecker.admin_user?(request)
6+
render plain: 'Forbidden', status: :forbidden
7+
end
8+
end
9+
10+
config.current_user_method do
11+
warden.user
12+
end
13+
end

config/routes.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# frozen_string_literal: true
22

33
require 'sidekiq/web'
4-
require 'sidekiq_web_constraint'
4+
require 'admin_user_checker'
55

66
Rails.application.routes.draw do
7+
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
78
mount ActionCable.server => '/cable'
89
mount Rswag::Api::Engine => '/api-docs'
910
mount Rswag::Ui::Engine => '/api-docs'
10-
mount Sidekiq::Web => '/sidekiq', :constraints => SidekiqWebConstraint.new
11+
mount Sidekiq::Web => '/sidekiq', :constraints => ->(req) { AdminUserChecker.admin_user?(req) }
1112

1213
get '/sidekiq', to: ->(_env) {
1314
[
1415
401,
1516
{ 'Content-Type' => 'text/plain' },
1617
['Unauthorized']
1718
]
18-
}, constraints: ->(req) { !SidekiqWebConstraint.new.matches?(req) }
19+
}, constraints: ->(req) { !AdminUserChecker.admin_user?(req) }
1920
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
2021

2122
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.

0 commit comments

Comments
 (0)