Module: ZenAdmin::Builtin

Defined in:
lib/zen_admin/builtin.rb

Class Method Summary collapse

Class Method Details

.register_active_storage_blobObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zen_admin/builtin.rb', line 3

def self.register_active_storage_blob
  return unless defined?(ActiveStorage::Attachment)
  return unless ActiveRecord::Base.connection.table_exists?("active_storage_attachments")

  # 锁定保护
  ActiveStorage::Attachment.send(:define_method, :zen_admin_editable?) { false }
  ActiveStorage::Blob.send(:define_method, :zen_admin_editable?) { false }

  # 1. 注册 Blob (完全隐藏,仅作为底层支撑)
  ZenAdmin.register(ActiveStorage::Blob) do |resource|
    resource.menu visible: false
    resource.show do
      field :id, label: "文件链接" do |blob|
        url = Rails.application.routes.url_helpers.rails_blob_url(blob, host: "localhost:3000") rescue ""
        render "zen_admin/builtin/file_link", url: url
      end
    end
  end

  # 2. Register System Attachments
  ZenAdmin.register(ActiveStorage::Attachment) do |resource|
    resource.label :'zen_admin.builtin.attachments', :'zen_admin.builtin.attachments'
    resource.menu label: :'zen_admin.builtin.attachments', icon: "fas fa-robot", position: 101, group: "系统管理"
    resource.soft_delete = false
    resource.includes :blob, :record
    resource.enable_batch_actions
    resource.batch_action :delete, label: :'zen_admin.actions.delete', type: :danger, confirm: I18n.t('zen_admin.ui.confirm_bulk_delete') do |records|
      records.destroy_all
    end
    
    resource.list do
      field :id, label: :'zen_admin.models.active_storage/attachment.fields.id' do |att|
        if att.blob.representable?
          url = main_app.rails_blob_path(att.blob, only_path: true)
          full_url = main_app.url_for(att.blob)
          tag.div(class: "thumbnail-box border rounded cursor-pointer shadow-xs", 
                  style: "width: 35px; height: 35px; overflow: hidden;",
                  data: { action: "click->layout#previewImage", preview_url: full_url }) do
            image_tag(url, style: "width: 100%; height: 100%; object-fit: cover;")
          end
        else
          # 精细化文件图标显示
          tag.i(class: "fas #{case att.blob.content_type
                            when /pdf/ then 'fa-file-pdf text-danger'
                            when /word|officedocument/ then 'fa-file-word text-primary'
                            when /excel|sheet/ then 'fa-file-excel text-success'
                            else 'fa-file-alt'
                            end} fa-lg")
        end
      end
      field :record_type, label: :'zen_admin.models.active_storage/attachment.fields.record_type'
      field :name, label: :'zen_admin.models.active_storage/attachment.fields.name'
      field :blob_filename, label: :'zen_admin.models.active_storage/attachment.fields.blob_filename' do |att|
        att.blob.filename.to_s
      end
      field :blob_id, label: :'zen_admin.models.active_storage/attachment.fields.blob_id' do |att|
        url = Rails.application.routes.url_helpers.rails_blob_url(att.blob, host: "localhost:3000") rescue "#"
        render "zen_admin/builtin/copy_button", url: url
      end
    end

    resource.show do
      field :record, label: :'zen_admin.models.active_storage/attachment.fields.record'
      field :blob, label: :'zen_admin.models.active_storage/attachment.fields.blob' do |att|
        url = Rails.application.routes.url_helpers.rails_blob_url(att.blob, host: "localhost:3000") rescue ""
        render "zen_admin/builtin/file_link", url: url
      end
    end
  end
  
  # 3. 显式加载我们的手动资源库模型
  require_dependency ZenAdmin::Engine.root.join("app/models/zen_admin/asset")
end