Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shrine support #3081

Merged
merged 6 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/cancan.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions gemfiles/rails_4.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions gemfiles/rails_4.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions gemfiles/rails_4.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions gemfiles/rails_5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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: "../"
3 changes: 3 additions & 0 deletions gemfiles/rails_5.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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: "../"
3 changes: 3 additions & 0 deletions gemfiles/rails_5.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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: "../"
1 change: 1 addition & 0 deletions lib/rails_admin/config/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
27 changes: 27 additions & 0 deletions lib/rails_admin/config/fields/factories/shrine.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/rails_admin/config/fields/types/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
48 changes: 48 additions & 0 deletions lib/rails_admin/config/fields/types/shrine.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 7 additions & 2 deletions spec/controllers/rails_admin/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' => {
Expand All @@ -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'])
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy_app/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy_app/app/active_record/field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions spec/dummy_app/app/active_record/shrine_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ShrineUploader < Shrine
plugin :activerecord

plugin :cached_attachment_data
plugin :determine_mime_type
plugin :pretty_location
plugin :remove_attachment
end
18 changes: 18 additions & 0 deletions spec/dummy_app/app/active_record/shrine_versioning_uploader.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/dummy_app/app/mongoid/field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions spec/dummy_app/app/mongoid/shrine_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ShrineUploader < Shrine
plugin :mongoid

plugin :cached_attachment_data
plugin :determine_mime_type
plugin :pretty_location
plugin :remove_attachment
end
18 changes: 18 additions & 0 deletions spec/dummy_app/app/mongoid/shrine_versioning_uploader.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/dummy_app/config/initializers/shrine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'shrine'
require 'shrine/storage/memory'

Shrine.storages = {
cache: Shrine::Storage::Memory.new,
store: Shrine::Storage::Memory.new,
}
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions spec/rails_admin/config/fields/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading