diff --git a/Gemfile b/Gemfile index 401c9f5247..52b6fe13b0 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,8 @@ group :test do gem 'rspec-rails', '>= 2.14' gem 'rubocop', '~> 0.41.2' gem 'simplecov', '>= 0.9', require: false + gem 'shrine', '~> 2.0' + gem 'shrine-memory' gem 'timecop', '>= 0.5' platforms :ruby_19 do diff --git a/gemfiles/cancan.gemfile b/gemfiles/cancan.gemfile index a71f660687..54770cd30c 100644 --- a/gemfiles/cancan.gemfile +++ b/gemfiles/cancan.gemfile @@ -36,6 +36,8 @@ group :test do gem "rack-cache", require: "rack/cache" gem "rspec-rails", ">= 2.14" gem "rubocop", "~> 0.41.2" + gem "shrine", "~> 2.0" + gem "shrine-memory" gem "simplecov", ">= 0.9", require: false gem "timecop", ">= 0.5" gem "cancan", ">= 1.6" diff --git a/gemfiles/rails_4.0.gemfile b/gemfiles/rails_4.0.gemfile index e2f757bb1a..92882fff5e 100644 --- a/gemfiles/rails_4.0.gemfile +++ b/gemfiles/rails_4.0.gemfile @@ -13,6 +13,7 @@ gem "kaminari", "~> 0.14" group :active_record do gem "paper_trail", "~> 5.0" + gem "test_after_commit" platforms :ruby, :mswin, :mingw do gem "mysql2", ">= 0.3.14" @@ -45,6 +46,8 @@ group :test do gem "rack-cache", require: "rack/cache" gem "rspec-rails", ">= 2.14" gem "rubocop", "~> 0.41.2" + gem "shrine", "~> 2.0" + gem "shrine-memory" gem "simplecov", ">= 0.9", require: false gem "timecop", ">= 0.5" gem "cancancan", "~> 1.12" @@ -60,6 +63,7 @@ group :mongoid do gem "mongoid-paperclip", ">= 0.0.8", require: "mongoid_paperclip" gem "carrierwave-mongoid", ">= 0.6.3", require: "carrierwave/mongoid" gem "refile-mongoid", github: "DimaSamodurov/refile-mongoid", platforms: :ruby + gem "shrine-mongoid" end platforms :ruby do diff --git a/gemfiles/rails_4.1.gemfile b/gemfiles/rails_4.1.gemfile index 199a4e7084..b33f7d3982 100644 --- a/gemfiles/rails_4.1.gemfile +++ b/gemfiles/rails_4.1.gemfile @@ -10,6 +10,7 @@ gem "capybara", ">= 0.8", group: :test group :active_record do gem "paper_trail", ">= 5.0" + gem "test_after_commit" platforms :ruby, :mswin, :mingw do gem "mysql2", ">= 0.3.14" @@ -42,6 +43,8 @@ group :test do gem "rack-cache", require: "rack/cache" gem "rspec-rails", ">= 2.14" gem "rubocop", "~> 0.41.2" + gem "shrine", "~> 2.0" + gem "shrine-memory" gem "simplecov", ">= 0.9", require: false gem "timecop", ">= 0.5" gem "cancancan", "~> 1.12" @@ -57,6 +60,7 @@ group :mongoid do gem "mongoid-paperclip", ">= 0.0.8", require: "mongoid_paperclip" gem "carrierwave-mongoid", ">= 0.6.3", require: "carrierwave/mongoid" gem "refile-mongoid", github: "DimaSamodurov/refile-mongoid", platforms: :ruby + gem "shrine-mongoid" end platforms :ruby do diff --git a/gemfiles/rails_4.2.gemfile b/gemfiles/rails_4.2.gemfile index 2ed92fa5dd..40f130d88e 100644 --- a/gemfiles/rails_4.2.gemfile +++ b/gemfiles/rails_4.2.gemfile @@ -11,6 +11,7 @@ gem "capybara", ">= 0.8", group: :test group :active_record do gem "paper_trail", ">= 5.0" + gem "test_after_commit" platforms :ruby, :mswin, :mingw do gem "mysql2", ">= 0.3.14" @@ -43,6 +44,8 @@ group :test do gem "rack-cache", require: "rack/cache" gem "rspec-rails", ">= 2.14" gem "rubocop", "~> 0.41.2" + gem "shrine", "~> 2.0" + gem "shrine-memory" gem "simplecov", ">= 0.9", require: false gem "timecop", ">= 0.5" gem "cancancan", "~> 1.12" @@ -58,6 +61,7 @@ group :mongoid do gem "mongoid-paperclip", ">= 0.0.8", require: "mongoid_paperclip" gem "carrierwave-mongoid", ">= 0.6.3", require: "carrierwave/mongoid" gem "refile-mongoid", github: "DimaSamodurov/refile-mongoid", platforms: :ruby + gem "shrine-mongoid" end platforms :ruby do diff --git a/gemfiles/rails_5.0.gemfile b/gemfiles/rails_5.0.gemfile index cdd75fe315..963ef603cb 100644 --- a/gemfiles/rails_5.0.gemfile +++ b/gemfiles/rails_5.0.gemfile @@ -42,6 +42,8 @@ group :test do gem "rack-cache", require: "rack/cache" gem "rspec-rails", ">= 2.14" gem "rubocop", "~> 0.41.2" + gem "shrine", "~> 2.0" + gem "shrine-memory" gem "simplecov", ">= 0.9", require: false gem "timecop", ">= 0.5" gem "cancancan", "~> 2.0" @@ -57,6 +59,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" end gemspec path: "../" diff --git a/gemfiles/rails_5.1.gemfile b/gemfiles/rails_5.1.gemfile index 96e9850409..b263364ced 100644 --- a/gemfiles/rails_5.1.gemfile +++ b/gemfiles/rails_5.1.gemfile @@ -43,6 +43,8 @@ group :test do gem "rack-cache", require: "rack/cache" gem "rspec-rails", ">= 2.14" gem "rubocop", "~> 0.41.2" + gem "shrine", "~> 2.0" + gem "shrine-memory" gem "simplecov", ">= 0.9", require: false gem "timecop", ">= 0.5" gem "cancancan", "~> 2.0" @@ -58,6 +60,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" end gemspec path: "../" diff --git a/gemfiles/rails_5.2.gemfile b/gemfiles/rails_5.2.gemfile index ec0b800956..4dc67c87ae 100644 --- a/gemfiles/rails_5.2.gemfile +++ b/gemfiles/rails_5.2.gemfile @@ -37,6 +37,8 @@ group :test do gem "rack-cache", require: "rack/cache" gem "rspec-rails", ">= 2.14" gem "rubocop", "~> 0.41.2" + gem "shrine", "~> 2.0" + gem "shrine-memory" gem "simplecov", ">= 0.9", require: false gem "timecop", ">= 0.5" gem "cancancan", "~> 2.0" @@ -52,6 +54,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" end gemspec path: "../" diff --git a/lib/rails_admin/config/fields.rb b/lib/rails_admin/config/fields.rb index a708698909..81d826425e 100644 --- a/lib/rails_admin/config/fields.rb +++ b/lib/rails_admin/config/fields.rb @@ -84,4 +84,5 @@ def self.register_factory(&block) require 'rails_admin/config/fields/factories/carrierwave' require 'rails_admin/config/fields/factories/refile' require 'rails_admin/config/fields/factories/active_storage' +require 'rails_admin/config/fields/factories/shrine' require 'rails_admin/config/fields/factories/association' diff --git a/lib/rails_admin/config/fields/factories/shrine.rb b/lib/rails_admin/config/fields/factories/shrine.rb new file mode 100644 index 0000000000..da690c604e --- /dev/null +++ b/lib/rails_admin/config/fields/factories/shrine.rb @@ -0,0 +1,27 @@ +require 'rails_admin/config/fields' +require 'rails_admin/config/fields/types' +require 'rails_admin/config/fields/types/file_upload' + +RailsAdmin::Config::Fields.register_factory do |parent, properties, fields| + next false unless defined?(::Shrine) + + attachment_names = parent.abstract_model.model.ancestors.select { |m| m.is_a?(Shrine::Attachment) }.map { |a| a.instance_variable_get("@name") } + next false if attachment_names.blank? + + attachment_name = attachment_names.detect { |a| a == properties.name.to_s.chomp('_data').to_sym } + next false unless attachment_name + + field = RailsAdmin::Config::Fields::Types.load(:shrine).new(parent, attachment_name, properties) + fields << field + + data_field_name = "#{attachment_name}_data".to_sym + child_properties = parent.abstract_model.properties.detect { |p| p.name == data_field_name } + next true unless child_properties + + children_field = fields.detect { |f| f.name == data_field_name } || RailsAdmin::Config::Fields.default_factory.call(parent, child_properties, fields) + children_field.hide unless field == children_field + children_field.filterable(false) unless field == children_field + + field.children_fields([data_field_name]) + true +end diff --git a/lib/rails_admin/config/fields/types/all.rb b/lib/rails_admin/config/fields/types/all.rb index a186ba8025..6f23ea3663 100644 --- a/lib/rails_admin/config/fields/types/all.rb +++ b/lib/rails_admin/config/fields/types/all.rb @@ -26,6 +26,7 @@ require 'rails_admin/config/fields/types/hidden' require 'rails_admin/config/fields/types/text' require 'rails_admin/config/fields/types/serialized' +require 'rails_admin/config/fields/types/shrine' require 'rails_admin/config/fields/types/time' require 'rails_admin/config/fields/types/timestamp' require 'rails_admin/config/fields/types/color' diff --git a/lib/rails_admin/config/fields/types/shrine.rb b/lib/rails_admin/config/fields/types/shrine.rb new file mode 100644 index 0000000000..b8a5b8c465 --- /dev/null +++ b/lib/rails_admin/config/fields/types/shrine.rb @@ -0,0 +1,48 @@ +require 'rails_admin/config/fields/types/file_upload' + +module RailsAdmin + module Config + module Fields + module Types + class Shrine < RailsAdmin::Config::Fields::Types::FileUpload + RailsAdmin::Config::Fields::Types.register(self) + + register_instance_option :thumb_method do + unless defined? @thumb_method + @thumb_method = begin + next nil unless value.is_a?(Hash) + + if value.key?(:thumb) + :thumb + elsif value.key?(:thumbnail) + :thumbnail + else + value.keys.first + end + end + end + @thumb_method + end + + register_instance_option :delete_method do + "remove_#{name}" if bindings[:object].respond_to?("remove_#{name}") + end + + register_instance_option :cache_method do + "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 + else + value.url + end + end + end + end + end + end +end diff --git a/spec/controllers/rails_admin/main_controller_spec.rb b/spec/controllers/rails_admin/main_controller_spec.rb index a6e801abfc..81e2ee3d43 100644 --- a/spec/controllers/rails_admin/main_controller_spec.rb +++ b/spec/controllers/rails_admin/main_controller_spec.rb @@ -392,6 +392,9 @@ class TeamWithNumberedPlayers < Team field :active_storage_assets do delete_method :remove_active_storage_assets end if defined?(ActiveStorage) + field :shrine_asset do + delete_method :remove_shrine_asset + end if defined?(Shrine) end controller.params = HashWithIndifferentAccess.new( 'field_test' => { @@ -408,7 +411,8 @@ class TeamWithNumberedPlayers < Team 'delete_paperclip_asset' => 'test', 'should_not_be_here' => 'test', }.merge(defined?(Refile) ? {'refile_asset' => 'test', 'remove_refile_asset' => 'test'} : {}). - merge(defined?(ActiveStorage) ? {'active_storage_asset' => 'test', 'remove_active_storage_asset' => 'test', 'active_storage_assets' => 'test', 'remove_active_storage_assets' => 'test'} : {}), + merge(defined?(ActiveStorage) ? {'active_storage_asset' => 'test', 'remove_active_storage_asset' => 'test', 'active_storage_assets' => 'test', 'remove_active_storage_assets' => 'test'} : {}). + merge(defined?(Shrine) ? {'shrine_asset' => 'test', 'remove_shrine_asset' => 'test'} : {}), ) controller.send(:sanitize_params_for!, :create, RailsAdmin.config(FieldTest), controller.params['field_test']) @@ -425,7 +429,8 @@ class TeamWithNumberedPlayers < Team 'paperclip_asset' => 'test', 'delete_paperclip_asset' => 'test', }.merge(defined?(Refile) ? {'refile_asset' => 'test', 'remove_refile_asset' => 'test'} : {}). - merge(defined?(ActiveStorage) ? {'active_storage_asset' => 'test', 'remove_active_storage_asset' => 'test', 'active_storage_assets' => 'test', 'remove_active_storage_assets' => 'test'} : {})) + merge(defined?(ActiveStorage) ? {'active_storage_asset' => 'test', 'remove_active_storage_asset' => 'test', 'active_storage_assets' => 'test', 'remove_active_storage_assets' => 'test'} : {}). + merge(defined?(Shrine) ? {'shrine_asset' => 'test', 'remove_shrine_asset' => 'test'} : {})) end it 'allows for polymorphic associations parameters' do diff --git a/spec/dummy_app/Gemfile b/spec/dummy_app/Gemfile index 778fcdc579..9e79e3bd12 100644 --- a/spec/dummy_app/Gemfile +++ b/spec/dummy_app/Gemfile @@ -27,6 +27,7 @@ group :mongoid do gem 'mongoid-paperclip', '>= 0.0.8', require: 'mongoid_paperclip' gem 'carrierwave-mongoid', '>= 0.6.3', require: 'carrierwave/mongoid' # gem 'refile-mongoid', '>= 0.0.1', platforms: [:ruby_21, :ruby_22] + gem 'shrine-mongoid' end gem 'carrierwave', '>= 0.8' @@ -36,6 +37,8 @@ gem 'mini_magick', '>= 3.4' gem 'mlb', '>= 0.7' gem 'paperclip', '>= 3.4' gem 'rails_admin', path: '../../' +gem 'shrine', '~> 2.0' +gem 'shrine-memory' platforms :ruby_21, :ruby_22 do # gem 'refile', '>= 0.5', require: 'refile/rails' diff --git a/spec/dummy_app/app/active_record/field_test.rb b/spec/dummy_app/app/active_record/field_test.rb index db43f6b637..23815f1f98 100644 --- a/spec/dummy_app/app/active_record/field_test.rb +++ b/spec/dummy_app/app/active_record/field_test.rb @@ -47,6 +47,9 @@ def carrierwave_assets=(files) end end + include ShrineUploader.attachment(:shrine_asset) + include ShrineVersioningUploader.attachment(:shrine_versioning_asset) + if ::Rails.version >= '4.1' # enum support was added in Rails 4.1 enum string_enum_field: {S: 's', M: 'm', L: 'l'} enum integer_enum_field: [:small, :medium, :large] diff --git a/spec/dummy_app/app/active_record/shrine_uploader.rb b/spec/dummy_app/app/active_record/shrine_uploader.rb new file mode 100644 index 0000000000..b8ab3ba0d8 --- /dev/null +++ b/spec/dummy_app/app/active_record/shrine_uploader.rb @@ -0,0 +1,8 @@ +class ShrineUploader < Shrine + plugin :activerecord + + plugin :cached_attachment_data + plugin :determine_mime_type + plugin :pretty_location + plugin :remove_attachment +end diff --git a/spec/dummy_app/app/active_record/shrine_versioning_uploader.rb b/spec/dummy_app/app/active_record/shrine_versioning_uploader.rb new file mode 100644 index 0000000000..20633d0d15 --- /dev/null +++ b/spec/dummy_app/app/active_record/shrine_versioning_uploader.rb @@ -0,0 +1,18 @@ +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), + } + end +end diff --git a/spec/dummy_app/app/mongoid/field_test.rb b/spec/dummy_app/app/mongoid/field_test.rb index f47ab1b287..d6eaeb82b7 100644 --- a/spec/dummy_app/app/mongoid/field_test.rb +++ b/spec/dummy_app/app/mongoid/field_test.rb @@ -33,6 +33,11 @@ class FieldTest field :protected_field, type: String has_mongoid_attached_file :paperclip_asset, styles: {thumb: '100x100>'} + field :shrine_asset_data, type: String + include ShrineUploader::Attachment.new(:shrine_asset) + field :shrine_versioning_asset_data, type: String + include ShrineVersioningUploader::Attachment.new(:shrine_versioning_asset) + has_many :nested_field_tests, dependent: :destroy, inverse_of: :field_test, autosave: true accepts_nested_attributes_for :nested_field_tests, allow_destroy: true diff --git a/spec/dummy_app/app/mongoid/shrine_uploader.rb b/spec/dummy_app/app/mongoid/shrine_uploader.rb new file mode 100644 index 0000000000..c9cb9533ce --- /dev/null +++ b/spec/dummy_app/app/mongoid/shrine_uploader.rb @@ -0,0 +1,8 @@ +class ShrineUploader < Shrine + plugin :mongoid + + plugin :cached_attachment_data + plugin :determine_mime_type + plugin :pretty_location + plugin :remove_attachment +end diff --git a/spec/dummy_app/app/mongoid/shrine_versioning_uploader.rb b/spec/dummy_app/app/mongoid/shrine_versioning_uploader.rb new file mode 100644 index 0000000000..db4813a57d --- /dev/null +++ b/spec/dummy_app/app/mongoid/shrine_versioning_uploader.rb @@ -0,0 +1,18 @@ +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), + } + end +end diff --git a/spec/dummy_app/config/initializers/shrine.rb b/spec/dummy_app/config/initializers/shrine.rb new file mode 100644 index 0000000000..901b6738e4 --- /dev/null +++ b/spec/dummy_app/config/initializers/shrine.rb @@ -0,0 +1,7 @@ +require 'shrine' +require 'shrine/storage/memory' + +Shrine.storages = { + cache: Shrine::Storage::Memory.new, + store: Shrine::Storage::Memory.new, +} diff --git a/spec/dummy_app/db/migrate/20181029101829_add_shrine_data_to_field_tests.rb b/spec/dummy_app/db/migrate/20181029101829_add_shrine_data_to_field_tests.rb new file mode 100644 index 0000000000..9daa39402a --- /dev/null +++ b/spec/dummy_app/db/migrate/20181029101829_add_shrine_data_to_field_tests.rb @@ -0,0 +1,6 @@ +class AddShrineDataToFieldTests < MigrationBase + def change + add_column :field_tests, :shrine_asset_data, :text + add_column :field_tests, :shrine_versioning_asset_data, :text + end +end diff --git a/spec/rails_admin/config/fields/base_spec.rb b/spec/rails_admin/config/fields/base_spec.rb index caedb28864..7748d6538a 100644 --- a/spec/rails_admin/config/fields/base_spec.rb +++ b/spec/rails_admin/config/fields/base_spec.rb @@ -132,6 +132,21 @@ class CommentReversed < Tableless end end end + + if defined?(Shrine) + context 'of a Shrine installation' do + it 'is the parent field itself' do + expect(RailsAdmin.config(FieldTest).fields.detect { |f| f.name == :shrine_asset }.children_fields).to eq([:shrine_asset_data]) + expect(RailsAdmin.config(FieldTest).fields.detect { |f| f.name == :shrine_asset }.hidden?).to be_falsey + end + + it 'is not filterable' do + fields = RailsAdmin.config(FieldTest).fields.select { |f| [:shrine_asset_data].include?(f.name) } + expect(fields).to all(be_hidden) + expect(fields).not_to include(be_filterable) + end + end + end end describe '#form_default_value' do diff --git a/spec/rails_admin/config/fields/types/file_upload_spec.rb b/spec/rails_admin/config/fields/types/file_upload_spec.rb index d9c70d1fba..aba346fae1 100644 --- a/spec/rails_admin/config/fields/types/file_upload_spec.rb +++ b/spec/rails_admin/config/fields/types/file_upload_spec.rb @@ -16,6 +16,10 @@ field :active_storage_asset do delete_method :remove_active_storage_asset end if defined?(ActiveStorage) + field :shrine_asset do + delete_method :remove_shrine_asset + cache_method :cached_shrine_asset_data + end if defined?(Shrine) end end expect(RailsAdmin.config(FieldTest).field(:carrierwave_asset).allowed_methods.collect(&:to_s)).to eq %w(carrierwave_asset remove_carrierwave_asset carrierwave_asset_cache) @@ -23,6 +27,7 @@ expect(RailsAdmin.config(FieldTest).field(:paperclip_asset).allowed_methods.collect(&:to_s)).to eq %w(paperclip_asset delete_paperclip_asset) expect(RailsAdmin.config(FieldTest).field(:refile_asset).allowed_methods.collect(&:to_s)).to eq %w(refile_asset remove_refile_asset) if defined?(Refile) expect(RailsAdmin.config(FieldTest).field(:active_storage_asset).allowed_methods.collect(&:to_s)).to eq %w(active_storage_asset remove_active_storage_asset) if defined?(ActiveStorage) + expect(RailsAdmin.config(FieldTest).field(:shrine_asset).allowed_methods.collect(&:to_s)).to eq %w(shrine_asset remove_shrine_asset cached_shrine_asset_data) if defined?(Shrine) end end diff --git a/spec/rails_admin/config/fields/types/shrine_spec.rb b/spec/rails_admin/config/fields/types/shrine_spec.rb new file mode 100644 index 0000000000..6b4b80be8b --- /dev/null +++ b/spec/rails_admin/config/fields/types/shrine_spec.rb @@ -0,0 +1,129 @@ +require 'spec_helper' + +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 + + describe '#image?' do + it 'returns true' do + expect(field.image?).to be_truthy + end + end + + describe '#value' do + context 'when attachment exists' do + it 'returns attached object' do + expect(field.value).to be_a(Hash) + end + end + + context 'when attachment does not exist' do + let(:record) { FactoryBot.create :field_test } + + it 'returns nil' do + expect(field.value).to be_nil + end + end + end + + describe '#thumb_method' do + it 'returns :thumb' do + expect(field.thumb_method).to eq(:thumb) + end + end + + describe '#resource_url' do + context 'when calling without thumb' do + it 'returns original url' do + expect(field.resource_url).to match(/original-/) + end + end + + context 'when calling with thumb' do + it 'returns thumb url' do + expect(field.resource_url(field.thumb_method)).to match(/thumb-/) + end + end + end + end + + context 'when asset without versions' do + let(:record) { FactoryBot.create :field_test } + let(:field) do + RailsAdmin.config('FieldTest').fields.detect do |f| + f.name == :shrine_asset + end.with(object: record) + end + + describe '#image?' do + context 'when attachment is an image' do + let(:record) { FactoryBot.create :field_test, shrine_asset: FakeIO.new('dummy', filename: 'test.jpg', content_type: 'image/jpeg') } + + it 'returns true' do + expect(field.image?).to be_truthy + end + end + + context 'when attachment is not an image' do + let(:record) { FactoryBot.create :field_test, shrine_asset: FakeIO.new('dummy', filename: 'test.txt', content_type: 'text/plain') } + + it 'returns false' do + expect(field.image?).to be_falsy + end + end + end + + describe '#value' do + context 'when attachment exists' do + let(:record) { FactoryBot.create :field_test, shrine_asset: FakeIO.new('dummy', filename: 'test.txt', content_type: 'text/plain') } + + it 'returns attached object' do + expect(field.value).to be_a(ShrineUploader::UploadedFile) + end + end + + context 'when attachment does not exist' do + it 'returns nil' do + expect(field.value).to be_nil + end + end + end + + describe '#thumb_method' do + let(:record) { FactoryBot.create :field_test, shrine_asset: FakeIO.new('dummy', filename: 'test.jpg', content_type: 'image/jpeg') } + + it 'returns nil' do + expect(field.thumb_method).to eq(nil) + end + end + + describe '#resource_url' do + context 'when calling without thumb' do + let(:record) { FactoryBot.create :field_test, shrine_asset: FakeIO.new('dummy', filename: 'test.txt', content_type: 'text/plain') } + + it 'returns url' do + expect(field.resource_url).not_to be_nil + end + end + + context 'when calling with thumb' do + let(:record) { FactoryBot.create :field_test, shrine_asset: FakeIO.new('dummy', filename: 'test.jpg', content_type: 'image/jpeg') } + + it 'returns url' do + expect(field.resource_url(field.thumb_method)).not_to be_nil + end + end + + context 'when attachment does not exist' do + it 'returns nil' do + expect(field.resource_url).to be_nil + end + end + end + end +end if defined?(Shrine) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 46b46c6f48..ad732ae179 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,6 +23,7 @@ require 'policies' require 'database_cleaner' require "orm/#{CI_ORM}" +require 'support/fakeio' Dir[File.expand_path('../shared_examples/**/*.rb', __FILE__)].each { |f| require f } diff --git a/spec/support/fakeio.rb b/spec/support/fakeio.rb new file mode 100644 index 0000000000..1cfffd8696 --- /dev/null +++ b/spec/support/fakeio.rb @@ -0,0 +1,23 @@ +require 'forwardable' +require 'stringio' + +class FakeIO + attr_reader :original_filename, :content_type + + def initialize(content, filename: nil, content_type: nil) + @io = StringIO.new(content) + @original_filename = filename + @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