Skip to content

Commit

Permalink
Merge pull request #3257 from LinkUpStudioUA/support_shrine_v3
Browse files Browse the repository at this point in the history
Upgrading to Shrine 3.x
  • Loading branch information
mshibuya committed Feb 28, 2021
2 parents 7434497 + e4d970e commit 6181283
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 61 deletions.
14 changes: 10 additions & 4 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ appraise "rails-5.0" do

group :test do
gem 'cancancan', '~> 2.0'
gem 'shrine', '~> 2.13.0'
gem 'shrine-memory'
end

group :active_record do
Expand All @@ -27,7 +29,7 @@ appraise "rails-5.0" do
gem 'mongoid-paperclip', '>= 0.0.8', require: 'mongoid_paperclip'
gem 'carrierwave-mongoid', '>= 0.6.3', require: 'carrierwave/mongoid'
gem 'cancancan-mongoid'
gem 'shrine-mongoid'
gem 'shrine-mongoid', '~> 0.2.4'
end
end

Expand All @@ -38,6 +40,7 @@ appraise "rails-5.1" do

group :test do
gem 'cancancan', '~> 2.0'
gem 'shrine', '~> 3.0'
end

group :active_record do
Expand All @@ -57,7 +60,7 @@ appraise "rails-5.1" do
gem 'mongoid-paperclip', '>= 0.0.8', require: 'mongoid_paperclip'
gem 'carrierwave-mongoid', '>= 0.6.3', require: 'carrierwave/mongoid'
gem 'cancancan-mongoid'
gem 'shrine-mongoid'
gem 'shrine-mongoid', '~> 1.0'
end
end

Expand All @@ -68,6 +71,7 @@ appraise "rails-5.2" do

group :test do
gem 'cancancan', '~> 2.0'
gem 'shrine', '~> 3.0'
end

group :active_record do
Expand All @@ -87,7 +91,7 @@ appraise "rails-5.2" do
gem 'mongoid-paperclip', '>= 0.0.8', require: 'mongoid_paperclip'
gem 'carrierwave-mongoid', '>= 0.6.3', require: 'carrierwave/mongoid'
gem 'cancancan-mongoid'
gem 'shrine-mongoid'
gem 'shrine-mongoid', '~> 1.0'
end
end

Expand All @@ -100,6 +104,7 @@ appraise "rails-6.0" do
group :test do
gem 'cancancan', '~> 3.0'
gem 'rspec-rails', '>= 4.0.0.beta2'
gem 'shrine', '~> 3.0'
end

group :active_record do
Expand All @@ -119,7 +124,7 @@ appraise "rails-6.0" do
gem 'mongoid-paperclip', '>= 0.0.8', require: 'mongoid_paperclip'
gem 'carrierwave-mongoid', '>= 0.6.3', require: 'carrierwave/mongoid'
gem 'cancancan-mongoid'
gem 'shrine-mongoid'
gem 'shrine-mongoid', '~> 1.0'
end
end

Expand All @@ -132,6 +137,7 @@ appraise "rails-6.1" do
group :test do
gem 'cancancan', '~> 3.0', github: 'ghiculescu/cancancan', branch: 'avoid-resolve_column_aliases-2'
gem 'rspec-rails', '>= 4.0.0.beta2'
gem 'shrine', '~> 3.0'
end

group :active_record do
Expand Down
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ group :test do
gem 'rubocop-performance'
gem 'simplecov', '>= 0.9', require: false
gem 'simplecov-lcov', require: false
gem 'shrine', '~> 2.0'
gem 'shrine-memory'
gem 'timecop', '>= 0.5'

platforms :ruby_19 do
Expand Down
6 changes: 3 additions & 3 deletions gemfiles/rails_5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ group :test do
gem "rubocop-performance"
gem "simplecov", ">= 0.9", require: false
gem "simplecov-lcov", require: false
gem "shrine", "~> 2.0"
gem "shrine-memory"
gem "timecop", ">= 0.5"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "shrine", "~> 2.13.0"
gem "shrine-memory"

platforms :ruby_19 do
gem "tins", "~> 1.6.0", require: false
Expand All @@ -63,7 +63,7 @@ group :mongoid do
gem "mongoid-paperclip", ">= 0.0.8", require: "mongoid_paperclip"
gem "carrierwave-mongoid", ">= 0.6.3", require: "carrierwave/mongoid"
gem "cancancan-mongoid"
gem "shrine-mongoid"
gem "shrine-mongoid", "~> 0.2.4"
end

