Skip to content

Commit c9d8541

Browse files
authored
Merge pull request #74 from blocknotes/support-for-rails-8.1
Support for Rails 8.1
2 parents b61ae9f + 3051529 commit c9d8541

14 files changed

Lines changed: 309 additions & 80 deletions

File tree

.github/workflows/linters.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
env:
3939
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4040
run: |
41-
reviewdog -fail-on-error -reporter=github-pr-review -runners=brakeman,fasterer,rubocop
41+
reviewdog -fail-on-error -reporter=github-pr-review -runners=fasterer,rubocop
4242
43+
# NOTE: removed brakeman due some issues
4344
# NOTE: check with: reviewdog -fail-on-error -reporter=github-pr-review -runners=fasterer -diff="git diff" -tee
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
name: Specs MSSQL Rails 8.1
3+
4+
on:
5+
pull_request:
6+
branches: [main]
7+
push:
8+
branches: [main]
9+
10+
jobs:
11+
tests:
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
ruby: ['3.2', '3.4']
17+
18+
env:
19+
DB_TEST: mssql
20+
RAILS_VERSION: 8.1
21+
22+
services:
23+
postgres:
24+
image: mcr.microsoft.com/mssql/server:2022-latest
25+
env:
26+
ACCEPT_EULA: 'Y'
27+
SA_PASSWORD: Pa%%w0rd
28+
ports:
29+
- 1433:1433
30+
31+
steps:
32+
- name: Install dependencies
33+
run: sudo apt-get update -qq && sudo apt-get install -yqq freetds-dev freetds-bin libvips-dev
34+
35+
- name: Checkout repository
36+
uses: actions/checkout@v4
37+
38+
- name: Set up Ruby
39+
uses: ruby/setup-ruby@v1
40+
with:
41+
ruby-version: ${{ matrix.ruby }}
42+
bundler-cache: true
43+
44+
- name: Database setup
45+
run: bin/rails db:create db:migrate db:test:prepare
46+
47+
- name: Run tests
48+
run: bin/rspec --profile
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
name: Specs MySQL Rails 8.1
3+
4+
on:
5+
pull_request:
6+
branches: [main]
7+
push:
8+
branches: [main]
9+
10+
jobs:
11+
tests:
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
ruby: ['3.2', '3.4']
17+
18+
env:
19+
DB_TEST: mysql
20+
RAILS_VERSION: 8.1
21+
22+
services:
23+
mysql:
24+
image: mysql
25+
env:
26+
MYSQL_USER: root
27+
MYSQL_PASSWORD: root
28+
# options: >-
29+
# --health-cmd="mysqladmin ping"
30+
# --health-interval 10s
31+
# --health-timeout 5s
32+
# --health-retries 5
33+
ports:
34+
- 3306:3306
35+
36+
steps:
37+
- name: Install dependencies
38+
run: sudo apt-get update -qq && sudo apt-get install -yqq freetds-dev freetds-bin libvips-dev
39+
40+
- name: Checkout repository
41+
uses: actions/checkout@v4
42+
43+
- name: Set up Ruby
44+
uses: ruby/setup-ruby@v1
45+
with:
46+
ruby-version: ${{ matrix.ruby }}
47+
bundler-cache: true
48+
49+
- name: Start database service
50+
run: sudo /etc/init.d/mysql start
51+
52+
- name: Database setup
53+
run: bin/rails db:create db:migrate db:test:prepare
54+
55+
- name: Run tests
56+
run: bin/rspec --profile
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
name: Specs Postgres Rails 8.1
3+
4+
on:
5+
pull_request:
6+
branches: [main]
7+
push:
8+
branches: [main]
9+
10+
jobs:
11+
tests:
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
ruby: ['3.2', '3.4']
17+
18+
env:
19+
DB_TEST: postgres
20+
RAILS_VERSION: 8.1
21+
22+
services:
23+
postgres:
24+
image: postgres:latest
25+
env:
26+
POSTGRES_USER: postgres
27+
POSTGRES_PASSWORD: password
28+
options: >-
29+
--health-cmd pg_isready
30+
--health-interval 10s
31+
--health-timeout 5s
32+
--health-retries 5
33+
ports:
34+
- 5432:5432
35+
36+
steps:
37+
- name: Install dependencies
38+
run: sudo apt-get update -qq && sudo apt-get install -yqq freetds-dev freetds-bin libvips-dev
39+
40+
- name: Checkout repository
41+
uses: actions/checkout@v4
42+
43+
- name: Set up Ruby
44+
uses: ruby/setup-ruby@v1
45+
with:
46+
ruby-version: ${{ matrix.ruby }}
47+
bundler-cache: true
48+
49+
- name: Database setup
50+
run: bin/rails db:create db:migrate db:test:prepare
51+
52+
- name: Run tests
53+
run: bin/rspec --profile
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
name: Specs SQLite Rails 8.1
3+
4+
on:
5+
pull_request:
6+
branches: [main]
7+
push:
8+
branches: [main]
9+
10+
jobs:
11+
tests:
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
ruby: ['3.2', '3.3', '3.4']
17+
18+
env:
19+
DB_TEST: sqlite
20+
RAILS_VERSION: 8.1
21+
22+
steps:
23+
- name: Install dependencies
24+
run: sudo apt-get update -qq && sudo apt-get install -yqq freetds-dev freetds-bin libvips-dev
25+
26+
- name: Checkout repository
27+
uses: actions/checkout@v4
28+
29+
- name: Set up Ruby
30+
uses: ruby/setup-ruby@v1
31+
with:
32+
ruby-version: ${{ matrix.ruby }}
33+
bundler-cache: true
34+
35+
- name: Database setup
36+
run: bin/rails db:create db:migrate db:test:prepare
37+
38+
- name: Run tests
39+
run: bin/rspec --profile

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ RSpec/NestedGroups:
4545
# Default is 3
4646
Max: 5
4747

