Skip to content

Commit b1e0c68

Browse files
authored
Support Rails 6.1 (#53)
* Support Rails 6.1 * Rubocop * Fix tests * Fix reek
1 parent 08bef0d commit b1e0c68

File tree

16 files changed

+56
-35
lines changed

16 files changed

+56
-35
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Gemfile.lock
2+
gemfiles/*.lock
23
spec/dummy/log
34
spec/dummy/db/**.sqlite3
5+
spec/dummy/tmp/storage/**/*
46
/coverage/

.reek.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ detectors:
44
exclude: []
55
BooleanParameter:
66
enabled: true
7-
exclude: []
7+
exclude:
8+
- ActiveStorageSupport::SupportForBase64#has_many_base64_attached
9+
- ActiveStorageSupport::SupportForBase64#has_one_base64_attached
810
ClassVariable:
911
enabled: false
1012
exclude: []

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Metrics/ModuleLength:
4242
Max: 200
4343

4444
Metrics/LineLength:
45-
Max: 100
45+
Max: 120
4646
# To make it possible to copy or click on URIs in the code, we allow lines
4747
# containing a URI to be longer than Max.
4848
AllowURI: true

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ language: ruby
33
rvm:
44
- 2.5.0
55
- 2.6.0
6+
- 2.7.0
67
- ruby-head
78

9+
gemfile:
10+
- gemfiles/rails_6_1.gemfile
11+
- gemfiles/rails_head.gemfile
12+
813
dist: bionic
914

1015
jobs:

active_storage_base64.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'active_storage_base64'
3-
s.version = '1.1.0'
3+
s.version = '1.2.0'
44
s.summary = 'Base64 support for ActiveStorage'
55
s.description = s.summary
66

@@ -12,10 +12,10 @@ Gem::Specification.new do |s|
1212
s.homepage = 'https://github.com/rootstrap/active-storage-base64'
1313
s.email = 'ricardo@rootstrap.com'
1414

15-
s.required_ruby_version = '>= 2.2.2'
15+
s.required_ruby_version = '>= 2.5.0'
1616

1717
# Dependencies
18-
s.add_dependency 'rails', '~> 6.0'
18+
s.add_dependency 'rails', '~> 6.1'
1919

2020
# Development dependencies
2121
s.add_development_dependency 'pry-rails', '~> 0.3.6'

gemfiles/RailsHeadGemfile

Lines changed: 0 additions & 13 deletions
This file was deleted.

gemfiles/rails_6_1.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'rails', '~> 6.1.0'
4+
5+
gemspec path: '..'

gemfiles/rails_head.gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://rubygems.org'
2+
3+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
4+
5+
gem 'rails', github: 'rails/rails', branch: 'master'
6+
7+
gemspec path: '..'

lib/active_storage_support/support_for_base64.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ module ActiveStorageSupport
55
module SupportForBase64
66
extend ActiveSupport::Concern
77
class_methods do
8-
def has_one_base64_attached(name, dependent: :purge_later)
9-
has_one_attached name, dependent: dependent
8+
def has_one_base64_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
9+
has_one_attached name, dependent: dependent, service: service, strict_loading: strict_loading
1010

1111
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
12+
# frozen_string_literal: true
1213
def #{name}
13-
@active_storage_attached_#{name} ||= ActiveStorageSupport::Base64One.new("#{name}", self)
14+
@active_storage_attached ||= {}
15+
@active_storage_attached[:#{name}] ||= ActiveStorageSupport::Base64One.new("#{name}", self)
1416
end
1517
def #{name}=(attachable)
1618
attachment_changes["#{name}"] =
@@ -25,12 +27,14 @@ def #{name}=(attachable)
2527
CODE
2628
end
2729

28-
def has_many_base64_attached(name, dependent: :purge_later)
29-
has_many_attached name, dependent: dependent
30+
def has_many_base64_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
31+
has_many_attached name, dependent: dependent, service: service, strict_loading: strict_loading
3032

3133
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
34+
# frozen_string_literal: true
3235
def #{name}
33-
@active_storage_attached_#{name} ||= ActiveStorageSupport::Base64Many.new("#{name}", self)
36+
@active_storage_attached ||= {}
37+
@active_storage_attached[:#{name}] ||= ActiveStorageSupport::Base64Many.new("#{name}", self)
3438
end
3539
def #{name}=(attachables)
3640
if ActiveStorage.replace_on_assign_to_many

spec/dummy/app/models/application_record.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require_relative '../../../../lib/active_storage_base64.rb'
2-
require 'pry'
32

43
class ApplicationRecord < ActiveRecord::Base
54
include ActiveStorageSupport::SupportForBase64

spec/dummy/config/environments/test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@
3535

3636
# Raises error for missing translations
3737
# config.action_view.raise_on_missing_translations = true
38+
39+
# ActiveStorage
40+
config.active_storage.service = :test
3841
end

spec/dummy/config/storage.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
local:
1+
test:
22
service: Disk
3-
root: <%= Rails.root.join("storage") %>
3+
root: <%= Rails.root.join("tmp/storage") %>

spec/dummy/db/schema.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,24 @@
1818
t.string 'filename', null: false
1919
t.string 'content_type'
2020
t.text 'metadata'
21+
t.string 'service_name', null: false
2122
t.bigint 'byte_size', null: false
2223
t.string 'checksum', null: false
2324
t.datetime 'created_at', null: false
2425
t.index ['key'], name: 'index_active_storage_blobs_on_key', unique: true
2526
end
2627

28+
create_table 'active_storage_variant_records' do |t|
29+
t.belongs_to 'blob', null: false, index: false
30+
t.string 'variation_digest', null: false
31+
32+
t.index %w[blob_id variation_digest], name: 'index_active_storage_variant_records_uniqueness', unique: true
33+
end
34+
2735
create_table 'users', force: :cascade do |t|
2836
t.string 'username'
2937
end
3038

3139
add_foreign_key 'active_storage_attachments', 'active_storage_blobs', column: 'blob_id'
40+
add_foreign_key 'active_storage_variant_records', 'active_storage_blobs', column: 'blob_id'
3241
end

spec/spec_helper.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
ENV['RAILS_ENV'] ||= 'test'
22
require 'dummy/config/environment.rb'
3-
require 'pry'
4-
3+
require 'fileutils'
54
require 'simplecov'
5+
6+
FileUtils.rm_rf('./spec/dummy/tmp/storage')
7+
68
SimpleCov.start
79

810
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
911

10-
require 'tmpdir'
11-
ActiveStorage::Blob.service =
12-
ActiveStorage::Service::DiskService.new(root: Dir.mktmpdir('active_storage_tests'))
13-
1412
ActiveRecord::Base.establish_connection(
1513
adapter: 'sqlite3',
1614
database: ':memory:'

spec/user_base64_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
it 'raises a Module::DelegationError error' do
227227
expect do
228228
rails_url.rails_blob_url(user.avatar, disposition: 'attachment')
229-
end.to raise_error(Module::DelegationError)
229+
end.to raise_error(ActionController::UrlGenerationError)
230230
end
231231
end
232232
end

spec/user_file_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
it 'raises a Module::DelegationError error' do
6969
expect do
7070
rails_url.rails_blob_url(user.avatar, disposition: 'attachment')
71-
end.to raise_error(Module::DelegationError)
71+
end.to raise_error(ActionController::UrlGenerationError)
7272
end
7373
end
7474
end

0 commit comments

Comments
 (0)