gemspec path: "../"
5 changes: 2 additions & 3 deletions gemfiles/rails_5.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ group :test do
gem "rubocop-performance"
gem "simplecov", ">= 0.9", require: false
gem "simplecov-lcov", require: false
gem "shrine", "~> 2.0"
gem "shrine-memory"
gem "timecop", ">= 0.5"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "shrine", "~> 3.0"

platforms :ruby_19 do
gem "tins", "~> 1.6.0", require: false
Expand All @@ -64,7 +63,7 @@ group :mongoid do
gem "mongoid-paperclip", ">= 0.0.8", require: "mongoid_paperclip"
gem "carrierwave-mongoid", ">= 0.6.3", require: "carrierwave/mongoid"
gem "cancancan-mongoid"
gem "shrine-mongoid"
gem "shrine-mongoid", "~> 1.0"
end

gemspec path: "../"
5 changes: 2 additions & 3 deletions gemfiles/rails_5.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ group :test do
gem "rubocop-performance"
gem "simplecov", ">= 0.9", require: false
gem "simplecov-lcov", require: false
gem "shrine", "~> 2.0"
gem "shrine-memory"
gem "timecop", ">= 0.5"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "shrine", "~> 3.0"

platforms :ruby_19 do
gem "tins", "~> 1.6.0", require: false
Expand All @@ -64,7 +63,7 @@ group :mongoid do
gem "mongoid-paperclip", ">= 0.0.8", require: "mongoid_paperclip"
gem "carrierwave-mongoid", ">= 0.6.3", require: "carrierwave/mongoid"
gem "cancancan-mongoid"
gem "shrine-mongoid"
gem "shrine-mongoid", "~> 1.0"
end

gemspec path: "../"
5 changes: 2 additions & 3 deletions gemfiles/rails_6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ group :test do
gem "rubocop-performance"
gem "simplecov", ">= 0.9", require: false
gem "simplecov-lcov", require: false
gem "shrine", "~> 2.0"
gem "shrine-memory"
gem "timecop", ">= 0.5"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "shrine", "~> 3.0"

platforms :ruby_19 do
gem "tins", "~> 1.6.0", require: false
Expand All @@ -64,7 +63,7 @@ group :mongoid do
gem "mongoid-paperclip", ">= 0.0.8", require: "mongoid_paperclip"
gem "carrierwave-mongoid", ">= 0.6.3", require: "carrierwave/mongoid"
gem "cancancan-mongoid"
gem "shrine-mongoid"
gem "shrine-mongoid", "~> 1.0"
end

gemspec path: "../"
3 changes: 1 addition & 2 deletions gemfiles/rails_6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ group :test do
gem "rubocop-performance"
gem "simplecov", ">= 0.9", require: false
gem "simplecov-lcov", require: false
gem "shrine", "~> 2.0"
gem "shrine-memory"
gem "timecop", ">= 0.5"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "shrine", "~> 3.0"

platforms :ruby_19 do
gem "tins", "~> 1.6.0", require: false
Expand Down
6 changes: 5 additions & 1 deletion lib/rails_admin/config/fields/types/file_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class FileUpload < RailsAdmin::Config::Fields::Base
resource_url.to_s
end

register_instance_option :link_name do
value
end

register_instance_option :pretty_value do
if value.presence
v = bindings[:view]
Expand All @@ -40,7 +44,7 @@ class FileUpload < RailsAdmin::Config::Fields::Base
image_html = v.image_tag(thumb_url, class: 'img-thumbnail')
url != thumb_url ? v.link_to(image_html, url, target: '_blank', rel: 'noopener noreferrer') : image_html
else
v.link_to(value, url, target: '_blank', rel: 'noopener noreferrer')
v.link_to(link_name, url, target: '_blank', rel: 'noopener noreferrer')
end
end
end
Expand Down
24 changes: 13 additions & 11 deletions lib/rails_admin/config/fields/types/shrine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ class Shrine < RailsAdmin::Config::Fields::Types::FileUpload
register_instance_option :thumb_method do
unless defined? @thumb_method
@thumb_method = begin
next nil unless value.is_a?(Hash)
next nil unless bindings[:object].respond_to?("#{name}_derivatives")

if value.key?(:thumb)
derivatives = bindings[:object].public_send("#{name}_derivatives")

if derivatives.key?(:thumb)
:thumb
elsif value.key?(:thumbnail)
elsif derivatives.key?(:thumbnail)
:thumbnail
else
value.keys.first
derivatives.keys.first
end
end
end
Expand All @@ -29,21 +31,21 @@ class Shrine < RailsAdmin::Config::Fields::Types::FileUpload
end

register_instance_option :cache_method do
name
name if bindings[:object].try("cached_#{name}_data")
end