48+
RSpec/Output:
49+
Exclude:
50+
- spec/dummy/bin/*
51+
4852
Style/ClassAndModuleChildren:
4953
Exclude:
5054
- 'lib/active_storage/service/db_service.rb'

app/controllers/active_storage_db/files_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def show
1717
def update
1818
if (token = decode_verified_token)
1919
file_uploaded = upload_file(token, body: request.body)
20-
head(file_uploaded ? :no_content : :unprocessable_entity)
20+
head(file_uploaded ? :no_content : unprocessable)
2121
else
2222
head(:not_found)
2323
end
2424
rescue ActiveStorage::IntegrityError
25-
head(:unprocessable_entity)
25+
head(unprocessable)
2626
end
2727

2828
private
@@ -59,5 +59,9 @@ def upload_file(token, body:)
5959
db_service.upload(token[:key], request.body, checksum: token[:checksum])
6060
true
6161
end
62+
63+
def unprocessable
64+
Gem::Version.new(Rails.version) >= Gem::Version.new("7.1") ? :unprocessable_content : :unprocessable_entity
65+
end
6266
end
6367
end

lib/active_storage/service/db_service.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

3-
require 'active_storage/service/db_service_rails60'
4-
require 'active_storage/service/db_service_rails61'
5-
require 'active_storage/service/db_service_rails70'
3+
require "active_storage/service/db_service_rails60"
4+
require "active_storage/service/db_service_rails61"
5+
require "active_storage/service/db_service_rails70"
66

77
module ActiveStorage
88
# Wraps a DB table as an Active Storage service. See ActiveStorage::Service
@@ -19,7 +19,7 @@ class Service::DBService < Service
1919
# :nocov:
2020

2121
def initialize(public: false, **)
22-
@chunk_size = ENV.fetch('ASDB_CHUNK_SIZE') { 1.megabytes }
22+
@chunk_size = ENV.fetch("ASDB_CHUNK_SIZE") { 1.megabytes }
2323
@public = public
2424
end
2525

@@ -69,7 +69,7 @@ def delete_prefixed(prefix)
6969
instrument :delete_prefixed, prefix: prefix do
7070
comment = "DBService#delete_prefixed"
7171
sanitized_prefix = "#{ApplicationRecord.sanitize_sql_like(prefix)}%"
72-
::ActiveStorageDB::File.annotate(comment).where('ref LIKE ?', sanitized_prefix).destroy_all
72+
::ActiveStorageDB::File.annotate(comment).where("ref LIKE ?", sanitized_prefix).destroy_all
7373
end
7474
end
7575

@@ -90,7 +90,7 @@ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, chec
9090
content_type: content_type,
9191
content_length: content_length,
9292
checksum: checksum,
93-
service_name: respond_to?(:name) ? name : 'db'
93+
service_name: respond_to?(:name) ? name : "db"
9494
},
9595
expires_in: expires_in,
9696
purpose: :blob_token
@@ -103,17 +103,25 @@ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, chec
103103
end
104104

105105
def headers_for_direct_upload(_key, content_type:, **)
106-
{ 'Content-Type' => content_type }
106+
{ "Content-Type" => content_type }
107107
end
108108

109109
private
110110

111111
def adapter_sqlite?
112-
@adapter_sqlite ||= ActiveStorageDB::File.connection.adapter_name == 'SQLite'
112+
@adapter_sqlite ||= active_storage_db_adapter_name == "SQLite"
113113
end
114114

115115
def adapter_sqlserver?
116-
@adapter_sqlserver ||= ActiveStorageDB::File.connection.adapter_name == 'SQLServer'
116+
@adapter_sqlserver ||= active_storage_db_adapter_name == "SQLServer"
117+
end
118+
119+
def active_storage_db_adapter_name
120+
if ActiveStorageDB::File.respond_to?(:lease_connection)
121+
ActiveStorageDB::File.lease_connection.adapter_name
122+
else
123+
ActiveStorageDB::File.connection.adapter_name
124+
end
117125
end
118126

119127
def generate_url(key, expires_in:, filename:, content_type:, disposition:)
@@ -123,7 +131,7 @@ def generate_url(key, expires_in:, filename:, content_type:, disposition:)
123131
key: key,
124132
disposition: content_disposition,
125133
content_type: content_type,
126-
service_name: respond_to?(:name) ? name : 'db'
134+
service_name: respond_to?(:name) ? name : "db"
127135
},
128136
expires_in: expires_in,
129137
purpose: :blob_key
@@ -162,10 +170,10 @@ def object_for(key, fields: nil)
162170
end
163171

164172
def stream(key)
165-
data_size = adapter_sqlserver? ? 'DATALENGTH(data)' : 'OCTET_LENGTH(data)'
173+
data_size = adapter_sqlserver? ? "DATALENGTH(data)" : "OCTET_LENGTH(data)"
166174
size = object_for(key, fields: "#{data_size} AS size")&.size || raise(ActiveStorage::FileNotFoundError)
167175
(size / @chunk_size.to_f).ceil.times.each do |i|
168-
range = (i * @chunk_size..((i + 1) * @chunk_size) - 1)
176+
range = (i * @chunk_size)..(((i + 1) * @chunk_size) - 1)
169177
yield download_chunk(key, range)
170178
end
171179
end

spec/dummy/app/controllers/posts_controller.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ def create
2828

2929
respond_to do |format|
3030
if @post.save
31-
format.html { redirect_to @post, notice: 'Post was successfully created.' }
31+
format.html { redirect_to @post, notice: "Post was successfully created." }
3232
format.json { render :show, status: :created, location: @post }
3333
else
3434
format.html { render :new }
35-
format.json { render json: { errors: @post.errors }, status: :unprocessable_entity }
35+
format.json { render json: { errors: @post.errors }, status: unprocessable }
3636
end
3737
end
3838
end
@@ -42,11 +42,11 @@ def create
4242
def update
4343
respond_to do |format|
4444
if @post.update(post_params)
45-
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
45+
format.html { redirect_to @post, notice: "Post was successfully updated." }
4646
format.json { render :show, status: :ok, location: @post }
4747
else
4848
format.html { render :edit }
49-
format.json { render json: { errors: @post.errors }, status: :unprocessable_entity }
49+
format.json { render json: { errors: @post.errors }, status: unprocessable }
5050
end
5151
end
5252
end
@@ -56,7 +56,7 @@ def update
5656
def destroy
5757
@post.destroy
5858
respond_to do |format|
59-
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
59+
format.html { redirect_to posts_url, notice: "Post was successfully destroyed." }
6060
format.json { head :no_content }
6161
end
6262
end
@@ -70,4 +70,8 @@ def load_post
7070
def post_params
7171
params.fetch(:post) { {} }.permit!
7272
end
73+
74+
def unprocessable
75+
Gem::Version.new(Rails.version) >= Gem::Version.new("7.1") ? :unprocessable_content : :unprocessable_entity
76+
end
7377
end

spec/dummy/app/models/application_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
class ApplicationRecord < ActiveRecord::Base
4-
if Gem::Version.new(Rails.version) >= Gem::Version.new('7.0')
4+
if Rails::VERSION::MAJOR >= 7
55
primary_abstract_class
66
else
77
self.abstract_class = true

0 commit comments

Comments
 (0)