-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3081 from LinkUpStudioUA/shrine_field
Add Shrine support
- Loading branch information
Showing
26 changed files
with
351 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
spec/dummy_app/app/active_record/shrine_versioning_uploader.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
6 changes: 6 additions & 0 deletions
6
spec/dummy_app/db/migrate/20181029101829_add_shrine_data_to_field_tests.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddShrineDataToFieldTests < ActiveRecord::Migration[5.0] | ||
def change | ||
add_column :field_tests, :shrine_asset_data, :text | ||
add_column :field_tests, :shrine_versioning_asset_data, :text | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.