Deploy Rails 8 Blog v1.0.0 from main 9acb6e4
Browse files- .env.example +35 -0
- .github/dependabot.yml +0 -12
- .github/workflows/ci.yml +0 -127
- .gitignore +2 -1
- .kamal/hooks/docker-setup.sample +0 -3
- .kamal/hooks/post-app-boot.sample +0 -3
- .kamal/hooks/post-deploy.sample +0 -14
- .kamal/hooks/post-proxy-reboot.sample +0 -3
- .kamal/hooks/pre-app-boot.sample +0 -3
- .kamal/hooks/pre-build.sample +0 -51
- .kamal/hooks/pre-connect.sample +0 -47
- .kamal/hooks/pre-deploy.sample +0 -122
- .kamal/hooks/pre-proxy-reboot.sample +0 -3
- .kamal/secrets +0 -20
- AGENTS.md +177 -468
- D8_SOURCE_AUTHORITY.txt +0 -2
- app/blueprints/post_blueprint.rb +9 -2
- app/controllers/admin/audit_logs_controller.rb +9 -1
- app/controllers/admin/posts_controller.rb +14 -4
- app/controllers/admin/slides_controller.rb +115 -0
- app/controllers/api/v1/posts_controller.rb +4 -1
- app/controllers/application_controller.rb +12 -0
- app/controllers/posts_controller.rb +1 -1
- app/models/blocked_request_log.rb +42 -0
- app/models/comment.rb +5 -0
- app/models/post.rb +6 -3
- app/models/setting.rb +0 -1
- app/models/slide.rb +65 -0
- app/models/slide_translation.rb +8 -0
- app/views/admin/audit_logs/index.html.erb +48 -0
- app/views/admin/audit_logs/show.html.erb +20 -0
- app/views/admin/posts/_slideshow.html.erb +120 -0
- app/views/admin/posts/edit.html.erb +2 -0
- app/views/admin/posts/index.html.erb +9 -6
- app/views/admin/posts/trash.html.erb +39 -0
- app/views/posts/show.html.erb +17 -1
- config/database.yml +1 -0
- config/initializers/rack_attack.rb +17 -34
- config/locales/en.yml +3 -0
- config/routes.rb +9 -0
.env.example
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Safe example only. Do not commit real values.
|
| 2 |
+
|
| 3 |
+
# PostgreSQL / Neon
|
| 4 |
+
DATABASE_URL=postgresql://USER:PASSWORD@HOST/DATABASE?sslmode=require
|
| 5 |
+
|
| 6 |
+
# Rails/Puma
|
| 7 |
+
RAILS_MAX_THREADS=3
|
| 8 |
+
JOB_CONCURRENCY=1
|
| 9 |
+
PORT=7860
|
| 10 |
+
RAILS_LOG_LEVEL=info
|
| 11 |
+
|
| 12 |
+
# Hugging Face host authorization
|
| 13 |
+
SPACE_HOST=your-space-subdomain.hf.space
|
| 14 |
+
|
| 15 |
+
# Cloudflare R2 authoritative Active Storage originals
|
| 16 |
+
R2_ENDPOINT=https://ACCOUNT_ID.r2.cloudflarestorage.com
|
| 17 |
+
R2_ACCESS_KEY_ID=replace_me
|
| 18 |
+
R2_SECRET_ACCESS_KEY=replace_me
|
| 19 |
+
R2_BUCKET=replace_me
|
| 20 |
+
R2_REGION=auto
|
| 21 |
+
|
| 22 |
+
# Cloudinary D7 derived ActionText mirror
|
| 23 |
+
CLOUDINARY_CLOUD_NAME=replace_me
|
| 24 |
+
CLOUDINARY_API_KEY=replace_me
|
| 25 |
+
CLOUDINARY_API_SECRET=replace_me
|
| 26 |
+
|
| 27 |
+
# Devise/JWT
|
| 28 |
+
DEVISE_JWT_SECRET_KEY=replace_with_rails_secret
|
| 29 |
+
|
| 30 |
+
# Mail defaults
|
| 31 |
+
MAILER_SENDER=noreply@example.com
|
| 32 |
+
MAIL_FROM_EMAIL=noreply@example.com
|
| 33 |
+
|
| 34 |
+
# Optional runtime toggle used by Puma configuration
|
| 35 |
+
SOLID_QUEUE_IN_PUMA=1
|
.github/dependabot.yml
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
version: 2
|
| 2 |
-
updates:
|
| 3 |
-
- package-ecosystem: bundler
|
| 4 |
-
directory: "/"
|
| 5 |
-
schedule:
|
| 6 |
-
interval: weekly
|
| 7 |
-
open-pull-requests-limit: 10
|
| 8 |
-
- package-ecosystem: github-actions
|
| 9 |
-
directory: "/"
|
| 10 |
-
schedule:
|
| 11 |
-
interval: weekly
|
| 12 |
-
open-pull-requests-limit: 10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.github/workflows/ci.yml
DELETED
|
@@ -1,127 +0,0 @@
|
|
| 1 |
-
name: CI
|
| 2 |
-
|
| 3 |
-
on:
|
| 4 |
-
pull_request:
|
| 5 |
-
push:
|
| 6 |
-
branches: [ main ]
|
| 7 |
-
|
| 8 |
-
jobs:
|
| 9 |
-
scan_ruby:
|
| 10 |
-
runs-on: ubuntu-latest
|
| 11 |
-
|
| 12 |
-
steps:
|
| 13 |
-
- name: Checkout code
|
| 14 |
-
uses: actions/checkout@v4
|
| 15 |
-
|
| 16 |
-
- name: Set up Ruby
|
| 17 |
-
uses: ruby/setup-ruby@v1
|
| 18 |
-
with:
|
| 19 |
-
ruby-version: .ruby-version
|
| 20 |
-
bundler-cache: true
|
| 21 |
-
|
| 22 |
-
- name: Scan for common Rails security vulnerabilities using static analysis
|
| 23 |
-
run: bin/brakeman --no-pager --except CheckEOLRuby
|
| 24 |
-
|
| 25 |
-
- name: Scan for known security vulnerabilities in gems used
|
| 26 |
-
run: bin/bundler-audit
|
| 27 |
-
|
| 28 |
-
scan_js:
|
| 29 |
-
runs-on: ubuntu-latest
|
| 30 |
-
|
| 31 |
-
steps:
|
| 32 |
-
- name: Checkout code
|
| 33 |
-
uses: actions/checkout@v4
|
| 34 |
-
|
| 35 |
-
- name: Set up Ruby
|
| 36 |
-
uses: ruby/setup-ruby@v1
|
| 37 |
-
with:
|
| 38 |
-
ruby-version: .ruby-version
|
| 39 |
-
bundler-cache: true
|
| 40 |
-
|
| 41 |
-
- name: Scan for security vulnerabilities in JavaScript dependencies
|
| 42 |
-
run: bin/importmap audit
|
| 43 |
-
|
| 44 |
-
lint:
|
| 45 |
-
runs-on: ubuntu-latest
|
| 46 |
-
env:
|
| 47 |
-
RUBOCOP_CACHE_ROOT: tmp/rubocop
|
| 48 |
-
steps:
|
| 49 |
-
- name: Checkout code
|
| 50 |
-
uses: actions/checkout@v4
|
| 51 |
-
|
| 52 |
-
- name: Set up Ruby
|
| 53 |
-
uses: ruby/setup-ruby@v1
|
| 54 |
-
with:
|
| 55 |
-
ruby-version: .ruby-version
|
| 56 |
-
bundler-cache: true
|
| 57 |
-
|
| 58 |
-
- name: Prepare RuboCop cache
|
| 59 |
-
uses: actions/cache@v4
|
| 60 |
-
env:
|
| 61 |
-
DEPENDENCIES_HASH: ${{ hashFiles('.ruby-version', '**/.rubocop.yml', '**/.rubocop_todo.yml', 'Gemfile.lock') }}
|
| 62 |
-
with:
|
| 63 |
-
path: ${{ env.RUBOCOP_CACHE_ROOT }}
|
| 64 |
-
key: rubocop-${{ runner.os }}-${{ env.DEPENDENCIES_HASH }}-${{ github.ref_name == github.event.repository.default_branch && github.run_id || 'default' }}
|
| 65 |
-
restore-keys: |
|
| 66 |
-
rubocop-${{ runner.os }}-${{ env.DEPENDENCIES_HASH }}-
|
| 67 |
-
|
| 68 |
-
- name: Lint code for consistent style
|
| 69 |
-
run: bin/rubocop -f github
|
| 70 |
-
|
| 71 |
-
test:
|
| 72 |
-
runs-on: ubuntu-latest
|
| 73 |
-
needs: [scan_ruby, scan_js, lint]
|
| 74 |
-
|
| 75 |
-
services:
|
| 76 |
-
postgres:
|
| 77 |
-
image: postgres:18-alpine
|
| 78 |
-
env:
|
| 79 |
-
POSTGRES_USER: postgres
|
| 80 |
-
POSTGRES_PASSWORD: postgres
|
| 81 |
-
POSTGRES_DB: rails_8_blog_test
|
| 82 |
-
ports:
|
| 83 |
-
- 5432:5432
|
| 84 |
-
options: >-
|
| 85 |
-
--health-cmd "pg_isready -U postgres -d rails_8_blog_test"
|
| 86 |
-
--health-interval 10s
|
| 87 |
-
--health-timeout 5s
|
| 88 |
-
--health-retries 5
|
| 89 |
-
|
| 90 |
-
env:
|
| 91 |
-
RAILS_ENV: test
|
| 92 |
-
CI: "true"
|
| 93 |
-
DATABASE_URL: postgresql://postgres:postgres@127.0.0.1:5432/rails_8_blog_test
|
| 94 |
-
D3_TEST_DATABASE_URL: postgresql://postgres:postgres@127.0.0.1:5432/rails_8_blog_test
|
| 95 |
-
|
| 96 |
-
steps:
|
| 97 |
-
- name: Checkout code
|
| 98 |
-
uses: actions/checkout@v4
|
| 99 |
-
|
| 100 |
-
- name: Set up Ruby
|
| 101 |
-
uses: ruby/setup-ruby@v1
|
| 102 |
-
with:
|
| 103 |
-
ruby-version: .ruby-version
|
| 104 |
-
bundler-cache: true
|
| 105 |
-
|
| 106 |
-
- name: Verify PostgreSQL adapter
|
| 107 |
-
run: |
|
| 108 |
-
bundle exec rails runner '
|
| 109 |
-
adapter = ActiveRecord::Base.connection.adapter_name
|
| 110 |
-
abort "unexpected adapter: #{adapter}" unless adapter == "PostgreSQL"
|
| 111 |
-
puts "CI_POSTGRESQL_ADAPTER_PROOF=PASS"
|
| 112 |
-
'
|
| 113 |
-
|
| 114 |
-
- name: Prepare database
|
| 115 |
-
run: bin/rails db:prepare
|
| 116 |
-
|
| 117 |
-
- name: Run RSpec with coverage
|
| 118 |
-
run: COVERAGE=true bundle exec rspec --format progress | tee tmp/rspec.txt
|
| 119 |
-
|
| 120 |
-
- name: Upload test results and coverage
|
| 121 |
-
uses: actions/upload-artifact@v4
|
| 122 |
-
if: always()
|
| 123 |
-
with:
|
| 124 |
-
name: test-results
|
| 125 |
-
path: |
|
| 126 |
-
tmp/rspec.txt
|
| 127 |
-
coverage/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.gitignore
CHANGED
|
@@ -16,8 +16,9 @@ node_modules
|
|
| 16 |
# Local evidence archives
|
| 17 |
/evidence/
|
| 18 |
|
| 19 |
-
# Ignore
|
| 20 |
/.env*
|
|
|
|
| 21 |
|
| 22 |
# Ignore all logfiles and tempfiles.
|
| 23 |
/log/*
|
|
|
|
| 16 |
# Local evidence archives
|
| 17 |
/evidence/
|
| 18 |
|
| 19 |
+
# Ignore local environment files, but keep the safe documented template.
|
| 20 |
/.env*
|
| 21 |
+
!/.env.example
|
| 22 |
|
| 23 |
# Ignore all logfiles and tempfiles.
|
| 24 |
/log/*
|
.kamal/hooks/docker-setup.sample
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
#!/bin/sh
|
| 2 |
-
|
| 3 |
-
echo "Docker set up on $KAMAL_HOSTS..."
|
|
|
|
|
|
|
|
|
|
|
|
.kamal/hooks/post-app-boot.sample
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
#!/bin/sh
|
| 2 |
-
|
| 3 |
-
echo "Booted app version $KAMAL_VERSION on $KAMAL_HOSTS..."
|
|
|
|
|
|
|
|
|
|
|
|
.kamal/hooks/post-deploy.sample
DELETED
|
@@ -1,14 +0,0 @@
|
|
| 1 |
-
#!/bin/sh
|
| 2 |
-
|
| 3 |
-
# A sample post-deploy hook
|
| 4 |
-
#
|
| 5 |
-
# These environment variables are available:
|
| 6 |
-
# KAMAL_RECORDED_AT
|
| 7 |
-
# KAMAL_PERFORMER
|
| 8 |
-
# KAMAL_VERSION
|
| 9 |
-
# KAMAL_HOSTS
|
| 10 |
-
# KAMAL_ROLES (if set)
|
| 11 |
-
# KAMAL_DESTINATION (if set)
|
| 12 |
-
# KAMAL_RUNTIME
|
| 13 |
-
|
| 14 |
-
echo "$KAMAL_PERFORMER deployed $KAMAL_VERSION to $KAMAL_DESTINATION in $KAMAL_RUNTIME seconds"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.kamal/hooks/post-proxy-reboot.sample
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
#!/bin/sh
|
| 2 |
-
|
| 3 |
-
echo "Rebooted kamal-proxy on $KAMAL_HOSTS"
|
|
|
|
|
|
|
|
|
|
|
|
.kamal/hooks/pre-app-boot.sample
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
#!/bin/sh
|
| 2 |
-
|
| 3 |
-
echo "Booting app version $KAMAL_VERSION on $KAMAL_HOSTS..."
|
|
|
|
|
|
|
|
|
|
|
|
.kamal/hooks/pre-build.sample
DELETED
|
@@ -1,51 +0,0 @@
|
|
| 1 |
-
#!/bin/sh
|
| 2 |
-
|
| 3 |
-
# A sample pre-build hook
|
| 4 |
-
#
|
| 5 |
-
# Checks:
|
| 6 |
-
# 1. We have a clean checkout
|
| 7 |
-
# 2. A remote is configured
|
| 8 |
-
# 3. The branch has been pushed to the remote
|
| 9 |
-
# 4. The version we are deploying matches the remote
|
| 10 |
-
#
|
| 11 |
-
# These environment variables are available:
|
| 12 |
-
# KAMAL_RECORDED_AT
|
| 13 |
-
# KAMAL_PERFORMER
|
| 14 |
-
# KAMAL_VERSION
|
| 15 |
-
# KAMAL_HOSTS
|
| 16 |
-
# KAMAL_ROLES (if set)
|
| 17 |
-
# KAMAL_DESTINATION (if set)
|
| 18 |
-
|
| 19 |
-
if [ -n "$(git status --porcelain)" ]; then
|
| 20 |
-
echo "Git checkout is not clean, aborting..." >&2
|
| 21 |
-
git status --porcelain >&2
|
| 22 |
-
exit 1
|
| 23 |
-
fi
|
| 24 |
-
|
| 25 |
-
first_remote=$(git remote)
|
| 26 |
-
|
| 27 |
-
if [ -z "$first_remote" ]; then
|
| 28 |
-
echo "No git remote set, aborting..." >&2
|
| 29 |
-
exit 1
|
| 30 |
-
fi
|
| 31 |
-
|
| 32 |
-
current_branch=$(git branch --show-current)
|
| 33 |
-
|
| 34 |
-
if [ -z "$current_branch" ]; then
|
| 35 |
-
echo "Not on a git branch, aborting..." >&2
|
| 36 |
-
exit 1
|
| 37 |
-
fi
|
| 38 |
-
|
| 39 |
-
remote_head=$(git ls-remote $first_remote --tags $current_branch | cut -f1)
|
| 40 |
-
|
| 41 |
-
if [ -z "$remote_head" ]; then
|
| 42 |
-
echo "Branch not pushed to remote, aborting..." >&2
|
| 43 |
-
exit 1
|
| 44 |
-
fi
|
| 45 |
-
|
| 46 |
-
if [ "$KAMAL_VERSION" != "$remote_head" ]; then
|
| 47 |
-
echo "Version ($KAMAL_VERSION) does not match remote HEAD ($remote_head), aborting..." >&2
|
| 48 |
-
exit 1
|
| 49 |
-
fi
|
| 50 |
-
|
| 51 |
-
exit 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.kamal/hooks/pre-connect.sample
DELETED
|
@@ -1,47 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env ruby
|
| 2 |
-
|
| 3 |
-
# A sample pre-connect check
|
| 4 |
-
#
|
| 5 |
-
# Warms DNS before connecting to hosts in parallel
|
| 6 |
-
#
|
| 7 |
-
# These environment variables are available:
|
| 8 |
-
# KAMAL_RECORDED_AT
|
| 9 |
-
# KAMAL_PERFORMER
|
| 10 |
-
# KAMAL_VERSION
|
| 11 |
-
# KAMAL_HOSTS
|
| 12 |
-
# KAMAL_ROLES (if set)
|
| 13 |
-
# KAMAL_DESTINATION (if set)
|
| 14 |
-
# KAMAL_RUNTIME
|
| 15 |
-
|
| 16 |
-
hosts = ENV["KAMAL_HOSTS"].split(",")
|
| 17 |
-
results = nil
|
| 18 |
-
max = 3
|
| 19 |
-
|
| 20 |
-
elapsed = Benchmark.realtime do
|
| 21 |
-
results = hosts.map do |host|
|
| 22 |
-
Thread.new do
|
| 23 |
-
tries = 1
|
| 24 |
-
|
| 25 |
-
begin
|
| 26 |
-
Socket.getaddrinfo(host, 0, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME)
|
| 27 |
-
rescue SocketError
|
| 28 |
-
if tries < max
|
| 29 |
-
puts "Retrying DNS warmup: #{host}"
|
| 30 |
-
tries += 1
|
| 31 |
-
sleep rand
|
| 32 |
-
retry
|
| 33 |
-
else
|
| 34 |
-
puts "DNS warmup failed: #{host}"
|
| 35 |
-
host
|
| 36 |
-
end
|
| 37 |
-
end
|
| 38 |
-
|
| 39 |
-
tries
|
| 40 |
-
end
|
| 41 |
-
end.map(&:value)
|
| 42 |
-
end
|
| 43 |
-
|
| 44 |
-
retries = results.sum - hosts.size
|
| 45 |
-
nopes = results.count { |r| r == max }
|
| 46 |
-
|
| 47 |
-
puts "Prewarmed %d DNS lookups in %.2f sec: %d retries, %d failures" % [ hosts.size, elapsed, retries, nopes ]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.kamal/hooks/pre-deploy.sample
DELETED
|
@@ -1,122 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env ruby
|
| 2 |
-
|
| 3 |
-
# A sample pre-deploy hook
|
| 4 |
-
#
|
| 5 |
-
# Checks the Github status of the build, waiting for a pending build to complete for up to 720 seconds.
|
| 6 |
-
#
|
| 7 |
-
# Fails unless the combined status is "success"
|
| 8 |
-
#
|
| 9 |
-
# These environment variables are available:
|
| 10 |
-
# KAMAL_RECORDED_AT
|
| 11 |
-
# KAMAL_PERFORMER
|
| 12 |
-
# KAMAL_VERSION
|
| 13 |
-
# KAMAL_HOSTS
|
| 14 |
-
# KAMAL_COMMAND
|
| 15 |
-
# KAMAL_SUBCOMMAND
|
| 16 |
-
# KAMAL_ROLES (if set)
|
| 17 |
-
# KAMAL_DESTINATION (if set)
|
| 18 |
-
|
| 19 |
-
# Only check the build status for production deployments
|
| 20 |
-
if ENV["KAMAL_COMMAND"] == "rollback" || ENV["KAMAL_DESTINATION"] != "production"
|
| 21 |
-
exit 0
|
| 22 |
-
end
|
| 23 |
-
|
| 24 |
-
require "bundler/inline"
|
| 25 |
-
|
| 26 |
-
# true = install gems so this is fast on repeat invocations
|
| 27 |
-
gemfile(true, quiet: true) do
|
| 28 |
-
source "https://rubygems.org"
|
| 29 |
-
|
| 30 |
-
gem "octokit"
|
| 31 |
-
gem "faraday-retry"
|
| 32 |
-
end
|
| 33 |
-
|
| 34 |
-
MAX_ATTEMPTS = 72
|
| 35 |
-
ATTEMPTS_GAP = 10
|
| 36 |
-
|
| 37 |
-
def exit_with_error(message)
|
| 38 |
-
$stderr.puts message
|
| 39 |
-
exit 1
|
| 40 |
-
end
|
| 41 |
-
|
| 42 |
-
class GithubStatusChecks
|
| 43 |
-
attr_reader :remote_url, :git_sha, :github_client, :combined_status
|
| 44 |
-
|
| 45 |
-
def initialize
|
| 46 |
-
@remote_url = github_repo_from_remote_url
|
| 47 |
-
@git_sha = `git rev-parse HEAD`.strip
|
| 48 |
-
@github_client = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"])
|
| 49 |
-
refresh!
|
| 50 |
-
end
|
| 51 |
-
|
| 52 |
-
def refresh!
|
| 53 |
-
@combined_status = github_client.combined_status(remote_url, git_sha)
|
| 54 |
-
end
|
| 55 |
-
|
| 56 |
-
def state
|
| 57 |
-
combined_status[:state]
|
| 58 |
-
end
|
| 59 |
-
|
| 60 |
-
def first_status_url
|
| 61 |
-
first_status = combined_status[:statuses].find { |status| status[:state] == state }
|
| 62 |
-
first_status && first_status[:target_url]
|
| 63 |
-
end
|
| 64 |
-
|
| 65 |
-
def complete_count
|
| 66 |
-
combined_status[:statuses].count { |status| status[:state] != "pending"}
|
| 67 |
-
end
|
| 68 |
-
|
| 69 |
-
def total_count
|
| 70 |
-
combined_status[:statuses].count
|
| 71 |
-
end
|
| 72 |
-
|
| 73 |
-
def current_status
|
| 74 |
-
if total_count > 0
|
| 75 |
-
"Completed #{complete_count}/#{total_count} checks, see #{first_status_url} ..."
|
| 76 |
-
else
|
| 77 |
-
"Build not started..."
|
| 78 |
-
end
|
| 79 |
-
end
|
| 80 |
-
|
| 81 |
-
private
|
| 82 |
-
def github_repo_from_remote_url
|
| 83 |
-
url = `git config --get remote.origin.url`.strip.delete_suffix(".git")
|
| 84 |
-
if url.start_with?("https://github.com/")
|
| 85 |
-
url.delete_prefix("https://github.com/")
|
| 86 |
-
elsif url.start_with?("git@github.com:")
|
| 87 |
-
url.delete_prefix("git@github.com:")
|
| 88 |
-
else
|
| 89 |
-
url
|
| 90 |
-
end
|
| 91 |
-
end
|
| 92 |
-
end
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
$stdout.sync = true
|
| 96 |
-
|
| 97 |
-
begin
|
| 98 |
-
puts "Checking build status..."
|
| 99 |
-
|
| 100 |
-
attempts = 0
|
| 101 |
-
checks = GithubStatusChecks.new
|
| 102 |
-
|
| 103 |
-
loop do
|
| 104 |
-
case checks.state
|
| 105 |
-
when "success"
|
| 106 |
-
puts "Checks passed, see #{checks.first_status_url}"
|
| 107 |
-
exit 0
|
| 108 |
-
when "failure"
|
| 109 |
-
exit_with_error "Checks failed, see #{checks.first_status_url}"
|
| 110 |
-
when "pending"
|
| 111 |
-
attempts += 1
|
| 112 |
-
end
|
| 113 |
-
|
| 114 |
-
exit_with_error "Checks are still pending, gave up after #{MAX_ATTEMPTS * ATTEMPTS_GAP} seconds" if attempts == MAX_ATTEMPTS
|
| 115 |
-
|
| 116 |
-
puts checks.current_status
|
| 117 |
-
sleep(ATTEMPTS_GAP)
|
| 118 |
-
checks.refresh!
|
| 119 |
-
end
|
| 120 |
-
rescue Octokit::NotFound
|
| 121 |
-
exit_with_error "Build status could not be found"
|
| 122 |
-
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.kamal/hooks/pre-proxy-reboot.sample
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
#!/bin/sh
|
| 2 |
-
|
| 3 |
-
echo "Rebooting kamal-proxy on $KAMAL_HOSTS..."
|
|
|
|
|
|
|
|
|
|
|
|
.kamal/secrets
DELETED
|
@@ -1,20 +0,0 @@
|
|
| 1 |
-
# Secrets defined here are available for reference under registry/password, env/secret, builder/secrets,
|
| 2 |
-
# and accessories/*/env/secret in config/deploy.yml. All secrets should be pulled from either
|
| 3 |
-
# password manager, ENV, or a file. DO NOT ENTER RAW CREDENTIALS HERE! This file needs to be safe for git.
|
| 4 |
-
|
| 5 |
-
# Example of extracting secrets from 1password (or another compatible pw manager)
|
| 6 |
-
# SECRETS=$(kamal secrets fetch --adapter 1password --account your-account --from Vault/Item KAMAL_REGISTRY_PASSWORD RAILS_MASTER_KEY)
|
| 7 |
-
# KAMAL_REGISTRY_PASSWORD=$(kamal secrets extract KAMAL_REGISTRY_PASSWORD ${SECRETS})
|
| 8 |
-
# RAILS_MASTER_KEY=$(kamal secrets extract RAILS_MASTER_KEY ${SECRETS})
|
| 9 |
-
|
| 10 |
-
# Example of extracting secrets from Rails credentials
|
| 11 |
-
# KAMAL_REGISTRY_PASSWORD=$(rails credentials:fetch kamal.registry_password)
|
| 12 |
-
|
| 13 |
-
# Use a GITHUB_TOKEN if private repositories are needed for the image
|
| 14 |
-
# GITHUB_TOKEN=$(gh config get -h github.com oauth_token)
|
| 15 |
-
|
| 16 |
-
# Grab the registry password from ENV
|
| 17 |
-
# KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD
|
| 18 |
-
|
| 19 |
-
# Improve security by using a password manager. Never check config/master.key into git!
|
| 20 |
-
RAILS_MASTER_KEY=$(cat config/master.key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AGENTS.md
CHANGED
|
@@ -1,477 +1,186 @@
|
|
| 1 |
-
|
| 2 |
-
> During the deployment-v2 migration, the authoritative plan is
|
| 3 |
-
> `../CODEX_EXECUTION_GUIDE/00_MASTER_EXECUTION_ORDER.md` plus the current
|
| 4 |
-
> phase file. Older Cloudinary/Redis/Sidekiq/SQLite instructions below are
|
| 5 |
-
> historical until Phase D12 rebases this document. When they conflict, the
|
| 6 |
-
> deployment-v2 plan wins.
|
| 7 |
-
|
| 8 |
-
# AGENTS.md — Rails Blog Management (Enterprise & API Ready)
|
| 9 |
-
|
| 10 |
-
> **AI Coding Agent Reference Guide**
|
| 11 |
-
> Stack: Ruby on Rails 8 · Hotwire (Turbo/Stimulus) · Devise + JWT · Pundit · Mobility · Cloudinary · Redis · Sidekiq · SQLite3
|
| 12 |
-
> Read this file **before writing a single line of code.** It defines conventions, build order, mental models, and common pitfalls for this project.
|
| 13 |
-
|
| 14 |
-
---
|
| 15 |
-
|
| 16 |
-
## Table of Contents
|
| 17 |
-
|
| 18 |
-
1. [Project Overview](#1-project-overview)
|
| 19 |
-
2. [Critical Build Order](#2-critical-build-order)
|
| 20 |
-
3. [Architecture Mental Model](#3-architecture-mental-model)
|
| 21 |
-
4. [Tech Stack & Gem Decisions](#4-tech-stack--gem-decisions)
|
| 22 |
-
5. [Environment & Configuration Rules](#5-environment--configuration-rules)
|
| 23 |
-
6. [Data Model Conventions](#6-data-model-conventions)
|
| 24 |
-
7. [Multilingual (i18n) Rules](#7-multilingual-i18n-rules)
|
| 25 |
-
8. [Authentication & Authorization Rules](#8-authentication--authorization-rules)
|
| 26 |
-
9. [Cloudinary Integration Rules](#9-cloudinary-integration-rules)
|
| 27 |
-
10. [Background Jobs Rules](#10-background-jobs-rules)
|
| 28 |
-
11. [Security & Rate Limiting Rules](#11-security--rate-limiting-rules)
|
| 29 |
-
12. [API (v1) Rules](#12-api-v1-rules)
|
| 30 |
-
13. [Admin & Settings Rules](#13-admin--settings-rules)
|
| 31 |
-
14. [Caching Rules](#14-caching-rules)
|
| 32 |
-
15. [Testing Rules](#15-testing-rules)
|
| 33 |
-
16. [File & Directory Conventions](#16-file--directory-conventions)
|
| 34 |
-
17. [Do Not / Never Rules](#17-do-not--never-rules)
|
| 35 |
-
18. [Skill Files Index](#18-skill-files-index)
|
| 36 |
-
|
| 37 |
-
---
|
| 38 |
-
|
| 39 |
-
## 1. Project Overview
|
| 40 |
-
|
| 41 |
-
This is a **full-stack Rails 8 blog management system** with enterprise features:
|
| 42 |
-
|
| 43 |
-
- **Content**: Multi-language blog posts (7 locales), rich text, image slideshows
|
| 44 |
-
- **Auth**: Devise (all modules) + JWT for API consumers
|
| 45 |
-
- **Authorization**: Pundit with 3 roles: `admin`, `author`, `reader`
|
| 46 |
-
- **Media**: Cloudinary (direct signed uploads + Media Library SDK)
|
| 47 |
-
- **Background**: Sidekiq + Sidekiq Scheduler
|
| 48 |
-
- **API**: RESTful JSON at `/api/v1/` with JWT auth and locale-aware responses
|
| 49 |
-
- **Admin**: Full dashboard with dynamic `Settings` (no-deploy config changes)
|
| 50 |
-
- **Security**: Rack::Attack (layer 0) + Rails 8 rate limiting (layer 1)
|
| 51 |
-
- **Database**: SQLite3 (development/production) + Redis (cache/queue)
|
| 52 |
-
|
| 53 |
-
**Language split:**
|
| 54 |
-
| Surface | Languages |
|
| 55 |
-
|---|---|
|
| 56 |
-
| Admin UI / Backend | English (en), Vietnamese (vi) |
|
| 57 |
-
| Post content / Frontend | en, vi, ru, ja, ko, th, de |
|
| 58 |
-
|
| 59 |
-
---
|
| 60 |
-
|
| 61 |
-
## 2. Critical Build Order
|
| 62 |
-
|
| 63 |
-
**Never skip or reorder phases.** Each phase depends on the previous one.
|
| 64 |
|
| 65 |
-
|
| 66 |
-
Phase 0 → Project setup, Gemfile, DB, Redis, Sidekiq, i18n config
|
| 67 |
-
Phase 1 → Devise + JWT + Pundit (auth must exist before any protected resource)
|
| 68 |
-
Phase 2 → Post model, multilingual content, workflow states, routes
|
| 69 |
-
Phase 3 → Cloudinary (upload, Media Library, transformations, housekeeping)
|
| 70 |
-
Phase 4 → Security: Rack::Attack + Rails rate limiting + maintenance mode
|
| 71 |
-
Phase 5 → API v1 (JWT-protected JSON endpoints)
|
| 72 |
-
Phase 6 → Admin Dashboard + dynamic Settings
|
| 73 |
-
Phase 7 → Audit log (PaperTrail) + Soft delete (Discard)
|
| 74 |
-
Phase 8 → Slideshow engine (SlideshowAsset + drag-and-drop)
|
| 75 |
-
Phase 9 → Multi-layer caching (fragment + Russian Doll + eager loading)
|
| 76 |
-
Phase 10 → Tests (RSpec + FactoryBot) + health checks + CI
|
| 77 |
-
```
|
| 78 |
|
| 79 |
-
|
| 80 |
-
- `Setting` (rack_attack reads limits from it)
|
| 81 |
-
- `BlockedRequestLog` (rack_attack writes to it)
|
| 82 |
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
-
Browser / Mobile App / Vue.js
|
| 89 |
-
│
|
| 90 |
-
▼ HTTP / WebSocket
|
| 91 |
-
┌─────────────────────────────────────────┐
|
| 92 |
-
│ Rails 8 App │
|
| 93 |
-
│ Web Routes (/vi /en ...) │
|
| 94 |
-
│ API v1 (/api/v1/...) │
|
| 95 |
-
│ Admin Namespace (/admin/...) │
|
| 96 |
-
│ │
|
| 97 |
-
│ Middleware: Rack::Attack → RateLimiter │
|
| 98 |
-
│ → MaintenanceMode → Devise │
|
| 99 |
-
└──────────┬────────────┬────────────┬───┘
|
| 100 |
-
│ │ │
|
| 101 |
-
SQLite3 Redis Cloudinary
|
| 102 |
-
(primary) (cache/queue) (media CDN)
|
| 103 |
-
│
|
| 104 |
-
Sidekiq
|
| 105 |
-
(background jobs)
|
| 106 |
-
```
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
---
|
| 119 |
-
|
| 120 |
-
## 4. Tech Stack & Gem Decisions
|
| 121 |
-
|
| 122 |
-
| Purpose | Gem | Notes |
|
| 123 |
-
|---|---|---|
|
| 124 |
-
| Auth | `devise` + `devise-jwt` | All modules enabled; JWT uses Denylist strategy |
|
| 125 |
-
| Authorization | `pundit` | Policy files in `app/policies/` |
|
| 126 |
-
| i18n data | `mobility` + `mobility-actiontext` | Table backend; translates `title`, `slug` |
|
| 127 |
-
| Media | `cloudinary` | Direct signed uploads only; no server-side byte processing |
|
| 128 |
-
| Rich text | ActionText (built-in) | One rich text field per locale: `content_en`, `content_vi`, etc. |
|
| 129 |
-
| Background jobs | `sidekiq` + `sidekiq-scheduler` | Queue names: `default`, `mailers`, `media`, `critical` |
|
| 130 |
-
| Rate limiting | `rack-attack` | Redis-backed; limits come from `Setting` model |
|
| 131 |
-
| Slug | `friendly_id` | Integrates with Mobility for per-locale slugs |
|
| 132 |
-
| Soft delete | `discard` | Used on `Post` and `Comment` |
|
| 133 |
-
| Audit | `paper_trail` | Used on `Post` and `Setting` |
|
| 134 |
-
| Pagination | `pagy` | Use in all index actions (web + API) |
|
| 135 |
-
| Serializer | `blueprinter` | API responses only |
|
| 136 |
-
| View components | `view_component` | Admin UI components |
|
| 137 |
-
| View tracking | `impressionist` | Unique views by IP |
|
| 138 |
-
| Dev/test extras | `rspec-rails`, `factory_bot_rails`, `faker`, `webmock`, `simplecov`, `shoulda-matchers` |
|
| 139 |
-
| Dev only | `letter_opener`, `annotate`, `rubocop-rails-omakase`, `dotenv-rails` |
|
| 140 |
-
|
| 141 |
-
---
|
| 142 |
-
|
| 143 |
-
## 5. Environment & Configuration Rules
|
| 144 |
-
|
| 145 |
-
- **Never hardcode credentials.** Always use `ENV.fetch('KEY', default)`.
|
| 146 |
-
- All secrets live in `.env` (loaded by `dotenv-rails` in dev/test). See `.env.example`.
|
| 147 |
-
- SQLite3 database files go in `storage/` — do NOT use `username/password/host` fields (those are PostgreSQL-only).
|
| 148 |
-
- Redis URL defaults to `redis://localhost:6379/0` for Sidekiq, `/1` for cache.
|
| 149 |
-
- `DEVISE_JWT_SECRET_KEY` must be set; generate with `rake secret`.
|
| 150 |
-
- Cloudinary requires: `CLOUDINARY_CLOUD_NAME`, `CLOUDINARY_API_KEY`, `CLOUDINARY_API_SECRET`.
|
| 151 |
-
- i18n available locales: `[:en, :vi, :ru, :ja, :ko, :th, :de]`, default: `:en`, fallback to `:en`.
|
| 152 |
-
- Sidekiq queues in priority order: `critical` > `default` > `mailers` > `media`.
|
| 153 |
-
|
| 154 |
-
---
|
| 155 |
-
|
| 156 |
-
## 6. Data Model Conventions
|
| 157 |
-
|
| 158 |
-
### Naming
|
| 159 |
-
- Use `snake_case` for all columns and tables.
|
| 160 |
-
- Cloudinary references are always stored as `cloudinary_public_id` (a `string`), never as a full URL.
|
| 161 |
-
- Rich text fields are named `content_{locale}` (e.g., `content_en`, `content_vi`).
|
| 162 |
-
- Soft-delete column: `discarded_at` (Discard gem convention).
|
| 163 |
-
- Audit column: handled by PaperTrail's `versions` table.
|
| 164 |
-
|
| 165 |
-
### Key models and their responsibilities
|
| 166 |
-
|
| 167 |
-
| Model | Responsibility |
|
| 168 |
-
|---|---|
|
| 169 |
-
| `User` | Auth (Devise), role (`admin`/`author`/`reader`), avatar public_id |
|
| 170 |
-
| `Post` | Core content entity; status state machine; belongs to user + category |
|
| 171 |
-
| `PostTranslation` | Mobility table: stores `title` and `slug` per locale |
|
| 172 |
-
| `Category` | Post classification; has friendly_id slug |
|
| 173 |
-
| `Tag` | Many-to-many with Post via `PostTag` |
|
| 174 |
-
| `SlideshowAsset` | Ordered Cloudinary images attached to a Post |
|
| 175 |
-
| `Comment` | Soft-deletable; counter cached on Post |
|
| 176 |
-
| `Reaction` | Polymorphic-style; `kind` string, unique per user+post |
|
| 177 |
-
| `Setting` | Key-value store with typed values; Redis-cached; PaperTrail-audited |
|
| 178 |
-
| `JwtDenylist` | Revoked JWT tokens |
|
| 179 |
-
| `BlockedRequestLog` | Log of IPs blocked by Rack::Attack |
|
| 180 |
-
|
| 181 |
-
### Post status workflow (strict 4-step, no skipping)
|
| 182 |
-
```
|
| 183 |
-
draft → pending → scheduled → published
|
| 184 |
-
```
|
| 185 |
-
- `draft`: author is editing
|
| 186 |
-
- `pending`: submitted, awaiting admin approval
|
| 187 |
-
- `scheduled`: admin approved, set a future `scheduled_at`
|
| 188 |
-
- `published`: live (either manually or via `PublishPostJob`)
|
| 189 |
-
|
| 190 |
-
---
|
| 191 |
-
|
| 192 |
-
## 7. Multilingual (i18n) Rules
|
| 193 |
-
|
| 194 |
-
- **Mobility translates `title` and `slug`** via the `post_translations` table. Do NOT put these in the posts table directly.
|
| 195 |
-
- **Rich text content per locale** uses separate ActionText fields: `has_rich_text :content_en`, `has_rich_text :content_vi`, etc. Access via `post.content_for(locale)`.
|
| 196 |
-
- **Admin UI** uses a Stimulus `language-tabs` controller — switching tabs does NOT reload the page.
|
| 197 |
-
- **URL structure**: `/:locale/posts/:slug` — locale is in the URL path, not a query param.
|
| 198 |
-
- **API locale**: passed via `Accept-Language` header or `?locale=` param.
|
| 199 |
-
- **Cache keys must include locale** to prevent cross-locale cache contamination: `cache [post, I18n.locale, 'card_v1']`.
|
| 200 |
-
- **Fallback**: all locales fall back to English (`:en`) when translation is missing.
|
| 201 |
-
- Admin YAML translations only for `[:en, :vi]`. Frontend YAML for all 7 locales.
|
| 202 |
-
- When using Mobility in tests, use `Mobility.with_locale(locale) { ... }` to set translations.
|
| 203 |
-
|
| 204 |
-
---
|
| 205 |
-
|
| 206 |
-
## 8. Authentication & Authorization Rules
|
| 207 |
-
|
| 208 |
-
### Devise modules (all enabled)
|
| 209 |
-
`database_authenticatable`, `registerable`, `recoverable`, `rememberable`, `validatable`, `confirmable`, `lockable`, `timeoutable`, `trackable`, `omniauthable`, `jwt_authenticatable`
|
| 210 |
-
|
| 211 |
-
### JWT
|
| 212 |
-
- Strategy: **Denylist** (table `jwt_denylist`)
|
| 213 |
-
- Dispatch on: `POST /api/v1/auth/sign_in`
|
| 214 |
-
- Revocation on: `DELETE /api/v1/auth/sign_out`
|
| 215 |
-
- Expiry: 24 hours
|
| 216 |
-
- Payload includes: `user_id`, `role`, `exp`, `jti`
|
| 217 |
-
- Web sessions and API JWT are separate — web uses cookie sessions, API uses Bearer tokens
|
| 218 |
-
|
| 219 |
-
### Pundit policies (in `app/policies/`)
|
| 220 |
-
- Every controller action that modifies data must call `authorize @record` or `policy_scope(Model)`.
|
| 221 |
-
- `PostPolicy#publish?` — admin or post owner
|
| 222 |
-
- `UserPolicy` — admin manages all users; users manage themselves
|
| 223 |
-
- Always rescue `Pundit::NotAuthorizedError` in `ApplicationController`
|
| 224 |
-
|
| 225 |
-
### Role rules
|
| 226 |
-
| Action | admin | author | reader |
|
| 227 |
-
|---|---|---|---|
|
| 228 |
-
| Create post | ✅ | ✅ | ❌ |
|
| 229 |
-
| Edit own post | ✅ | ✅ | ❌ |
|
| 230 |
-
| Edit any post | ✅ | ❌ | ❌ |
|
| 231 |
-
| Publish/approve | ✅ | ❌ | ❌ |
|
| 232 |
-
| Comment | ✅ | ✅ | ✅ |
|
| 233 |
-
| Admin dashboard | ✅ | ❌ | ❌ |
|
| 234 |
-
| Settings page | ✅ | ❌ | ❌ |
|
| 235 |
-
|
| 236 |
-
---
|
| 237 |
-
|
| 238 |
-
## 9. Cloudinary Integration Rules
|
| 239 |
-
|
| 240 |
-
- **Never process image bytes on the Rails server.** All uploads are direct signed uploads from the browser to Cloudinary.
|
| 241 |
-
- Rails only generates the **signature** for upload (via `UploadsController#signature`). The actual file never touches Rails.
|
| 242 |
-
- Store only `cloudinary_public_id` in the database, never the full URL. Reconstruct URLs via `Cloudinary::Utils.cloudinary_url(...)`.
|
| 243 |
-
- Always use `f_auto` (format) and `q_auto` (quality) on all transformation URLs.
|
| 244 |
-
- **Folder convention:**
|
| 245 |
-
- Thumbnails: `thumb/{YYYY-MM}/{post_id}`
|
| 246 |
-
- Gallery/Slideshow: `gallery/{YYYY-MM}/{post_id}/{position}`
|
| 247 |
-
- Avatars: `avatars/{user_id}`
|
| 248 |
-
- When a post slug changes, enqueue `RenameCloudinaryAssetsJob` to: (1) rename asset on Cloudinary, (2) update URLs in ActionText rich text fields.
|
| 249 |
-
- `CloudinaryHousekeepingJob` runs every Sunday at 3 AM to delete orphaned assets.
|
| 250 |
-
- Use the **Cloudinary Media Library SDK** in admin to let users pick existing assets without re-uploading.
|
| 251 |
-
|
| 252 |
-
---
|
| 253 |
-
|
| 254 |
-
## 10. Background Jobs Rules
|
| 255 |
-
|
| 256 |
-
Queue names and their purposes:
|
| 257 |
-
|
| 258 |
-
| Queue | Used for |
|
| 259 |
-
|---|---|
|
| 260 |
-
| `critical` | Security alerts (`AdminNotificationJob`) |
|
| 261 |
-
| `default` | Post publishing (`PublishPostJob`) |
|
| 262 |
-
| `mailers` | All ActionMailer jobs |
|
| 263 |
-
| `media` | Cloudinary operations (`RenameCloudinaryAssetsJob`, `CloudinaryHousekeepingJob`) |
|
| 264 |
-
|
| 265 |
-
Rules:
|
| 266 |
-
- All job classes inherit from `ApplicationJob`.
|
| 267 |
-
- Jobs must be **idempotent** — safe to run multiple times (always check record still exists and is in the expected state).
|
| 268 |
-
- `PublishPostJob`: check `post.scheduled?` and `post.scheduled_at <= Time.current` before publishing.
|
| 269 |
-
- Scheduled jobs are configured in `config/sidekiq.yml` under `:schedule`.
|
| 270 |
-
- Always pass IDs to jobs, never full AR objects (prevents serialization issues).
|
| 271 |
-
|
| 272 |
-
---
|
| 273 |
-
|
| 274 |
-
## 11. Security & Rate Limiting Rules
|
| 275 |
-
|
| 276 |
-
### Two-layer defense
|
| 277 |
-
|
| 278 |
-
**Layer 0 — Rack::Attack (middleware, before Rails routing):**
|
| 279 |
-
- Throttles login attempts by IP and by email (5 attempts per 60 seconds, configurable via `Setting`)
|
| 280 |
-
- IP blocklist and whitelist loaded from `Setting`, cached in Redis (5-minute TTL)
|
| 281 |
-
- On block: creates `BlockedRequestLog`, enqueues `AdminNotificationJob`, returns JSON 429
|
| 282 |
-
|
| 283 |
-
**Layer 1 — Rails 8 `rate_limit` (controller level, per user/session):**
|
| 284 |
-
- `CommentsController#create`: 10 per minute per user_id or IP
|
| 285 |
-
- `ReactionsController#create/destroy`: 30 per minute per user_id or IP
|
| 286 |
-
- Returns flash message in user's locale, then redirects
|
| 287 |
-
|
| 288 |
-
**Maintenance mode middleware:**
|
| 289 |
-
- Checks `Setting.get('maintenance_mode')` from Redis cache
|
| 290 |
-
- Admin paths (`/admin`, `/auth`) are always bypassed
|
| 291 |
-
- Returns 503 with localized HTML message
|
| 292 |
-
|
| 293 |
-
### Key setting keys for security
|
| 294 |
-
| Key | Default | Description |
|
| 295 |
-
|---|---|---|
|
| 296 |
-
| `rack_login_limit` | 5 | Max login attempts per period |
|
| 297 |
-
| `rack_login_period` | 60 | Period in seconds |
|
| 298 |
-
| `devise_lock_attempts` | 5 | Devise lockable threshold |
|
| 299 |
-
| `blocked_ips` | `[]` | IP blacklist (array) |
|
| 300 |
-
| `whitelist_ips` | `[]` | IP whitelist (array) |
|
| 301 |
-
| `maintenance_mode` | `false` | Enable 503 mode |
|
| 302 |
-
|
| 303 |
-
---
|
| 304 |
-
|
| 305 |
-
## 12. API (v1) Rules
|
| 306 |
-
|
| 307 |
-
- **Base controller**: `Api::V1::BaseController < ActionController::API`
|
| 308 |
-
- Includes `Pundit::Authorization` and `Pagy::Backend`
|
| 309 |
-
- Authentication via JWT Bearer token in `Authorization` header
|
| 310 |
-
- Locale from `Accept-Language` header or `?locale=` param (falls back to `:en`)
|
| 311 |
-
- All public endpoints (`index`, `show` for posts/categories/tags) skip authentication
|
| 312 |
-
- Responses use **Blueprinter** serializers (never `to_json` directly on AR objects)
|
| 313 |
-
- Pagination metadata included in all collection responses via `pagy_metadata`
|
| 314 |
-
- `POST /api/v1/auth/sign_in` → returns JWT in `Authorization: Bearer <token>` header
|
| 315 |
-
- `DELETE /api/v1/auth/sign_out` → revokes JWT (adds to denylist)
|
| 316 |
-
- `GET /api/v1/health` → returns DB/Redis/Sidekiq status (no auth required)
|
| 317 |
-
- Error responses: always JSON `{ error: "message" }` or `{ errors: [...] }`
|
| 318 |
-
- HTTP status codes: 200, 201, 401, 403, 404, 422, 429
|
| 319 |
-
|
| 320 |
-
---
|
| 321 |
-
|
| 322 |
-
## 13. Admin & Settings Rules
|
| 323 |
-
|
| 324 |
-
### Admin namespace
|
| 325 |
-
- All admin controllers inherit from `Admin::AdminController < ApplicationController`
|
| 326 |
-
- `Admin::AdminController` enforces `authenticate_user!` + `require_admin!`
|
| 327 |
-
- Uses `layout 'admin'`
|
| 328 |
-
- URL prefix: `/admin/`
|
| 329 |
-
|
| 330 |
-
### Dynamic Settings
|
| 331 |
-
- `Setting.get('key', default)` — reads from Redis cache, falls back to DB
|
| 332 |
-
- `Setting.set('key', value)` — writes to DB, invalidates Redis cache
|
| 333 |
-
- `value_type` determines how the value is cast: `string`, `integer`, `boolean`, `array`, `json`
|
| 334 |
-
- Categories: `general`, `security`, `media`, `email`, `rate_limit`
|
| 335 |
-
- Always use `Setting.get(...)` in initializers/middleware — never `Setting.find_by(...)` directly (avoids N+1 on every request)
|
| 336 |
-
- Settings changes take effect within 5 minutes (Redis TTL)
|
| 337 |
-
- All Setting changes are audited by PaperTrail
|
| 338 |
-
|
| 339 |
-
### Multi-SMTP
|
| 340 |
-
- Providers stored as JSON array in `Setting['smtp_providers']`
|
| 341 |
-
- Each provider: `{ name, host, port, username, password, active }`
|
| 342 |
-
- Strategies: `round_robin` (default), `random`, or forced by `smtp_force_provider`
|
| 343 |
-
- Implemented as `ActionMailer::Interceptor` — registered in `config/initializers/action_mailer.rb`
|
| 344 |
-
|
| 345 |
-
---
|
| 346 |
-
|
| 347 |
-
## 14. Caching Rules
|
| 348 |
-
|
| 349 |
-
- **Always include `I18n.locale` in fragment cache keys** — failure to do so will serve wrong-language cached content.
|
| 350 |
-
- Use `cache_key_with_version` (Rails default) — cache auto-expires on model `updated_at` change.
|
| 351 |
-
- Cache key pattern: `[model_instance, I18n.locale, 'version_tag']`
|
| 352 |
-
- Russian Doll caching: outer cache wraps inner caches (slideshow, comments). Inner caches can expire independently.
|
| 353 |
-
- **Settings cache**: 5-minute TTL via `Rails.cache.fetch("setting:#{key}", expires_in: 5.minutes)`
|
| 354 |
-
- **Never** eager-load associations inside a cached fragment — eager load in the controller, then pass to the view.
|
| 355 |
-
- Use `.includes(:user, :category, :tags, :post_translations)` on all list queries.
|
| 356 |
-
|
| 357 |
-
---
|
| 358 |
-
|
| 359 |
-
## 15. Testing Rules
|
| 360 |
-
|
| 361 |
-
- Test framework: **RSpec** (not Minitest)
|
| 362 |
-
- Factories: **FactoryBot** (in `spec/factories/`)
|
| 363 |
-
- Coverage: **SimpleCov** — minimum 90% line coverage enforced in CI
|
| 364 |
-
- HTTP mocking: **WebMock** — all external HTTP calls must be stubbed
|
| 365 |
-
- Cloudinary: always stub via `stub_cloudinary_requests` helper (in `spec/support/cloudinary_helpers.rb`)
|
| 366 |
-
- Use `Mobility.with_locale(locale) { ... }` when setting translatable attributes in factories
|
| 367 |
-
- Request specs for all API endpoints; model specs for all models; system specs for critical admin flows
|
| 368 |
-
- Tests must not make real network calls — `WebMock.disable_net_connect!(allow_localhost: true)`
|
| 369 |
-
- CI runs: RuboCop → RSpec → coverage check (fail if < 90%)
|
| 370 |
-
- Test environment uses same SQLite3 adapter; Redis must be available in CI (use service container)
|
| 371 |
-
|
| 372 |
-
---
|
| 373 |
-
|
| 374 |
-
## 16. File & Directory Conventions
|
| 375 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
```
|
| 377 |
-
app/
|
| 378 |
-
controllers/
|
| 379 |
-
admin/
|
| 380 |
-
admin_controller.rb ← base for all admin controllers
|
| 381 |
-
settings_controller.rb
|
| 382 |
-
audit_logs_controller.rb
|
| 383 |
-
slideshow_assets_controller.rb
|
| 384 |
-
api/
|
| 385 |
-
v1/
|
| 386 |
-
base_controller.rb ← base for all API controllers
|
| 387 |
-
posts_controller.rb
|
| 388 |
-
health_controller.rb
|
| 389 |
-
uploads_controller.rb ← Cloudinary signature endpoint
|
| 390 |
-
jobs/
|
| 391 |
-
publish_post_job.rb
|
| 392 |
-
rename_cloudinary_assets_job.rb
|
| 393 |
-
cloudinary_housekeeping_job.rb
|
| 394 |
-
admin_notification_job.rb
|
| 395 |
-
mailers/
|
| 396 |
-
admin_mailer.rb
|
| 397 |
-
interceptors/
|
| 398 |
-
smtp_selector_interceptor.rb
|
| 399 |
-
middleware/
|
| 400 |
-
maintenance_mode.rb
|
| 401 |
-
models/
|
| 402 |
-
concerns/
|
| 403 |
-
auditable.rb ← shared PaperTrail concern
|
| 404 |
-
setting.rb ← MUST be loadable before rack_attack.rb runs
|
| 405 |
-
blocked_request_log.rb ← MUST be loadable before rack_attack.rb runs
|
| 406 |
-
blueprints/ ← Blueprinter serializers (API only)
|
| 407 |
-
post_blueprint.rb
|
| 408 |
-
policies/ ← Pundit policies
|
| 409 |
-
post_policy.rb
|
| 410 |
-
user_policy.rb
|
| 411 |
-
javascript/controllers/ ← Stimulus controllers
|
| 412 |
-
language_tabs_controller.js
|
| 413 |
-
cloudinary_upload_controller.js
|
| 414 |
-
cloudinary_media_library_controller.js
|
| 415 |
-
sortable_controller.js
|
| 416 |
-
config/
|
| 417 |
-
initializers/
|
| 418 |
-
cloudinary.rb
|
| 419 |
-
devise.rb
|
| 420 |
-
mobility.rb
|
| 421 |
-
rack_attack.rb ← depends on Setting + BlockedRequestLog models
|
| 422 |
-
sidekiq.rb
|
| 423 |
-
action_mailer.rb
|
| 424 |
-
sidekiq.yml ← queues + scheduled jobs
|
| 425 |
-
spec/
|
| 426 |
-
support/
|
| 427 |
-
cloudinary_helpers.rb
|
| 428 |
-
factories/
|
| 429 |
-
users.rb
|
| 430 |
-
posts.rb
|
| 431 |
-
categories.rb
|
| 432 |
-
tags.rb
|
| 433 |
-
```
|
| 434 |
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
##
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
| Inline Rack::Attack limits as constants | Read limits from `Setting` model (dynamic) |
|
| 455 |
-
| Use `deliver_now` for admin alerts | Use `deliver_later` (goes through Sidekiq) |
|
| 456 |
-
| Write migrations with index missing on foreign keys | Always add `add_index` for FK columns |
|
| 457 |
-
|
| 458 |
-
---
|
| 459 |
-
|
| 460 |
-
## 18. Skill Files Index
|
| 461 |
-
|
| 462 |
-
Detailed skill files are in the `.skills/` directory next to this file.
|
| 463 |
-
|
| 464 |
-
| Skill file | When to read it |
|
| 465 |
-
|---|---|
|
| 466 |
-
| `.skills/SKILL-auth.md` | Working on Devise config, JWT, Pundit policies, user roles |
|
| 467 |
-
| `.skills/SKILL-multilingual.md` | Working on Mobility, translations, locale routing, admin tabs |
|
| 468 |
-
| `.skills/SKILL-cloudinary.md` | Working on uploads, Media Library, transformations, housekeeping |
|
| 469 |
-
| `.skills/SKILL-background-jobs.md` | Working on Sidekiq jobs, scheduling, job patterns |
|
| 470 |
-
| `.skills/SKILL-settings.md` | Working on the dynamic Settings system, SMTP, middleware |
|
| 471 |
-
| `.skills/SKILL-api.md` | Working on API v1 endpoints, serializers, JWT API auth |
|
| 472 |
-
| `.skills/SKILL-testing.md` | Writing RSpec specs, factories, stubs, coverage |
|
| 473 |
-
| `.skills/SKILL-security.md` | Working on Rack::Attack, rate limiting, maintenance mode |
|
| 474 |
-
|
| 475 |
-
---
|
| 476 |
-
|
| 477 |
-
*Last updated: based on Implementation Plan v1.1*
|
|
|
|
| 1 |
+
# AGENTS.md — Rails 8 Blog Architecture v2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
This is the current coding-agent authority for the deployment-v2 branch.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
Historical instructions that referenced SQLite, Redis, Sidekiq, or Cloudinary as the authoritative original media store have been preserved verbatim in:
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
`docs/AGENTS_LEGACY_V1.md`
|
| 8 |
|
| 9 |
+
When historical documentation conflicts with this file or the D0-D12 execution authority, this file and the current architecture-v2 documents win.
|
| 10 |
|
| 11 |
+
## Current production architecture
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
- Rails 8.1 application.
|
| 14 |
+
- PostgreSQL for development/test/production architecture; Neon is the production database target.
|
| 15 |
+
- Solid Queue is the production Active Job backend.
|
| 16 |
+
- Redis and Sidekiq are not production runtime dependencies.
|
| 17 |
+
- Rails MemoryStore is the current process-local cache.
|
| 18 |
+
- Cloudflare R2 is the authoritative Active Storage original-media store in production.
|
| 19 |
+
- Cloudinary Free is a bounded derived mirror only for eligible public Post ActionText raster attachments.
|
| 20 |
+
- Slideshow images use Active Storage/R2 and are not part of the D7 Cloudinary mirror.
|
| 21 |
+
- Hugging Face Docker Space is disposable compute; persistent state must live in Neon/R2/approved external services.
|
| 22 |
+
- Puma runs the Rails app; Solid Queue may run inside Puma through the existing production configuration.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
See `docs/ARCHITECTURE_V2.md` for the production topology and invariants.
|
| 25 |
+
|
| 26 |
+
## Repository and Git rules
|
| 27 |
+
|
| 28 |
+
- Do not work directly on `main`.
|
| 29 |
+
- Current deployment branch: `codex/deployment-v2`.
|
| 30 |
+
- Do not rewrite accepted phase-evidence commits.
|
| 31 |
+
- Do not push, force-push, tag, or create a release without explicit user authorization.
|
| 32 |
+
- Never commit secrets, local databases, runtime logs, provider tokens, `.env`, or Rails master keys.
|
| 33 |
+
- Prefer one coherent semantic commit per completed task/phase.
|
| 34 |
+
|
| 35 |
+
## Database rules
|
| 36 |
+
|
| 37 |
+
- Production uses PostgreSQL via `DATABASE_URL`.
|
| 38 |
+
- Never point destructive local commands at a production Neon URL.
|
| 39 |
+
- Migrations must be additive/reversible until staging validation is complete.
|
| 40 |
+
- Use normal Rails migrations; never rewrite historical migrations.
|
| 41 |
+
- Test/development must not drift back to SQLite.
|
| 42 |
+
- Preserve the production data authority in Neon; a disposable HF filesystem is not a database backup.
|
| 43 |
+
|
| 44 |
+
## Background jobs
|
| 45 |
+
|
| 46 |
+
- All jobs inherit from `ApplicationJob`.
|
| 47 |
+
- Production adapter is Solid Queue.
|
| 48 |
+
- Do not add Redis/Sidekiq runtime dependencies.
|
| 49 |
+
- Jobs must be idempotent where practical and receive stable IDs, not large object payloads.
|
| 50 |
+
- Scheduled publication correctness must not depend solely on an exact worker wake-up; public visibility is due-aware in `Post.publicly_visible`.
|
| 51 |
+
|
| 52 |
+
## Media rules
|
| 53 |
+
|
| 54 |
+
### R2 originals
|
| 55 |
+
|
| 56 |
+
Production Active Storage service is `cloudflare_r2`.
|
| 57 |
+
|
| 58 |
+
Required production variables:
|
| 59 |
+
|
| 60 |
+
- `R2_ENDPOINT`
|
| 61 |
+
- `R2_ACCESS_KEY_ID`
|
| 62 |
+
- `R2_SECRET_ACCESS_KEY`
|
| 63 |
+
- `R2_BUCKET`
|
| 64 |
+
- `R2_REGION`
|
| 65 |
+
|
| 66 |
+
Never expose these values in logs, HTML, API output, evidence, or documentation.
|
| 67 |
+
|
| 68 |
+
### Cloudinary D7 mirror
|
| 69 |
+
|
| 70 |
+
Cloudinary is not the original store.
|
| 71 |
+
|
| 72 |
+
Only eligible JPEG/PNG/WebP/GIF ActionText attachments belonging to a currently publicly-visible Post may be mirrored.
|
| 73 |
+
|
| 74 |
+
D7 rules:
|
| 75 |
+
|
| 76 |
+
- R2 remains authoritative.
|
| 77 |
+
- non-public media must not be mirrored;
|
| 78 |
+
- no arbitrary transform input;
|
| 79 |
+
- fixed eager derivative set only;
|
| 80 |
+
- authenticated Cloudinary delivery;
|
| 81 |
+
- no secret or R2-presign persistence/logging;
|
| 82 |
+
- mirror cleanup must never delete the R2 original.
|
| 83 |
+
|
| 84 |
+
### Slideshows
|
| 85 |
+
|
| 86 |
+
- `Slide` belongs to `Post`.
|
| 87 |
+
- persisted `position` defines canonical ordering.
|
| 88 |
+
- localized caption/alt text uses `SlideTranslation`/Mobility.
|
| 89 |
+
- images use Active Storage/R2.
|
| 90 |
+
- slideshow assets are excluded from D7 Cloudinary ActionText mirroring.
|
| 91 |
+
|
| 92 |
+
## Localization
|
| 93 |
+
|
| 94 |
+
Supported content locales:
|
| 95 |
+
|
| 96 |
+
`en vi ru ja ko th de`
|
| 97 |
+
|
| 98 |
+
- Post title/slug use Mobility table backend.
|
| 99 |
+
- Post ActionText content is locale-specific.
|
| 100 |
+
- Slide caption/alt text is table-backed localized data.
|
| 101 |
+
- Public/API locale behavior must preserve existing fallback rules.
|
| 102 |
+
- Cache keys for locale-sensitive fragments must include locale.
|
| 103 |
+
|
| 104 |
+
## Authentication and authorization
|
| 105 |
+
|
| 106 |
+
- Devise handles web authentication.
|
| 107 |
+
- devise-jwt supports API authentication.
|
| 108 |
+
- Pundit is the authorization layer.
|
| 109 |
+
- Roles: admin, author, reader.
|
| 110 |
+
- Existing admin namespace remains admin-only.
|
| 111 |
+
- Public queries must use the appropriate public visibility scope rather than `Post.all`.
|
| 112 |
+
|
| 113 |
+
## Audit and soft delete
|
| 114 |
+
|
| 115 |
+
- PaperTrail records audited model changes.
|
| 116 |
+
- Post and Comment soft deletion uses Discard.
|
| 117 |
+
- Admin trash/restore actions must remain auditable.
|
| 118 |
+
- Discarded Posts are excluded from public visibility.
|
| 119 |
+
- Security logs are bounded through `BlockedRequestLog.record!`.
|
| 120 |
+
|
| 121 |
+
## Security / throttling / caching
|
| 122 |
+
|
| 123 |
+
Rack::Attack:
|
| 124 |
+
|
| 125 |
+
- login throttles count POST sign-in attempts only;
|
| 126 |
+
- limits/periods come from `Setting.get`;
|
| 127 |
+
- blocked/whitelisted IP lists come from canonical Setting cache keys;
|
| 128 |
+
- Rack::Attack throttle storage remains in-memory.
|
| 129 |
+
|
| 130 |
+
Settings cache:
|
| 131 |
+
|
| 132 |
+
- canonical key is `setting:<key>`;
|
| 133 |
+
- use `Setting.get` rather than direct per-request DB lookups;
|
| 134 |
+
- updates invalidate the canonical key.
|
| 135 |
+
|
| 136 |
+
Do not reintroduce Redis for caching.
|
| 137 |
+
|
| 138 |
+
## Testing contract
|
| 139 |
+
|
| 140 |
+
Framework: RSpec.
|
| 141 |
+
|
| 142 |
+
For production Ruby/JS behavior changes:
|
| 143 |
+
|
| 144 |
+
1. write focused failing test;
|
| 145 |
+
2. prove RED for the intended reason;
|
| 146 |
+
3. implement minimum fix;
|
| 147 |
+
4. prove focused GREEN;
|
| 148 |
+
5. run related regression;
|
| 149 |
+
6. run full suite;
|
| 150 |
+
7. confirm coverage floor;
|
| 151 |
+
8. run quality/security gates.
|
| 152 |
+
|
| 153 |
+
Current locked minimum coverage:
|
| 154 |
+
|
| 155 |
+
- line: 41.83%
|
| 156 |
+
- branch: 50.22%
|
| 157 |
+
|
| 158 |
+
Standard final gates:
|
| 159 |
+
|
| 160 |
+
```bash
|
| 161 |
+
bundle exec rspec
|
| 162 |
+
bundle exec rubocop
|
| 163 |
+
bundle exec brakeman --no-pager
|
| 164 |
+
bundle exec bundler-audit check --update
|
| 165 |
+
bin/importmap audit
|
| 166 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
Network calls in tests must be stubbed unless a phase explicitly defines controlled provider acceptance.
|
| 169 |
+
|
| 170 |
+
## Production/release authority
|
| 171 |
+
|
| 172 |
+
D12 qualification documentation:
|
| 173 |
+
|
| 174 |
+
- `docs/ARCHITECTURE_V2.md`
|
| 175 |
+
- `docs/BACKUP_RESTORE_V2.md`
|
| 176 |
+
- `docs/RELEASE_READINESS_V2.md`
|
| 177 |
+
|
| 178 |
+
A local qualification result does not itself authorize:
|
| 179 |
+
|
| 180 |
+
- Git push;
|
| 181 |
+
- remote tag;
|
| 182 |
+
- GitHub release;
|
| 183 |
+
- production database mutation;
|
| 184 |
+
- R2 destructive cleanup.
|
| 185 |
+
|
| 186 |
+
Those actions require explicit human authorization.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
D8_SOURCE_AUTHORITY.txt
DELETED
|
@@ -1,2 +0,0 @@
|
|
| 1 |
-
D8 source authority
|
| 2 |
-
commit: 9e3137fa587494795d8e922a7c73ed6ee10e20c9
|
|
|
|
|
|
|
|
|
app/blueprints/post_blueprint.rb
CHANGED
|
@@ -35,8 +35,15 @@ class PostBlueprint < Blueprinter::Base
|
|
| 35 |
end
|
| 36 |
|
| 37 |
field :slideshow_assets do |post|
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
end
|
| 41 |
end
|
| 42 |
end
|
|
|
|
| 35 |
end
|
| 36 |
|
| 37 |
field :slideshow_assets do |post|
|
| 38 |
+
post.slides.map do |slide|
|
| 39 |
+
{
|
| 40 |
+
id: slide.id,
|
| 41 |
+
position: slide.position,
|
| 42 |
+
image_url: Rails.application.routes.url_helpers.rails_blob_path(slide.image, only_path: true),
|
| 43 |
+
caption: slide.caption_for(I18n.locale),
|
| 44 |
+
alt_text: slide.alt_text_for(I18n.locale)
|
| 45 |
+
}
|
| 46 |
+
end
|
| 47 |
end
|
| 48 |
end
|
| 49 |
end
|
app/controllers/admin/audit_logs_controller.rb
CHANGED
|
@@ -4,8 +4,16 @@ module Admin
|
|
| 4 |
@blocked_logs = BlockedRequestLog.recent
|
| 5 |
@blocked_logs = @blocked_logs.by_ip(params[:ip]) if params[:ip].present?
|
| 6 |
@blocked_logs = @blocked_logs.by_reason(params[:reason]) if params[:reason].present?
|
| 7 |
-
|
| 8 |
@pagy, @blocked_logs = pagy(@blocked_logs, items: 50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
end
|
| 10 |
end
|
| 11 |
end
|
|
|
|
| 4 |
@blocked_logs = BlockedRequestLog.recent
|
| 5 |
@blocked_logs = @blocked_logs.by_ip(params[:ip]) if params[:ip].present?
|
| 6 |
@blocked_logs = @blocked_logs.by_reason(params[:reason]) if params[:reason].present?
|
|
|
|
| 7 |
@pagy, @blocked_logs = pagy(@blocked_logs, items: 50)
|
| 8 |
+
|
| 9 |
+
@versions = PaperTrail::Version.order(created_at: :desc)
|
| 10 |
+
@versions = @versions.where(whodunnit: params[:user_id].to_s) if params[:user_id].present?
|
| 11 |
+
@versions = @versions.where(item_type: params[:item_type]) if params[:item_type].present?
|
| 12 |
+
@versions = @versions.limit(100)
|
| 13 |
+
end
|
| 14 |
+
|
| 15 |
+
def show
|
| 16 |
+
@version = PaperTrail::Version.find(params[:id])
|
| 17 |
end
|
| 18 |
end
|
| 19 |
end
|
app/controllers/admin/posts_controller.rb
CHANGED
|
@@ -1,9 +1,13 @@
|
|
| 1 |
module Admin
|
| 2 |
class PostsController < AdminController
|
| 3 |
-
before_action :set_post, only: [ :show, :edit, :update, :destroy, :publish, :schedule ]
|
| 4 |
|
| 5 |
def index
|
| 6 |
-
@pagy, @posts = pagy(policy_scope(Post).includes(:user, :category, :post_translations))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
end
|
| 8 |
|
| 9 |
def show
|
|
@@ -49,10 +53,16 @@ module Admin
|
|
| 49 |
|
| 50 |
def destroy
|
| 51 |
authorize @post
|
| 52 |
-
@post.discard
|
| 53 |
redirect_to admin_posts_path, notice: t("admin.posts.destroyed")
|
| 54 |
end
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def publish
|
| 57 |
authorize @post, :publish?
|
| 58 |
@post.update!(status: "published", published_at: Time.current)
|
|
@@ -68,7 +78,7 @@ module Admin
|
|
| 68 |
private
|
| 69 |
|
| 70 |
def set_post
|
| 71 |
-
@post = Post.find(params[:id])
|
| 72 |
end
|
| 73 |
|
| 74 |
def post_params
|
|
|
|
| 1 |
module Admin
|
| 2 |
class PostsController < AdminController
|
| 3 |
+
before_action :set_post, only: [ :show, :edit, :update, :destroy, :publish, :schedule, :restore ]
|
| 4 |
|
| 5 |
def index
|
| 6 |
+
@pagy, @posts = pagy(policy_scope(Post).kept.includes(:user, :category, :post_translations))
|
| 7 |
+
end
|
| 8 |
+
|
| 9 |
+
def trash
|
| 10 |
+
@pagy, @posts = pagy(policy_scope(Post).discarded.includes(:user, :category, :post_translations))
|
| 11 |
end
|
| 12 |
|
| 13 |
def show
|
|
|
|
| 53 |
|
| 54 |
def destroy
|
| 55 |
authorize @post
|
| 56 |
+
@post.discard!
|
| 57 |
redirect_to admin_posts_path, notice: t("admin.posts.destroyed")
|
| 58 |
end
|
| 59 |
|
| 60 |
+
def restore
|
| 61 |
+
authorize @post, :update?
|
| 62 |
+
@post.undiscard!
|
| 63 |
+
redirect_to admin_posts_path, notice: t("admin.posts.restored", default: "Post restored successfully")
|
| 64 |
+
end
|
| 65 |
+
|
| 66 |
def publish
|
| 67 |
authorize @post, :publish?
|
| 68 |
@post.update!(status: "published", published_at: Time.current)
|
|
|
|
| 78 |
private
|
| 79 |
|
| 80 |
def set_post
|
| 81 |
+
@post = Post.friendly.find(params[:id])
|
| 82 |
end
|
| 83 |
|
| 84 |
def post_params
|
app/controllers/admin/slides_controller.rb
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module Admin
|
| 2 |
+
class SlidesController < AdminController
|
| 3 |
+
before_action :set_post
|
| 4 |
+
before_action :authorize_post
|
| 5 |
+
before_action :set_slide, only: [ :update, :destroy ]
|
| 6 |
+
|
| 7 |
+
def create
|
| 8 |
+
@slide = @post.slides.build(position: next_position)
|
| 9 |
+
@slide.image.attach(slide_params[:image]) if slide_params[:image].present?
|
| 10 |
+
assign_translations(@slide)
|
| 11 |
+
|
| 12 |
+
if @slide.save
|
| 13 |
+
redirect_to edit_admin_post_path(@post), notice: "Slide added successfully"
|
| 14 |
+
else
|
| 15 |
+
render plain: @slide.errors.full_messages.to_sentence, status: :unprocessable_entity
|
| 16 |
+
end
|
| 17 |
+
end
|
| 18 |
+
|
| 19 |
+
def update
|
| 20 |
+
@slide.image.attach(slide_params[:image]) if slide_params[:image].present?
|
| 21 |
+
assign_translations(@slide)
|
| 22 |
+
|
| 23 |
+
if @slide.save
|
| 24 |
+
redirect_to edit_admin_post_path(@post), notice: "Slide updated successfully"
|
| 25 |
+
else
|
| 26 |
+
render plain: @slide.errors.full_messages.to_sentence, status: :unprocessable_entity
|
| 27 |
+
end
|
| 28 |
+
end
|
| 29 |
+
|
| 30 |
+
def destroy
|
| 31 |
+
@slide.destroy!
|
| 32 |
+
normalize_positions!
|
| 33 |
+
redirect_to edit_admin_post_path(@post), notice: "Slide removed successfully"
|
| 34 |
+
end
|
| 35 |
+
|
| 36 |
+
def reorder
|
| 37 |
+
slide_ids = Array(params[:slide_ids]).map(&:to_i)
|
| 38 |
+
current_ids = @post.slides.pluck(:id)
|
| 39 |
+
|
| 40 |
+
unless valid_reorder?(slide_ids, current_ids)
|
| 41 |
+
return render plain: "Invalid slideshow order", status: :unprocessable_entity
|
| 42 |
+
end
|
| 43 |
+
|
| 44 |
+
Slide.transaction do
|
| 45 |
+
slides_by_id = @post.slides.where(id: slide_ids).index_by(&:id)
|
| 46 |
+
|
| 47 |
+
slide_ids.each_with_index do |slide_id, index|
|
| 48 |
+
slides_by_id.fetch(slide_id).update_columns(position: -(index + 1))
|
| 49 |
+
end
|
| 50 |
+
|
| 51 |
+
slide_ids.each_with_index do |slide_id, index|
|
| 52 |
+
slides_by_id.fetch(slide_id).update_columns(position: index + 1)
|
| 53 |
+
end
|
| 54 |
+
end
|
| 55 |
+
|
| 56 |
+
redirect_to edit_admin_post_path(@post), notice: "Slides reordered successfully"
|
| 57 |
+
end
|
| 58 |
+
|
| 59 |
+
private
|
| 60 |
+
|
| 61 |
+
def set_post
|
| 62 |
+
@post = Post.friendly.find(params[:post_id])
|
| 63 |
+
end
|
| 64 |
+
|
| 65 |
+
def authorize_post
|
| 66 |
+
authorize @post, :update?
|
| 67 |
+
end
|
| 68 |
+
|
| 69 |
+
def set_slide
|
| 70 |
+
@slide = @post.slides.find(params[:id])
|
| 71 |
+
end
|
| 72 |
+
|
| 73 |
+
def slide_params
|
| 74 |
+
params.require(:slide).permit(:image, translations: {})
|
| 75 |
+
end
|
| 76 |
+
|
| 77 |
+
def assign_translations(slide)
|
| 78 |
+
translations = slide_params[:translations]
|
| 79 |
+
return unless translations.present?
|
| 80 |
+
|
| 81 |
+
translations.each do |locale, attributes|
|
| 82 |
+
next unless I18n.available_locales.include?(locale.to_sym)
|
| 83 |
+
|
| 84 |
+
safe_attributes = attributes.to_h.slice("caption", "alt_text")
|
| 85 |
+
|
| 86 |
+
Mobility.with_locale(locale) do
|
| 87 |
+
slide.caption = safe_attributes["caption"]
|
| 88 |
+
slide.alt_text = safe_attributes["alt_text"]
|
| 89 |
+
end
|
| 90 |
+
end
|
| 91 |
+
end
|
| 92 |
+
|
| 93 |
+
def next_position
|
| 94 |
+
@post.slides.maximum(:position).to_i + 1
|
| 95 |
+
end
|
| 96 |
+
|
| 97 |
+
def valid_reorder?(slide_ids, current_ids)
|
| 98 |
+
slide_ids.length == current_ids.length &&
|
| 99 |
+
slide_ids.uniq.length == slide_ids.length &&
|
| 100 |
+
slide_ids.sort == current_ids.sort
|
| 101 |
+
end
|
| 102 |
+
|
| 103 |
+
def normalize_positions!
|
| 104 |
+
slides = @post.slides.reload.to_a
|
| 105 |
+
Slide.transaction do
|
| 106 |
+
slides.each_with_index do |slide, index|
|
| 107 |
+
slide.update_columns(position: -(index + 1))
|
| 108 |
+
end
|
| 109 |
+
slides.each_with_index do |slide, index|
|
| 110 |
+
slide.update_columns(position: index + 1)
|
| 111 |
+
end
|
| 112 |
+
end
|
| 113 |
+
end
|
| 114 |
+
end
|
| 115 |
+
end
|
app/controllers/api/v1/posts_controller.rb
CHANGED
|
@@ -16,7 +16,10 @@ module Api
|
|
| 16 |
end
|
| 17 |
|
| 18 |
def show
|
| 19 |
-
post = Post.publicly_visible
|
|
|
|
|
|
|
|
|
|
| 20 |
authorize post
|
| 21 |
|
| 22 |
# Track unique views (skip if session not available - e.g., API requests)
|
|
|
|
| 16 |
end
|
| 17 |
|
| 18 |
def show
|
| 19 |
+
post = Post.publicly_visible
|
| 20 |
+
.includes(slides: [ :slide_translations, { image_attachment: :blob } ])
|
| 21 |
+
.friendly
|
| 22 |
+
.find(params[:slug])
|
| 23 |
authorize post
|
| 24 |
|
| 25 |
# Track unique views (skip if session not available - e.g., API requests)
|
app/controllers/application_controller.rb
CHANGED
|
@@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base
|
|
| 6 |
stale_when_importmap_changes
|
| 7 |
|
| 8 |
before_action :set_locale
|
|
|
|
| 9 |
before_action :authenticate_user!, unless: :devise_or_static_controller?
|
| 10 |
|
| 11 |
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
|
@@ -18,6 +19,17 @@ class ApplicationController < ActionController::Base
|
|
| 18 |
|
| 19 |
private
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def devise_or_static_controller?
|
| 22 |
is_a?(DeviseController) || is_a?(StaticController)
|
| 23 |
end
|
|
|
|
| 6 |
stale_when_importmap_changes
|
| 7 |
|
| 8 |
before_action :set_locale
|
| 9 |
+
before_action :set_paper_trail_whodunnit
|
| 10 |
before_action :authenticate_user!, unless: :devise_or_static_controller?
|
| 11 |
|
| 12 |
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
|
|
|
| 19 |
|
| 20 |
private
|
| 21 |
|
| 22 |
+
def user_for_paper_trail
|
| 23 |
+
current_user&.id&.to_s
|
| 24 |
+
end
|
| 25 |
+
|
| 26 |
+
def info_for_paper_trail
|
| 27 |
+
{
|
| 28 |
+
request_ip: request.remote_ip,
|
| 29 |
+
request_path: request.path
|
| 30 |
+
}
|
| 31 |
+
end
|
| 32 |
+
|
| 33 |
def devise_or_static_controller?
|
| 34 |
is_a?(DeviseController) || is_a?(StaticController)
|
| 35 |
end
|
app/controllers/posts_controller.rb
CHANGED
|
@@ -11,7 +11,7 @@ class PostsController < ApplicationController
|
|
| 11 |
def show
|
| 12 |
@post = policy_scope(Post).publicly_visible.friendly.find(params[:slug])
|
| 13 |
authorize @post
|
| 14 |
-
@comments = @post.comments.
|
| 15 |
@current_reaction = @post.reactions.find_by(user: current_user) if user_signed_in?
|
| 16 |
impressionist(@post, nil, unique: [ :ip_address ])
|
| 17 |
end
|
|
|
|
| 11 |
def show
|
| 12 |
@post = policy_scope(Post).publicly_visible.friendly.find(params[:slug])
|
| 13 |
authorize @post
|
| 14 |
+
@comments = @post.comments.includes(:user).order(created_at: :asc)
|
| 15 |
@current_reaction = @post.reactions.find_by(user: current_user) if user_signed_in?
|
| 16 |
impressionist(@post, nil, unique: [ :ip_address ])
|
| 17 |
end
|
app/models/blocked_request_log.rb
CHANGED
|
@@ -1,8 +1,50 @@
|
|
| 1 |
class BlockedRequestLog < ApplicationRecord
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
validates :ip, presence: true
|
| 3 |
validates :reason, presence: true
|
| 4 |
|
| 5 |
scope :recent, -> { order(created_at: :desc) }
|
| 6 |
scope :by_ip, ->(ip) { where(ip: ip) }
|
| 7 |
scope :by_reason, ->(reason) { where(reason: reason) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
end
|
|
|
|
| 1 |
class BlockedRequestLog < ApplicationRecord
|
| 2 |
+
MAX_RETAINED_ROWS = 10_000
|
| 3 |
+
MAX_PATH_BYTES = 2_048
|
| 4 |
+
MAX_USER_AGENT_BYTES = 1_024
|
| 5 |
+
|
| 6 |
validates :ip, presence: true
|
| 7 |
validates :reason, presence: true
|
| 8 |
|
| 9 |
scope :recent, -> { order(created_at: :desc) }
|
| 10 |
scope :by_ip, ->(ip) { where(ip: ip) }
|
| 11 |
scope :by_reason, ->(reason) { where(reason: reason) }
|
| 12 |
+
|
| 13 |
+
def self.record!(ip:, reason:, path: nil, user_agent: nil)
|
| 14 |
+
transaction do
|
| 15 |
+
log = create!(
|
| 16 |
+
ip:,
|
| 17 |
+
reason:,
|
| 18 |
+
path: bounded_string(path, MAX_PATH_BYTES),
|
| 19 |
+
user_agent: bounded_string(user_agent, MAX_USER_AGENT_BYTES)
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
prune_excess!
|
| 23 |
+
log
|
| 24 |
+
end
|
| 25 |
+
end
|
| 26 |
+
|
| 27 |
+
def self.prune_excess!
|
| 28 |
+
excess_ids = order(created_at: :desc, id: :desc)
|
| 29 |
+
.offset(MAX_RETAINED_ROWS)
|
| 30 |
+
.pluck(:id)
|
| 31 |
+
where(id: excess_ids).delete_all if excess_ids.any?
|
| 32 |
+
end
|
| 33 |
+
|
| 34 |
+
def self.bounded_string(value, max_bytes)
|
| 35 |
+
return nil if value.nil?
|
| 36 |
+
|
| 37 |
+
source = value.to_s.scrub
|
| 38 |
+
return source if source.bytesize <= max_bytes
|
| 39 |
+
|
| 40 |
+
bounded = +""
|
| 41 |
+
source.each_char do |character|
|
| 42 |
+
break if bounded.bytesize + character.bytesize > max_bytes
|
| 43 |
+
|
| 44 |
+
bounded << character
|
| 45 |
+
end
|
| 46 |
+
bounded
|
| 47 |
+
end
|
| 48 |
+
|
| 49 |
+
private_class_method :bounded_string, :prune_excess!
|
| 50 |
end
|
app/models/comment.rb
CHANGED
|
@@ -1,8 +1,13 @@
|
|
| 1 |
class Comment < ApplicationRecord
|
| 2 |
include Discard::Model
|
|
|
|
| 3 |
|
| 4 |
belongs_to :user
|
| 5 |
belongs_to :post, counter_cache: true
|
| 6 |
|
| 7 |
validates :body, presence: true
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
end
|
|
|
|
| 1 |
class Comment < ApplicationRecord
|
| 2 |
include Discard::Model
|
| 3 |
+
include Auditable
|
| 4 |
|
| 5 |
belongs_to :user
|
| 6 |
belongs_to :post, counter_cache: true
|
| 7 |
|
| 8 |
validates :body, presence: true
|
| 9 |
+
|
| 10 |
+
def display_body
|
| 11 |
+
discarded? ? I18n.t("comments.deleted_placeholder") : body
|
| 12 |
+
end
|
| 13 |
end
|
app/models/post.rb
CHANGED
|
@@ -3,6 +3,7 @@ class Post < ApplicationRecord
|
|
| 3 |
belongs_to :category
|
| 4 |
has_many :post_tags, dependent: :destroy
|
| 5 |
has_many :tags, through: :post_tags
|
|
|
|
| 6 |
has_many :comments, dependent: :destroy
|
| 7 |
has_many :reactions, dependent: :destroy
|
| 8 |
has_many :post_translations, class_name: "PostTranslation", dependent: :destroy
|
|
@@ -25,17 +26,19 @@ class Post < ApplicationRecord
|
|
| 25 |
unique: :ip_address
|
| 26 |
|
| 27 |
include Discard::Model
|
|
|
|
| 28 |
|
| 29 |
STATUSES = %w[draft pending scheduled published].freeze
|
| 30 |
|
| 31 |
validates :status, inclusion: { in: STATUSES }, presence: true
|
| 32 |
validate :scheduled_at_must_be_future, if: :scheduled?
|
| 33 |
|
| 34 |
-
scope :published, -> { where(status: "published").where("published_at <= ?", Time.current) }
|
| 35 |
scope :publicly_visible, -> {
|
| 36 |
now = Time.current
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
published_posts.or(due_scheduled_posts)
|
| 41 |
}
|
|
|
|
| 3 |
belongs_to :category
|
| 4 |
has_many :post_tags, dependent: :destroy
|
| 5 |
has_many :tags, through: :post_tags
|
| 6 |
+
has_many :slides, -> { order(:position, :id) }, dependent: :destroy
|
| 7 |
has_many :comments, dependent: :destroy
|
| 8 |
has_many :reactions, dependent: :destroy
|
| 9 |
has_many :post_translations, class_name: "PostTranslation", dependent: :destroy
|
|
|
|
| 26 |
unique: :ip_address
|
| 27 |
|
| 28 |
include Discard::Model
|
| 29 |
+
include Auditable
|
| 30 |
|
| 31 |
STATUSES = %w[draft pending scheduled published].freeze
|
| 32 |
|
| 33 |
validates :status, inclusion: { in: STATUSES }, presence: true
|
| 34 |
validate :scheduled_at_must_be_future, if: :scheduled?
|
| 35 |
|
| 36 |
+
scope :published, -> { kept.where(status: "published").where("published_at <= ?", Time.current) }
|
| 37 |
scope :publicly_visible, -> {
|
| 38 |
now = Time.current
|
| 39 |
+
visible = kept
|
| 40 |
+
published_posts = visible.where(status: "published").where("published_at <= ?", now)
|
| 41 |
+
due_scheduled_posts = visible.where(status: "scheduled").where("scheduled_at <= ?", now)
|
| 42 |
|
| 43 |
published_posts.or(due_scheduled_posts)
|
| 44 |
}
|
app/models/setting.rb
CHANGED
|
@@ -64,6 +64,5 @@ class Setting < ApplicationRecord
|
|
| 64 |
|
| 65 |
def clear_cache
|
| 66 |
Rails.cache.delete("setting:#{key}")
|
| 67 |
-
Rails.cache.delete(key) # For Rack::Attack bare key lookups
|
| 68 |
end
|
| 69 |
end
|
|
|
|
| 64 |
|
| 65 |
def clear_cache
|
| 66 |
Rails.cache.delete("setting:#{key}")
|
|
|
|
| 67 |
end
|
| 68 |
end
|
app/models/slide.rb
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Slide < ApplicationRecord
|
| 2 |
+
SUPPORTED_IMAGE_TYPES = %w[image/jpeg image/png image/webp image/gif].freeze
|
| 3 |
+
|
| 4 |
+
belongs_to :post
|
| 5 |
+
has_many :slide_translations, dependent: :destroy
|
| 6 |
+
has_one_attached :image
|
| 7 |
+
|
| 8 |
+
extend Mobility
|
| 9 |
+
translates :caption, :alt_text, backend: :table
|
| 10 |
+
|
| 11 |
+
validates :position,
|
| 12 |
+
presence: true,
|
| 13 |
+
numericality: { only_integer: true, greater_than: 0 },
|
| 14 |
+
uniqueness: { scope: :post_id }
|
| 15 |
+
|
| 16 |
+
validate :image_must_be_attached
|
| 17 |
+
validate :image_must_be_supported_raster
|
| 18 |
+
validate :within_slideshow_limit, on: :create
|
| 19 |
+
|
| 20 |
+
def caption_for(locale)
|
| 21 |
+
localized_value_for(:caption, locale)
|
| 22 |
+
end
|
| 23 |
+
|
| 24 |
+
def alt_text_for(locale)
|
| 25 |
+
localized_value_for(:alt_text, locale)
|
| 26 |
+
end
|
| 27 |
+
|
| 28 |
+
private
|
| 29 |
+
|
| 30 |
+
def localized_value_for(attribute, locale)
|
| 31 |
+
requested = locale.to_sym
|
| 32 |
+
direct = Mobility.with_locale(requested) { public_send(attribute, fallback: false).presence }
|
| 33 |
+
return direct if direct
|
| 34 |
+
|
| 35 |
+
default_locale = I18n.default_locale.to_sym
|
| 36 |
+
return nil if requested == default_locale
|
| 37 |
+
|
| 38 |
+
Mobility.with_locale(default_locale) { public_send(attribute, fallback: false).presence }
|
| 39 |
+
end
|
| 40 |
+
|
| 41 |
+
def image_must_be_attached
|
| 42 |
+
errors.add(:image, :blank) unless image.attached?
|
| 43 |
+
end
|
| 44 |
+
|
| 45 |
+
def image_must_be_supported_raster
|
| 46 |
+
return unless image.attached?
|
| 47 |
+
return if SUPPORTED_IMAGE_TYPES.include?(image.blob.content_type)
|
| 48 |
+
|
| 49 |
+
errors.add(:image, :invalid)
|
| 50 |
+
end
|
| 51 |
+
|
| 52 |
+
def within_slideshow_limit
|
| 53 |
+
return unless post
|
| 54 |
+
|
| 55 |
+
max_size = Setting.get("slideshow_max_size", 5).to_i
|
| 56 |
+
current_count = if post.persisted?
|
| 57 |
+
self.class.where(post_id: post.id).count
|
| 58 |
+
else
|
| 59 |
+
post.slides.count { |slide| slide != self && !slide.marked_for_destruction? }
|
| 60 |
+
end
|
| 61 |
+
return if current_count < max_size
|
| 62 |
+
|
| 63 |
+
errors.add(:base, "Slideshow cannot contain more than #{max_size} slides")
|
| 64 |
+
end
|
| 65 |
+
end
|
app/models/slide_translation.rb
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class SlideTranslation < ApplicationRecord
|
| 2 |
+
belongs_to :slide
|
| 3 |
+
|
| 4 |
+
validates :locale,
|
| 5 |
+
presence: true,
|
| 6 |
+
inclusion: { in: ->(_) { I18n.available_locales.map(&:to_s) } },
|
| 7 |
+
uniqueness: { scope: :slide_id }
|
| 8 |
+
end
|
app/views/admin/audit_logs/index.html.erb
CHANGED
|
@@ -72,6 +72,54 @@
|
|
| 72 |
<% end %>
|
| 73 |
</div>
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
<!-- Pagination -->
|
| 76 |
<% if @pagy.pages > 1 %>
|
| 77 |
<div class="mt-6">
|
|
|
|
| 72 |
<% end %>
|
| 73 |
</div>
|
| 74 |
|
| 75 |
+
<div class="mt-10">
|
| 76 |
+
<div class="mb-4 flex items-center justify-between">
|
| 77 |
+
<div>
|
| 78 |
+
<h2 class="text-2xl font-bold text-gray-900">Change History</h2>
|
| 79 |
+
<p class="text-sm text-gray-600">PaperTrail versions for audited records.</p>
|
| 80 |
+
</div>
|
| 81 |
+
</div>
|
| 82 |
+
|
| 83 |
+
<div class="bg-white rounded-lg shadow p-4 mb-4">
|
| 84 |
+
<%= form_with url: admin_audit_logs_path, method: :get, class: "grid gap-4 sm:grid-cols-3" do |f| %>
|
| 85 |
+
<%= f.text_field :user_id, placeholder: "User ID", value: params[:user_id], class: "w-full px-4 py-2 border rounded-lg" %>
|
| 86 |
+
<%= f.select :item_type, options_for_select([["All models", ""], ["Post", "Post"], ["Setting", "Setting"], ["Comment", "Comment"]], params[:item_type]), {}, class: "w-full px-4 py-2 border rounded-lg" %>
|
| 87 |
+
<div class="flex gap-2">
|
| 88 |
+
<%= f.submit "Filter history", class: "px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700" %>
|
| 89 |
+
<%= link_to "Clear", admin_audit_logs_path, class: "px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300" %>
|
| 90 |
+
</div>
|
| 91 |
+
<% end %>
|
| 92 |
+
</div>
|
| 93 |
+
|
| 94 |
+
<div class="bg-white rounded-lg shadow overflow-hidden">
|
| 95 |
+
<table class="min-w-full divide-y divide-gray-200">
|
| 96 |
+
<thead class="bg-gray-50">
|
| 97 |
+
<tr>
|
| 98 |
+
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Time</th>
|
| 99 |
+
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Item</th>
|
| 100 |
+
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Event</th>
|
| 101 |
+
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">User</th>
|
| 102 |
+
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Path</th>
|
| 103 |
+
</tr>
|
| 104 |
+
</thead>
|
| 105 |
+
<tbody class="bg-white divide-y divide-gray-200">
|
| 106 |
+
<% @versions.each do |version| %>
|
| 107 |
+
<tr>
|
| 108 |
+
<td class="px-6 py-4 text-sm text-gray-700"><%= version.created_at %></td>
|
| 109 |
+
<td class="px-6 py-4 text-sm"><%= link_to "#{version.item_type} ##{version.item_id}", admin_audit_log_path(version), class: "text-primary-600 hover:text-primary-800" %></td>
|
| 110 |
+
<td class="px-6 py-4 text-sm text-gray-700"><%= version.event %></td>
|
| 111 |
+
<td class="px-6 py-4 text-sm text-gray-700"><%= version.whodunnit.presence || "system" %></td>
|
| 112 |
+
<td class="px-6 py-4 text-sm text-gray-500"><%= version.request_path %></td>
|
| 113 |
+
</tr>
|
| 114 |
+
<% end %>
|
| 115 |
+
</tbody>
|
| 116 |
+
</table>
|
| 117 |
+
<% if @versions.empty? %>
|
| 118 |
+
<div class="py-10 text-center text-gray-500">No change history found.</div>
|
| 119 |
+
<% end %>
|
| 120 |
+
</div>
|
| 121 |
+
</div>
|
| 122 |
+
|
| 123 |
<!-- Pagination -->
|
| 124 |
<% if @pagy.pages > 1 %>
|
| 125 |
<div class="mt-6">
|
app/views/admin/audit_logs/show.html.erb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<div class="container mx-auto px-4 py-8">
|
| 2 |
+
<div class="mb-6">
|
| 3 |
+
<%= link_to "← Audit Logs", admin_audit_logs_path, class: "text-sm font-medium text-primary-600 hover:text-primary-800" %>
|
| 4 |
+
<h1 class="mt-3 text-3xl font-bold text-gray-900">Audit Version</h1>
|
| 5 |
+
</div>
|
| 6 |
+
|
| 7 |
+
<dl class="grid gap-4 rounded-lg bg-white p-6 shadow sm:grid-cols-2">
|
| 8 |
+
<div><dt class="text-sm text-gray-500">Item</dt><dd class="font-medium"><%= @version.item_type %> #<%= @version.item_id %></dd></div>
|
| 9 |
+
<div><dt class="text-sm text-gray-500">Event</dt><dd class="font-medium"><%= @version.event %></dd></div>
|
| 10 |
+
<div><dt class="text-sm text-gray-500">User</dt><dd class="font-medium"><%= @version.whodunnit.presence || "system" %></dd></div>
|
| 11 |
+
<div><dt class="text-sm text-gray-500">Time</dt><dd class="font-medium"><%= @version.created_at %></dd></div>
|
| 12 |
+
<div><dt class="text-sm text-gray-500">Request IP</dt><dd class="font-medium"><%= @version.request_ip %></dd></div>
|
| 13 |
+
<div><dt class="text-sm text-gray-500">Request path</dt><dd class="font-medium"><%= @version.request_path %></dd></div>
|
| 14 |
+
</dl>
|
| 15 |
+
|
| 16 |
+
<div class="mt-6 rounded-lg bg-white p-6 shadow">
|
| 17 |
+
<h2 class="text-lg font-semibold text-gray-900">Changes</h2>
|
| 18 |
+
<pre class="mt-3 overflow-x-auto whitespace-pre-wrap text-sm text-gray-700"><%= @version.object_changes.presence || "No object changes recorded." %></pre>
|
| 19 |
+
</div>
|
| 20 |
+
</div>
|
app/views/admin/posts/_slideshow.html.erb
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<% slides = post.slides.to_a %>
|
| 2 |
+
<% max_size = Setting.get("slideshow_max_size", 5).to_i %>
|
| 3 |
+
<% ordered_ids = slides.map(&:id) %>
|
| 4 |
+
|
| 5 |
+
<section data-testid="slideshow-admin" class="mt-8 space-y-6 rounded-xl border border-gray-200 bg-white p-6">
|
| 6 |
+
<div class="flex flex-wrap items-center justify-between gap-3">
|
| 7 |
+
<div>
|
| 8 |
+
<h2 class="text-lg font-bold text-gray-900">Slideshow</h2>
|
| 9 |
+
<p class="mt-1 text-sm text-gray-600">Ordered localized images for this post.</p>
|
| 10 |
+
</div>
|
| 11 |
+
<p class="text-sm font-semibold text-gray-700"><%= slides.size %> / <%= max_size %></p>
|
| 12 |
+
</div>
|
| 13 |
+
|
| 14 |
+
<% if slides.size >= max_size %>
|
| 15 |
+
<div class="rounded-lg bg-amber-50 px-4 py-3 text-sm font-medium text-amber-800">
|
| 16 |
+
Slideshow limit reached
|
| 17 |
+
</div>
|
| 18 |
+
<% else %>
|
| 19 |
+
<%= form_with url: admin_post_slides_path(post, locale: I18n.locale),
|
| 20 |
+
scope: :slide,
|
| 21 |
+
method: :post,
|
| 22 |
+
local: true,
|
| 23 |
+
html: { multipart: true, class: "space-y-4 rounded-lg border border-dashed border-gray-300 p-4" } do |form| %>
|
| 24 |
+
<div>
|
| 25 |
+
<%= form.label :image, "Slide image", class: "block text-sm font-semibold text-gray-700" %>
|
| 26 |
+
<%= form.file_field :image, accept: "image/jpeg,image/png,image/webp,image/gif",
|
| 27 |
+
class: "mt-1 block w-full text-sm text-gray-700" %>
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
<div class="grid gap-4 md:grid-cols-2">
|
| 31 |
+
<% I18n.available_locales.each do |locale| %>
|
| 32 |
+
<div class="rounded-lg border border-gray-200 p-3">
|
| 33 |
+
<p class="mb-2 text-xs font-bold uppercase tracking-wide text-gray-500"><%= locale %></p>
|
| 34 |
+
<%= label_tag "slide_translations_#{locale}_caption", "Caption", class: "block text-xs font-semibold text-gray-600" %>
|
| 35 |
+
<%= text_field_tag "slide[translations][#{locale}][caption]", nil,
|
| 36 |
+
id: "slide_translations_#{locale}_caption",
|
| 37 |
+
class: "mt-1 block w-full rounded-lg border-gray-300 text-sm" %>
|
| 38 |
+
<%= label_tag "slide_translations_#{locale}_alt_text", "Alt text", class: "mt-3 block text-xs font-semibold text-gray-600" %>
|
| 39 |
+
<%= text_field_tag "slide[translations][#{locale}][alt_text]", nil,
|
| 40 |
+
id: "slide_translations_#{locale}_alt_text",
|
| 41 |
+
class: "mt-1 block w-full rounded-lg border-gray-300 text-sm" %>
|
| 42 |
+
</div>
|
| 43 |
+
<% end %>
|
| 44 |
+
</div>
|
| 45 |
+
|
| 46 |
+
<%= form.submit "Add slide", class: "rounded-lg bg-indigo-600 px-4 py-2 text-sm font-semibold text-white" %>
|
| 47 |
+
<% end %>
|
| 48 |
+
<% end %>
|
| 49 |
+
|
| 50 |
+
<div class="space-y-4">
|
| 51 |
+
<% slides.each_with_index do |slide, index| %>
|
| 52 |
+
<article class="rounded-xl border border-gray-200 p-4">
|
| 53 |
+
<div class="flex flex-col gap-4 lg:flex-row">
|
| 54 |
+
<div class="w-full lg:w-48">
|
| 55 |
+
<%= image_tag slide.image, alt: slide.alt_text_for(I18n.locale).to_s,
|
| 56 |
+
class: "aspect-video w-full rounded-lg object-cover" %>
|
| 57 |
+
<p class="mt-2 text-xs font-semibold text-gray-500">Position <%= slide.position %></p>
|
| 58 |
+
</div>
|
| 59 |
+
|
| 60 |
+
<div class="flex-1 space-y-4">
|
| 61 |
+
<%= form_with model: [:admin, post, slide], local: true, class: "space-y-4" do |form| %>
|
| 62 |
+
<div>
|
| 63 |
+
<%= form.label :image, "Replace image", class: "block text-sm font-semibold text-gray-700" %>
|
| 64 |
+
<%= form.file_field :image, accept: "image/jpeg,image/png,image/webp,image/gif",
|
| 65 |
+
class: "mt-1 block w-full text-sm text-gray-700" %>
|
| 66 |
+
</div>
|
| 67 |
+
|
| 68 |
+
<div class="grid gap-4 md:grid-cols-2">
|
| 69 |
+
<% I18n.available_locales.each do |locale| %>
|
| 70 |
+
<% caption = Mobility.with_locale(locale) { slide.caption(fallback: false) } %>
|
| 71 |
+
<% alt_text = Mobility.with_locale(locale) { slide.alt_text(fallback: false) } %>
|
| 72 |
+
<div class="rounded-lg border border-gray-200 p-3">
|
| 73 |
+
<p class="mb-2 text-xs font-bold uppercase tracking-wide text-gray-500"><%= locale %></p>
|
| 74 |
+
<%= label_tag "slide_#{slide.id}_#{locale}_caption", "Caption", class: "block text-xs font-semibold text-gray-600" %>
|
| 75 |
+
<%= text_field_tag "slide[translations][#{locale}][caption]", caption,
|
| 76 |
+
id: "slide_#{slide.id}_#{locale}_caption",
|
| 77 |
+
class: "mt-1 block w-full rounded-lg border-gray-300 text-sm" %>
|
| 78 |
+
<%= label_tag "slide_#{slide.id}_#{locale}_alt_text", "Alt text", class: "mt-3 block text-xs font-semibold text-gray-600" %>
|
| 79 |
+
<%= text_field_tag "slide[translations][#{locale}][alt_text]", alt_text,
|
| 80 |
+
id: "slide_#{slide.id}_#{locale}_alt_text",
|
| 81 |
+
class: "mt-1 block w-full rounded-lg border-gray-300 text-sm" %>
|
| 82 |
+
</div>
|
| 83 |
+
<% end %>
|
| 84 |
+
</div>
|
| 85 |
+
|
| 86 |
+
<%= form.submit "Save slide", class: "rounded-lg bg-indigo-600 px-4 py-2 text-sm font-semibold text-white" %>
|
| 87 |
+
<% end %>
|
| 88 |
+
|
| 89 |
+
<div class="flex flex-wrap gap-2">
|
| 90 |
+
<% if index.positive? %>
|
| 91 |
+
<% up_ids = ordered_ids.dup.tap { |ids| ids[index - 1], ids[index] = ids[index], ids[index - 1] } %>
|
| 92 |
+
<%= form_with url: reorder_admin_post_slides_path(post, locale: I18n.locale), method: :patch, local: true do %>
|
| 93 |
+
<% up_ids.each do |slide_id| %>
|
| 94 |
+
<%= hidden_field_tag "slide_ids[]", slide_id %>
|
| 95 |
+
<% end %>
|
| 96 |
+
<%= submit_tag "Move up", class: "rounded-lg border border-gray-300 px-3 py-2 text-sm font-semibold text-gray-700" %>
|
| 97 |
+
<% end %>
|
| 98 |
+
<% end %>
|
| 99 |
+
|
| 100 |
+
<% if index < slides.length - 1 %>
|
| 101 |
+
<% down_ids = ordered_ids.dup.tap { |ids| ids[index], ids[index + 1] = ids[index + 1], ids[index] } %>
|
| 102 |
+
<%= form_with url: reorder_admin_post_slides_path(post, locale: I18n.locale), method: :patch, local: true do %>
|
| 103 |
+
<% down_ids.each do |slide_id| %>
|
| 104 |
+
<%= hidden_field_tag "slide_ids[]", slide_id %>
|
| 105 |
+
<% end %>
|
| 106 |
+
<%= submit_tag "Move down", class: "rounded-lg border border-gray-300 px-3 py-2 text-sm font-semibold text-gray-700" %>
|
| 107 |
+
<% end %>
|
| 108 |
+
<% end %>
|
| 109 |
+
|
| 110 |
+
<%= button_to "Delete slide",
|
| 111 |
+
admin_post_slide_path(post, slide, locale: I18n.locale),
|
| 112 |
+
method: :delete,
|
| 113 |
+
class: "rounded-lg px-3 py-2 text-sm font-semibold text-rose-700" %>
|
| 114 |
+
</div>
|
| 115 |
+
</div>
|
| 116 |
+
</div>
|
| 117 |
+
</article>
|
| 118 |
+
<% end %>
|
| 119 |
+
</div>
|
| 120 |
+
</section>
|
app/views/admin/posts/edit.html.erb
CHANGED
|
@@ -21,3 +21,5 @@
|
|
| 21 |
</div>
|
| 22 |
<% end %>
|
| 23 |
</div>
|
|
|
|
|
|
|
|
|
| 21 |
</div>
|
| 22 |
<% end %>
|
| 23 |
</div>
|
| 24 |
+
|
| 25 |
+
<%= render "slideshow", post: @post %>
|
app/views/admin/posts/index.html.erb
CHANGED
|
@@ -3,12 +3,15 @@
|
|
| 3 |
<h1 class="text-2xl font-bold text-gray-900"><%= t("admin.posts.title") %></h1>
|
| 4 |
<p class="mt-1 text-sm text-gray-600"><%= t("admin.posts.subtitle") %></p>
|
| 5 |
</div>
|
| 6 |
-
<
|
| 7 |
-
<
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
</div>
|
| 13 |
|
| 14 |
<div class="bg-white shadow rounded-lg overflow-hidden">
|
|
|
|
| 3 |
<h1 class="text-2xl font-bold text-gray-900"><%= t("admin.posts.title") %></h1>
|
| 4 |
<p class="mt-1 text-sm text-gray-600"><%= t("admin.posts.subtitle") %></p>
|
| 5 |
</div>
|
| 6 |
+
<div class="flex items-center gap-3">
|
| 7 |
+
<%= link_to "Trash", trash_admin_posts_path, class: "text-sm font-medium text-gray-600 hover:text-gray-900" %>
|
| 8 |
+
<%= link_to new_admin_post_path, class: "inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary-600 hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500" do %>
|
| 9 |
+
<svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 10 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"/>
|
| 11 |
+
</svg>
|
| 12 |
+
<%= t("admin.posts.new") %>
|
| 13 |
+
<% end %>
|
| 14 |
+
</div>
|
| 15 |
</div>
|
| 16 |
|
| 17 |
<div class="bg-white shadow rounded-lg overflow-hidden">
|
app/views/admin/posts/trash.html.erb
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<div class="mb-6 flex items-center justify-between">
|
| 2 |
+
<div>
|
| 3 |
+
<h1 class="text-2xl font-bold text-gray-900">Post Trash</h1>
|
| 4 |
+
<p class="mt-1 text-sm text-gray-600">Soft-deleted posts that can be restored.</p>
|
| 5 |
+
</div>
|
| 6 |
+
<%= link_to "Back to posts", admin_posts_path, class: "text-sm font-medium text-primary-600 hover:text-primary-700" %>
|
| 7 |
+
</div>
|
| 8 |
+
|
| 9 |
+
<div class="bg-white shadow rounded-lg overflow-hidden">
|
| 10 |
+
<table class="min-w-full divide-y divide-gray-200">
|
| 11 |
+
<thead class="bg-gray-50">
|
| 12 |
+
<tr>
|
| 13 |
+
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Title</th>
|
| 14 |
+
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Deleted at</th>
|
| 15 |
+
<th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">Actions</th>
|
| 16 |
+
</tr>
|
| 17 |
+
</thead>
|
| 18 |
+
<tbody class="bg-white divide-y divide-gray-200">
|
| 19 |
+
<% @posts.each do |post| %>
|
| 20 |
+
<tr>
|
| 21 |
+
<td class="px-6 py-4 text-sm font-medium text-gray-900"><%= post.title %></td>
|
| 22 |
+
<td class="px-6 py-4 text-sm text-gray-500"><%= post.discarded_at %></td>
|
| 23 |
+
<td class="px-6 py-4 text-right">
|
| 24 |
+
<%= button_to "Restore", restore_admin_post_path(post), method: :patch, class: "text-green-700 hover:text-green-900" %>
|
| 25 |
+
</td>
|
| 26 |
+
</tr>
|
| 27 |
+
<% end %>
|
| 28 |
+
</tbody>
|
| 29 |
+
</table>
|
| 30 |
+
<% if @posts.empty? %>
|
| 31 |
+
<div class="py-10 text-center text-gray-500">Trash is empty.</div>
|
| 32 |
+
<% end %>
|
| 33 |
+
|
| 34 |
+
<% if @pagy %>
|
| 35 |
+
<div class="px-6 py-4 border-t border-gray-200">
|
| 36 |
+
<%== @pagy.series_nav %>
|
| 37 |
+
</div>
|
| 38 |
+
<% end %>
|
| 39 |
+
</div>
|
app/views/posts/show.html.erb
CHANGED
|
@@ -10,6 +10,22 @@
|
|
| 10 |
<h1 class="mt-3 text-4xl font-bold tracking-tight text-slate-900 sm:text-5xl"><%= @post.title %></h1>
|
| 11 |
<div class="mt-6"><%= render "posts/post_meta", post: @post %></div>
|
| 12 |
<div class="prose prose-slate mt-10 max-w-none"><%= @post.content_for(I18n.locale) %></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
<section aria-labelledby="engagement-heading" class="mt-12 space-y-8 border-t border-slate-200 pt-8">
|
| 14 |
<h2 id="engagement-heading" class="text-2xl font-bold text-slate-900"><%= t("engagement.title") %></h2>
|
| 15 |
<% if user_signed_in? %>
|
|
@@ -31,7 +47,7 @@
|
|
| 31 |
<p class="text-slate-600"><%= link_to t("shared.sign_in"), new_user_session_path(locale: I18n.locale), class: "font-semibold text-indigo-600" %> <%= t("engagement.sign_in_prompt") %></p>
|
| 32 |
<% end %>
|
| 33 |
<div class="space-y-4">
|
| 34 |
-
<% @comments.each do |comment| %><article class="rounded-xl bg-slate-100 p-4"><p class="font-semibold text-slate-900"><%= comment.user.full_name %></p><p class="mt-1 text-slate-700"><%= comment.
|
| 35 |
</div>
|
| 36 |
</section>
|
| 37 |
<div class="mt-12 border-t border-slate-200 pt-6"><%= link_to t("posts.back"), posts_path(locale: I18n.locale), class: "text-sm font-semibold text-indigo-600 hover:text-indigo-700" %></div>
|
|
|
|
| 10 |
<h1 class="mt-3 text-4xl font-bold tracking-tight text-slate-900 sm:text-5xl"><%= @post.title %></h1>
|
| 11 |
<div class="mt-6"><%= render "posts/post_meta", post: @post %></div>
|
| 12 |
<div class="prose prose-slate mt-10 max-w-none"><%= @post.content_for(I18n.locale) %></div>
|
| 13 |
+
|
| 14 |
+
<% if @post.slides.any? %>
|
| 15 |
+
<section data-testid="post-slideshow" aria-label="Slideshow" class="mt-10 space-y-6">
|
| 16 |
+
<% @post.slides.each do |slide| %>
|
| 17 |
+
<figure class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
|
| 18 |
+
<%= image_tag slide.image,
|
| 19 |
+
alt: slide.alt_text_for(I18n.locale).to_s,
|
| 20 |
+
class: "aspect-video w-full object-cover" %>
|
| 21 |
+
<% if (caption = slide.caption_for(I18n.locale)).present? %>
|
| 22 |
+
<figcaption class="px-4 py-3 text-sm text-slate-700"><%= caption %></figcaption>
|
| 23 |
+
<% end %>
|
| 24 |
+
</figure>
|
| 25 |
+
<% end %>
|
| 26 |
+
</section>
|
| 27 |
+
<% end %>
|
| 28 |
+
|
| 29 |
<section aria-labelledby="engagement-heading" class="mt-12 space-y-8 border-t border-slate-200 pt-8">
|
| 30 |
<h2 id="engagement-heading" class="text-2xl font-bold text-slate-900"><%= t("engagement.title") %></h2>
|
| 31 |
<% if user_signed_in? %>
|
|
|
|
| 47 |
<p class="text-slate-600"><%= link_to t("shared.sign_in"), new_user_session_path(locale: I18n.locale), class: "font-semibold text-indigo-600" %> <%= t("engagement.sign_in_prompt") %></p>
|
| 48 |
<% end %>
|
| 49 |
<div class="space-y-4">
|
| 50 |
+
<% @comments.each do |comment| %><article class="rounded-xl bg-slate-100 p-4"><p class="font-semibold text-slate-900"><%= comment.user.full_name %></p><p class="mt-1 text-slate-700"><%= comment.display_body %></p></article><% end %>
|
| 51 |
</div>
|
| 52 |
</section>
|
| 53 |
<div class="mt-12 border-t border-slate-200 pt-6"><%= link_to t("posts.back"), posts_path(locale: I18n.locale), class: "text-sm font-semibold text-indigo-600 hover:text-indigo-700" %></div>
|
config/database.yml
CHANGED
|
@@ -10,6 +10,7 @@ development:
|
|
| 10 |
test:
|
| 11 |
<<: *default
|
| 12 |
database: rails_8_blog_test
|
|
|
|
| 13 |
|
| 14 |
production:
|
| 15 |
<<: *default
|
|
|
|
| 10 |
test:
|
| 11 |
<<: *default
|
| 12 |
database: rails_8_blog_test
|
| 13 |
+
prepared_statements: false
|
| 14 |
|
| 15 |
production:
|
| 16 |
<<: *default
|
config/initializers/rack_attack.rb
CHANGED
|
@@ -1,78 +1,61 @@
|
|
| 1 |
class Rack::Attack
|
| 2 |
-
# In-memory store for throttling (no external cache dependency)
|
| 3 |
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
| 4 |
|
| 5 |
-
# Helper to read settings from cache
|
| 6 |
def self.setting(key, default)
|
| 7 |
-
|
| 8 |
-
Setting.find_by(key: key)&.typed_value || default
|
| 9 |
-
end
|
| 10 |
end
|
| 11 |
|
| 12 |
-
# Whitelist: bypass all throttles for whitelisted IPs
|
| 13 |
safelist("whitelist_ips") do |req|
|
| 14 |
-
|
| 15 |
-
Setting.find_by(key: "whitelist_ips")&.typed_value || []
|
| 16 |
-
end
|
| 17 |
-
allowed.include?(req.ip)
|
| 18 |
end
|
| 19 |
|
| 20 |
-
# Blocklist: block IPs from database
|
| 21 |
blocklist("blocked_ips") do |req|
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
end
|
| 27 |
|
| 28 |
-
# Throttle: login attempts by IP
|
| 29 |
throttle("login/ip", limit: ->(_req) { setting("rack_login_limit", 5) },
|
| 30 |
period: ->(_req) { setting("rack_login_period", 60).seconds }) do |req|
|
| 31 |
-
|
| 32 |
-
req.ip
|
| 33 |
-
end
|
| 34 |
end
|
| 35 |
|
| 36 |
-
# Throttle: login attempts by email
|
| 37 |
throttle("login/email", limit: ->(_req) { setting("rack_login_limit", 5) },
|
| 38 |
period: ->(_req) { setting("rack_login_period", 60).seconds }) do |req|
|
| 39 |
-
if
|
| 40 |
req.params.dig("user", "email")&.downcase&.strip
|
| 41 |
end
|
| 42 |
end
|
| 43 |
|
| 44 |
-
# Custom response for throttled requests
|
| 45 |
self.throttled_responder = lambda do |request|
|
| 46 |
-
|
| 47 |
-
BlockedRequestLog.create!(
|
| 48 |
ip: request.ip,
|
| 49 |
path: request.path,
|
| 50 |
reason: "throttled",
|
| 51 |
user_agent: request.get_header("HTTP_USER_AGENT")
|
| 52 |
)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
AdminNotificationJob.perform_later("IP #{request.ip} throttled on #{request.path}")
|
| 56 |
|
| 57 |
-
# Return 429 response
|
| 58 |
[ 429, { "Content-Type" => "application/json" },
|
| 59 |
-
[ { error: "Too Many Requests", retry_after: 60 }.to_json ] ]
|
| 60 |
end
|
| 61 |
|
| 62 |
-
# Custom response for blocklisted requests
|
| 63 |
self.blocklisted_responder = lambda do |request|
|
| 64 |
-
|
| 65 |
-
BlockedRequestLog.create!(
|
| 66 |
ip: request.ip,
|
| 67 |
path: request.path,
|
| 68 |
reason: "blocklisted",
|
| 69 |
user_agent: request.get_header("HTTP_USER_AGENT")
|
| 70 |
)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
AdminNotificationJob.perform_later("Blocklisted IP #{request.ip} attempted access to #{request.path}")
|
| 74 |
|
| 75 |
-
# Return 403 response
|
| 76 |
[ 403, { "Content-Type" => "application/json" },
|
| 77 |
[ { error: "Forbidden" }.to_json ] ]
|
| 78 |
end
|
|
|
|
| 1 |
class Rack::Attack
|
|
|
|
| 2 |
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
| 3 |
|
|
|
|
| 4 |
def self.setting(key, default)
|
| 5 |
+
Setting.get(key, default)
|
|
|
|
|
|
|
| 6 |
end
|
| 7 |
|
|
|
|
| 8 |
safelist("whitelist_ips") do |req|
|
| 9 |
+
setting("whitelist_ips", []).include?(req.ip)
|
|
|
|
|
|
|
|
|
|
| 10 |
end
|
| 11 |
|
|
|
|
| 12 |
blocklist("blocked_ips") do |req|
|
| 13 |
+
setting("blocked_ips", []).include?(req.ip)
|
| 14 |
+
end
|
| 15 |
+
|
| 16 |
+
login_request = lambda do |req|
|
| 17 |
+
req.post? && (
|
| 18 |
+
req.path.match?(%r{^/(?:[a-z]{2}/)?auth/sign_in$}) ||
|
| 19 |
+
req.path == "/api/v1/auth/sign_in"
|
| 20 |
+
)
|
| 21 |
end
|
| 22 |
|
|
|
|
| 23 |
throttle("login/ip", limit: ->(_req) { setting("rack_login_limit", 5) },
|
| 24 |
period: ->(_req) { setting("rack_login_period", 60).seconds }) do |req|
|
| 25 |
+
req.ip if login_request.call(req)
|
|
|
|
|
|
|
| 26 |
end
|
| 27 |
|
|
|
|
| 28 |
throttle("login/email", limit: ->(_req) { setting("rack_login_limit", 5) },
|
| 29 |
period: ->(_req) { setting("rack_login_period", 60).seconds }) do |req|
|
| 30 |
+
if login_request.call(req)
|
| 31 |
req.params.dig("user", "email")&.downcase&.strip
|
| 32 |
end
|
| 33 |
end
|
| 34 |
|
|
|
|
| 35 |
self.throttled_responder = lambda do |request|
|
| 36 |
+
log = BlockedRequestLog.record!(
|
|
|
|
| 37 |
ip: request.ip,
|
| 38 |
path: request.path,
|
| 39 |
reason: "throttled",
|
| 40 |
user_agent: request.get_header("HTTP_USER_AGENT")
|
| 41 |
)
|
| 42 |
|
| 43 |
+
AdminNotificationJob.perform_later("IP #{request.ip} throttled on #{log.path}")
|
|
|
|
| 44 |
|
|
|
|
| 45 |
[ 429, { "Content-Type" => "application/json" },
|
| 46 |
+
[ { error: "Too Many Requests", retry_after: setting("rack_login_period", 60) }.to_json ] ]
|
| 47 |
end
|
| 48 |
|
|
|
|
| 49 |
self.blocklisted_responder = lambda do |request|
|
| 50 |
+
log = BlockedRequestLog.record!(
|
|
|
|
| 51 |
ip: request.ip,
|
| 52 |
path: request.path,
|
| 53 |
reason: "blocklisted",
|
| 54 |
user_agent: request.get_header("HTTP_USER_AGENT")
|
| 55 |
)
|
| 56 |
|
| 57 |
+
AdminNotificationJob.perform_later("Blocklisted IP #{request.ip} attempted access to #{log.path}")
|
|
|
|
| 58 |
|
|
|
|
| 59 |
[ 403, { "Content-Type" => "application/json" },
|
| 60 |
[ { error: "Forbidden" }.to_json ] ]
|
| 61 |
end
|
config/locales/en.yml
CHANGED
|
@@ -3,6 +3,9 @@ en:
|
|
| 3 |
name: "Rails 8 Blog"
|
| 4 |
tagline: "Write. Publish. Inspire."
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
nav:
|
| 7 |
home: "Home"
|
| 8 |
admin: "Admin"
|
|
|
|
| 3 |
name: "Rails 8 Blog"
|
| 4 |
tagline: "Write. Publish. Inspire."
|
| 5 |
|
| 6 |
+
comments:
|
| 7 |
+
deleted_placeholder: "This comment has been deleted."
|
| 8 |
+
|
| 9 |
nav:
|
| 10 |
home: "Home"
|
| 11 |
admin: "Admin"
|
config/routes.rb
CHANGED
|
@@ -15,9 +15,18 @@ Rails.application.routes.draw do
|
|
| 15 |
namespace :admin do
|
| 16 |
get "/", to: "dashboard#index", as: :root
|
| 17 |
resources :posts do
|
|
|
|
|
|
|
|
|
|
| 18 |
member do
|
| 19 |
patch :publish
|
| 20 |
patch :schedule
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
end
|
| 22 |
end
|
| 23 |
resources :users
|
|
|
|
| 15 |
namespace :admin do
|
| 16 |
get "/", to: "dashboard#index", as: :root
|
| 17 |
resources :posts do
|
| 18 |
+
collection do
|
| 19 |
+
get :trash
|
| 20 |
+
end
|
| 21 |
member do
|
| 22 |
patch :publish
|
| 23 |
patch :schedule
|
| 24 |
+
patch :restore
|
| 25 |
+
end
|
| 26 |
+
resources :slides, only: [ :create, :update, :destroy ] do
|
| 27 |
+
collection do
|
| 28 |
+
patch :reorder
|
| 29 |
+
end
|
| 30 |
end
|
| 31 |
end
|
| 32 |
resources :users
|