Skip to content

Commit

Permalink
Support Rails 6.1 (#53)
Browse files Browse the repository at this point in the history
* Support Rails 6.1

* Rubocop

* Fix tests

* Fix reek
  • Loading branch information
santib authored Dec 14, 2020
1 parent 08bef0d commit b1e0c68
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Gemfile.lock
gemfiles/*.lock
spec/dummy/log
spec/dummy/db/**.sqlite3
spec/dummy/tmp/storage/**/*
/coverage/
4 changes: 3 additions & 1 deletion .reek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ detectors:
exclude: []
BooleanParameter:
enabled: true
exclude: []
exclude:
- ActiveStorageSupport::SupportForBase64#has_many_base64_attached
- ActiveStorageSupport::SupportForBase64#has_one_base64_attached
ClassVariable:
enabled: false
exclude: []
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Metrics/ModuleLength:
Max: 200

Metrics/LineLength:
Max: 100
Max: 120
# To make it possible to copy or click on URIs in the code, we allow lines
# containing a URI to be longer than Max.
AllowURI: true
Expand Down
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ language: ruby
rvm:
- 2.5.0
- 2.6.0
- 2.7.0
- ruby-head

gemfile:
- gemfiles/rails_6_1.gemfile
- gemfiles/rails_head.gemfile

dist: bionic

jobs:
Expand Down
6 changes: 3 additions & 3 deletions active_storage_base64.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'active_storage_base64'
s.version = '1.1.0'
s.version = '1.2.0'
s.summary = 'Base64 support for ActiveStorage'
s.description = s.summary

Expand All @@ -12,10 +12,10 @@ Gem::Specification.new do |s|
s.homepage = 'https://github.com/rootstrap/active-storage-base64'
s.email = '[email protected]'

s.required_ruby_version = '>= 2.2.2'
s.required_ruby_version = '>= 2.5.0'

# Dependencies
s.add_dependency 'rails', '~> 6.0'
s.add_dependency 'rails', '~> 6.1'

# Development dependencies
s.add_development_dependency 'pry-rails', '~> 0.3.6'
Expand Down
13 changes: 0 additions & 13 deletions gemfiles/RailsHeadGemfile

This file was deleted.

5 changes: 5 additions & 0 deletions gemfiles/rails_6_1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'rails', '~> 6.1.0'

gemspec path: '..'
7 changes: 7 additions & 0 deletions gemfiles/rails_head.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'https://rubygems.org'

git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem 'rails', github: 'rails/rails', branch: 'master'

gemspec path: '..'
16 changes: 10 additions & 6 deletions lib/active_storage_support/support_for_base64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ module ActiveStorageSupport
module SupportForBase64
extend ActiveSupport::Concern
class_methods do
def has_one_base64_attached(name, dependent: :purge_later)
has_one_attached name, dependent: dependent
def has_one_base64_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
has_one_attached name, dependent: dependent, service: service, strict_loading: strict_loading

generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{name}
@active_storage_attached_#{name} ||= ActiveStorageSupport::Base64One.new("#{name}", self)
@active_storage_attached ||= {}
@active_storage_attached[:#{name}] ||= ActiveStorageSupport::Base64One.new("#{name}", self)
end
def #{name}=(attachable)
attachment_changes["#{name}"] =
Expand All @@ -25,12 +27,14 @@ def #{name}=(attachable)
CODE
end

def has_many_base64_attached(name, dependent: :purge_later)
has_many_attached name, dependent: dependent
def has_many_base64_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
has_many_attached name, dependent: dependent, service: service, strict_loading: strict_loading

generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
# frozen_string_literal: true
def #{name}
@active_storage_attached_#{name} ||= ActiveStorageSupport::Base64Many.new("#{name}", self)
@active_storage_attached ||= {}
@active_storage_attached[:#{name}] ||= ActiveStorageSupport::Base64Many.new("#{name}", self)
end
def #{name}=(attachables)
if ActiveStorage.replace_on_assign_to_many
Expand Down
1 change: 0 additions & 1 deletion spec/dummy/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require_relative '../../../../lib/active_storage_base64.rb'
require 'pry'

class ApplicationRecord < ActiveRecord::Base
include ActiveStorageSupport::SupportForBase64
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

# ActiveStorage
config.active_storage.service = :test
end
4 changes: 2 additions & 2 deletions spec/dummy/config/storage.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
local:
test:
service: Disk
root: <%= Rails.root.join("storage") %>
root: <%= Rails.root.join("tmp/storage") %>
9 changes: 9 additions & 0 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@
t.string 'filename', null: false
t.string 'content_type'
t.text 'metadata'
t.string 'service_name', null: false
t.bigint 'byte_size', null: false
t.string 'checksum', null: false
t.datetime 'created_at', null: false
t.index ['key'], name: 'index_active_storage_blobs_on_key', unique: true
end

create_table 'active_storage_variant_records' do |t|
t.belongs_to 'blob', null: false, index: false
t.string 'variation_digest', null: false

t.index %w[blob_id variation_digest], name: 'index_active_storage_variant_records_uniqueness', unique: true
end

create_table 'users', force: :cascade do |t|
t.string 'username'
end

add_foreign_key 'active_storage_attachments', 'active_storage_blobs', column: 'blob_id'
add_foreign_key 'active_storage_variant_records', 'active_storage_blobs', column: 'blob_id'
end
10 changes: 4 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
ENV['RAILS_ENV'] ||= 'test'
require 'dummy/config/environment.rb'
require 'pry'

require 'fileutils'
require 'simplecov'

FileUtils.rm_rf('./spec/dummy/tmp/storage')

SimpleCov.start

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

require 'tmpdir'
ActiveStorage::Blob.service =
ActiveStorage::Service::DiskService.new(root: Dir.mktmpdir('active_storage_tests'))

ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
Expand Down
2 changes: 1 addition & 1 deletion spec/user_base64_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
it 'raises a Module::DelegationError error' do
expect do
rails_url.rails_blob_url(user.avatar, disposition: 'attachment')
end.to raise_error(Module::DelegationError)
end.to raise_error(ActionController::UrlGenerationError)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/user_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
it 'raises a Module::DelegationError error' do
expect do
rails_url.rails_blob_url(user.avatar, disposition: 'attachment')
end.to raise_error(Module::DelegationError)
end.to raise_error(ActionController::UrlGenerationError)
end
end
end
Expand Down

0 comments on commit b1e0c68

Please sign in to comment.