Skip to content

Commit

Permalink
Merge 45c3b2c into be399b2
Browse files Browse the repository at this point in the history
  • Loading branch information
akhsunna committed Feb 24, 2020
2 parents be399b2 + 45c3b2c commit 9c84a13
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 50 deletions.
8 changes: 4 additions & 4 deletions Appraisals
Expand Up @@ -27,7 +27,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', '~> 1.0'
end
end

Expand Down Expand Up @@ -57,7 +57,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 Down Expand Up @@ -87,7 +87,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 Down Expand Up @@ -119,6 +119,6 @@ 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', github: 'mshibuya/cancancan-mongoid', branch: 'mongoid-7'
gem 'shrine-mongoid'
gem 'shrine-mongoid', '~> 1.0'
end
end
3 changes: 1 addition & 2 deletions Gemfile
Expand Up @@ -37,8 +37,7 @@ group :test do
gem 'rubocop', '~> 0.68.1'
gem 'rubocop-performance'
gem 'simplecov', '>= 0.9', require: false
gem 'shrine', '~> 2.0'
gem 'shrine-memory'
gem 'shrine', '~> 3.0'
gem 'timecop', '>= 0.5'

platforms :ruby_19 do
Expand Down
5 changes: 2 additions & 3 deletions gemfiles/rails_5.0.gemfile
Expand Up @@ -46,8 +46,7 @@ group :test do
gem "rubocop", "~> 0.68.1"
gem "rubocop-performance"
gem "simplecov", ">= 0.9", require: false
gem "shrine", "~> 2.0"
gem "shrine-memory"
gem "shrine", "~> 3.0"
gem "timecop", ">= 0.5"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Expand All @@ -62,7 +61,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.1.gemfile
Expand Up @@ -47,8 +47,7 @@ group :test do
gem "rubocop", "~> 0.68.1"
gem "rubocop-performance"
gem "simplecov", ">= 0.9", require: false
gem "shrine", "~> 2.0"
gem "shrine-memory"
gem "shrine", "~> 3.0"
gem "timecop", ">= 0.5"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Expand All @@ -63,7 +62,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
Expand Up @@ -47,8 +47,7 @@ group :test do
gem "rubocop", "~> 0.68.1"
gem "rubocop-performance"
gem "simplecov", ">= 0.9", require: false
gem "shrine", "~> 2.0"
gem "shrine-memory"
gem "shrine", "~> 3.0"
gem "timecop", ">= 0.5"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Expand All @@ -63,7 +62,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
Expand Up @@ -47,8 +47,7 @@ group :test do
gem "rubocop", "~> 0.68.1"
gem "rubocop-performance"
gem "simplecov", ">= 0.9", require: false
gem "shrine", "~> 2.0"
gem "shrine-memory"
gem "shrine", "~> 3.0"
gem "timecop", ">= 0.5"
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Expand All @@ -63,7 +62,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", github: "mshibuya/cancancan-mongoid", branch: "mongoid-7"
gem "shrine-mongoid"
gem "shrine-mongoid", '~> 1.0'
end

gemspec path: "../"
6 changes: 5 additions & 1 deletion lib/rails_admin/config/fields/types/file_upload.rb
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
18 changes: 12 additions & 6 deletions lib/rails_admin/config/fields/types/shrine.rb
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 @@ -32,15 +34,19 @@ class Shrine < RailsAdmin::Config::Fields::Types::FileUpload
name
end

register_instance_option :link_name do
value.original_filename
end

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

def resource_url(thumb = nil)
return nil unless value

if value.is_a?(Hash)
value[thumb || value.keys.first].url
if thumb
bindings[:object].public_send(:"#{name}", thumb).url
else
value.url
end
Expand Down
5 changes: 2 additions & 3 deletions spec/dummy_app/Gemfile
Expand Up @@ -27,7 +27,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 @@ -37,8 +37,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
Expand Up @@ -2,17 +2,14 @@ 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
plugin :derivatives

process(:store) do |io|
Attacher.derivatives_processor do |original|
{
original: io,
thumb: FakeIO.another_version(io, :thumb),
thumb: FakeIO.new('', filename: File.basename(original.path), content_type: File.extname(original.path)),
}
end
end
9 changes: 3 additions & 6 deletions spec/dummy_app/app/mongoid/shrine_versioning_uploader.rb
Expand Up @@ -2,17 +2,14 @@ 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
plugin :derivatives

process(:store) do |io|
Attacher.derivatives_processor do |original|
{
original: io,
thumb: FakeIO.another_version(io, :thumb),
thumb: FakeIO.new('', filename: File.basename(original.path), content_type: File.extname(original.path)),
}
end
end
18 changes: 16 additions & 2 deletions spec/rails_admin/config/fields/types/shrine_spec.rb
Expand Up @@ -3,22 +3,36 @@
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 record.shrine_versioning_asset
record.shrine_versioning_asset_derivatives!
record.save
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 +54,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
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 9c84a13

Please sign in to comment.