register_instance_option :cache_value do
bindings[:object].public_send("cached_#{name}_data") if bindings[:object].respond_to?("cached_#{name}_data")
bindings[:object].try("cached_#{name}_data")
end

register_instance_option :link_name do
value.original_filename
end

def resource_url(thumb = nil)
return nil unless value

if value.is_a?(Hash)
value[thumb || value.keys.first].url
else
value.url
end
thumb && bindings[:object].public_send(:"#{name}", thumb).try(:url) || value.url
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions spec/dummy_app/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ group :mongoid do
gem 'kaminari-mongoid'
gem 'mongoid-paperclip', '>= 0.0.8', require: 'mongoid_paperclip'
gem 'carrierwave-mongoid', '>= 0.6.3', require: 'carrierwave/mongoid'
gem 'shrine-mongoid'
gem 'shrine-mongoid', '~> 1.0'
end

gem 'carrierwave', '>= 2.0.0.rc', '< 3.0'
Expand All @@ -33,8 +33,7 @@ gem 'mini_magick', '>= 3.4'
gem 'mlb', '>= 0.7'
gem 'paperclip', '>= 3.4'
gem 'rails_admin', path: '../../'
gem 'shrine', '~> 2.0'
gem 'shrine-memory'
gem 'shrine', '~> 3.0'

# Gems used only for assets and not required
# in production environments by default.
Expand Down
16 changes: 8 additions & 8 deletions spec/dummy_app/app/active_record/shrine_versioning_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ class ShrineVersioningUploader < Shrine
plugin :activerecord

plugin :cached_attachment_data
plugin :delete_raw
plugin :determine_mime_type
plugin :pretty_location
plugin :processing
plugin :remove_attachment
plugin :versions

process(:store) do |io|
{
original: io,
thumb: FakeIO.another_version(io, :thumb),
}
if Gem.loaded_specs['shrine'].version >= Gem::Version.create('3')
plugin :derivatives

Attacher.derivatives_processor do |original|
{
thumb: FakeIO.new('', filename: File.basename(original.path), content_type: File.extname(original.path)),
}
end
end
end
16 changes: 8 additions & 8 deletions spec/dummy_app/app/mongoid/shrine_versioning_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ class ShrineVersioningUploader < Shrine
plugin :mongoid

plugin :cached_attachment_data
plugin :delete_raw
plugin :determine_mime_type
plugin :pretty_location
plugin :processing
plugin :remove_attachment
plugin :versions

process(:store) do |io|
{
original: io,
thumb: FakeIO.another_version(io, :thumb),
}
if Gem.loaded_specs['shrine'].version >= Gem::Version.create('3')
plugin :derivatives

Attacher.derivatives_processor do |original|
{
thumb: FakeIO.new('', filename: File.basename(original.path), content_type: File.extname(original.path)),
}
end
end
end
22 changes: 20 additions & 2 deletions spec/rails_admin/config/fields/types/shrine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,40 @@
RSpec.describe RailsAdmin::Config::Fields::Types::Shrine do
context 'when asset is an image with versions' do
let(:record) { FactoryBot.create :field_test, shrine_versioning_asset: FakeIO.new('dummy', filename: 'test.jpg', content_type: 'image/jpeg') }

let(:field) do
RailsAdmin.config('FieldTest').fields.detect do |f|
f.name == :shrine_versioning_asset
end.with(object: record)
end

before do
if Gem.loaded_specs['shrine'].version >= Gem::Version.create('3')
if record.shrine_versioning_asset
record.shrine_versioning_asset_derivatives!
record.save
end
else
skip
end
end

describe '#image?' do
it 'returns true' do
expect(field.image?).to be_truthy
end
end

describe '#link_name' do
it 'returns filename' do
expect(field.link_name).to eq('test.jpg')
end
end

describe '#value' do
context 'when attachment exists' do
it 'returns attached object' do
expect(field.value).to be_a(Hash)
expect(field.value).to be_a(ShrineVersioningUploader::UploadedFile)
end
end

Expand All @@ -40,7 +58,7 @@
describe '#resource_url' do
context 'when calling without thumb' do
it 'returns original url' do
expect(field.resource_url).to match(/original-/)
expect(field.resource_url).to_not match(/thumb-/)
end
end

Expand Down
8 changes: 0 additions & 8 deletions spec/support/fakeio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ def initialize(content, filename: nil, content_type: nil)
@content_type = content_type
end

def self.another_version(object, version_name)
FakeIO.new(
"#{version_name}-#{object.read}",
filename: "#{version_name}-#{object.original_filename}",
content_type: object.content_type,
)
end

extend Forwardable
delegate %i(read rewind eof? close size) => :@io
end

0 comments on commit 6181283

Please sign in to comment.