-
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 #3037 from sferik/active_storage
ActiveStorage integration
- Loading branch information
Showing
16 changed files
with
213 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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| | ||
if defined?(::ActiveStorage) && properties.is_a?(RailsAdmin::Adapters::ActiveRecord::Association) && (match = /\A(.+)_attachment\Z/.match properties.name) && properties.klass.to_s == 'ActiveStorage::Attachment' | ||
name = match[1] | ||
field = RailsAdmin::Config::Fields::Types.load(:active_storage).new(parent, name, properties) | ||
fields << field | ||
associations = ["#{name}_attachment".to_sym, "#{name}_blob".to_sym] | ||
children_fields = associations.map do |child_name| | ||
next unless child_association = parent.abstract_model.associations.detect { |p| p.name.to_sym == child_name } | ||
child_field = fields.detect { |f| f.name == child_name } || RailsAdmin::Config::Fields.default_factory.call(parent, child_association, fields) | ||
child_field.hide unless field == child_field | ||
child_field.filterable(false) unless field == child_field | ||
child_field.name | ||
end.flatten | ||
field.children_fields(children_fields) | ||
true | ||
else | ||
false | ||
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,44 @@ | ||
require 'rails_admin/config/fields/types/file_upload' | ||
|
||
module RailsAdmin | ||
module Config | ||
module Fields | ||
module Types | ||
class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload | ||
RailsAdmin::Config::Fields::Types.register(self) | ||
|
||
register_instance_option :thumb_method do | ||
{resize: '100x100>'} | ||
end | ||
|
||
register_instance_option :delete_method do | ||
"remove_#{name}" if bindings[:object].respond_to?("remove_#{name}") | ||
end | ||
|
||
register_instance_option :image? do | ||
if value | ||
value.filename.to_s.split('.').last =~ /jpg|jpeg|png|gif|svg/i | ||
end | ||
end | ||
|
||
def resource_url(thumb = false) | ||
return nil unless value | ||
if thumb && value.variable? | ||
variant = value.variant(thumb) | ||
Rails.application.routes.url_helpers.rails_blob_representation_path( | ||
variant.blob.signed_id, variant.variation.key, variant.blob.filename, only_path: true | ||
) | ||
else | ||
Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true) | ||
end | ||
end | ||
|
||
def value | ||
attachment = super | ||
attachment if attachment && attachment.attached? | ||
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
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,3 @@ | ||
local: | ||
service: Disk | ||
root: <%= Rails.root.join('public', 'system') %> |
30 changes: 30 additions & 0 deletions
30
spec/dummy_app/db/migrate/20180701084251_create_active_storage_tables.active_storage.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,30 @@ | ||
# This migration comes from active_storage (originally 20170806125915) | ||
class CreateActiveStorageTables < MigrationBase | ||
def change | ||
create_table :active_storage_blobs do |t| | ||
t.string :key, null: false | ||
t.string :filename, null: false | ||
t.string :content_type | ||
t.text :metadata | ||
if t.respond_to? :bigint | ||
t.bigint :byte_size, null: false | ||
else | ||
t.integer :byte_size, null: false | ||
end | ||
t.string :checksum, null: false | ||
t.datetime :created_at, null: false | ||
|
||
t.index [:key], unique: true | ||
end | ||
|
||
create_table :active_storage_attachments do |t| | ||
t.string :name, null: false | ||
t.references :record, null: false, polymorphic: true, index: false | ||
t.references :blob, null: false | ||
|
||
t.datetime :created_at, null: false | ||
|
||
t.index [:record_type, :record_id, :name, :blob_id], name: "index_active_storage_attachments_uniqueness", unique: true | ||
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
74 changes: 74 additions & 0 deletions
74
spec/rails_admin/config/fields/types/active_storage_spec.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,74 @@ | ||
require 'spec_helper' | ||
|
||
describe RailsAdmin::Config::Fields::Types::ActiveStorage do | ||
it_behaves_like 'a generic field type', :string_field, :active_storage | ||
|
||
let(:record) { FactoryGirl.create :field_test } | ||
let(:field) do | ||
RailsAdmin.config('FieldTest').fields.detect do |f| | ||
f.name == :active_storage_asset | ||
end.with(object: record) | ||
end | ||
|
||
describe '#image?' do | ||
context 'when attachment is an image' do | ||
let(:record) { FactoryGirl.create :field_test, active_storage_asset: {io: StringIO.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) { FactoryGirl.create :field_test, active_storage_asset: {io: StringIO.new('dummy'), filename: "test.txt", content_type: "text/plain"} } | ||
|
||
it 'returns false' do | ||
expect(field.image?).to be_falsy | ||
end | ||
end | ||
end | ||
|
||
describe '#resource_url' do | ||
context 'when calling with thumb = false' do | ||
let(:record) { FactoryGirl.create :field_test, active_storage_asset: {io: StringIO.new('dummy'), filename: "test.jpg", content_type: "image/jpeg"} } | ||
|
||
it 'returns original url' do | ||
expect(field.resource_url).not_to match(/representations/) | ||
end | ||
end | ||
|
||
context 'when attachment is an image' do | ||
let(:record) { FactoryGirl.create :field_test, active_storage_asset: {io: StringIO.new('dummy'), filename: "test.jpg", content_type: "image/jpeg"} } | ||
|
||
it 'returns variant\'s url' do | ||
expect(field.resource_url(true)).to match(/representations/) | ||
end | ||
end | ||
|
||
context 'when attachment is not an image' do | ||
let(:record) { FactoryGirl.create :field_test, active_storage_asset: {io: StringIO.new('dummy'), filename: "test.txt", content_type: "text/plain"} } | ||
|
||
it 'returns original url' do | ||
expect(field.resource_url(true)).not_to match(/representations/) | ||
end | ||
end | ||
end | ||
|
||
describe '#value' do | ||
context 'when attachment exists' do | ||
let(:record) { FactoryGirl.create :field_test, active_storage_asset: {io: StringIO.new('dummy'), filename: "test.jpg", content_type: "image/jpeg"} } | ||
|
||
it 'returns attached object' do | ||
expect(field.value).to be_a(ActiveStorage::Attached::One) | ||
end | ||
end | ||
|
||
context 'when attachment does not exist' do | ||
let(:record) { FactoryGirl.create :field_test } | ||
|
||
it 'returns nil' do | ||
expect(field.value).to be_nil | ||
end | ||
end | ||
end | ||
end if defined?(ActiveStorage) |
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