effective_storage 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f43ada6fb79a20117caa0e82a3a2853852e4db3e38c80bf6278b539b05fcdb3a
4
- data.tar.gz: b727658f31266edf3646e8f6658b0b16c9b524439efd034caa83438e5c5a88a3
3
+ metadata.gz: e6b647a6cc8602078845e0112be3346f284705778fd081694deacf161a7ebf91
4
+ data.tar.gz: 1340ad269815fc10141936f997da57860a30ecdbb760658706fc84cb69281d64
5
5
  SHA512:
6
- metadata.gz: e0d0f65d241af25b65e7e326a908614e4d439126edd464a15449a4a2d93a7cf2e5539cd5929b98902f1e346ac3854ceb86e690af0a312b79a97aa19b960c90ab
7
- data.tar.gz: d6c1f5c005c079b9a3af3347c5f4fa590ddb4c788890683220b2202b2f574e82bedfde51d2b34964ea4e4359baf105f1af6dab1e4dc85a5a8a0ba26b231223c0
6
+ metadata.gz: 9c28337427ce9d4ef0782cf951a2b1bb6e5004b7ef3f4f7df842dfe54e2dc5dfe1578576a03d38bb55de85f0c504a5c1896b586a7f57f9c16768d75504f42f93
7
+ data.tar.gz: 307df9a832e70ae1709b0a24e6d665e1de1fedbad5d21d1b4347d8976ead29acd39682ffaac5eb83d0adc66f21c077139721df5d0e9707391b0996775e4c859e
data/README.md CHANGED
@@ -61,14 +61,23 @@ All authorization checks are handled via the effective_resources gem found in th
61
61
  The permissions you actually want to define are as follows (using CanCan):
62
62
 
63
63
  ```ruby
64
- can(:show, ActiveStorage::Attachment) { |attachment| attachment.permission_public? }
64
+ # Allow anyone to download a public file
65
+ can(:show, ActiveStorage::Blob) { |blob| blob.permission_public? }
65
66
 
66
67
  if user.persisted?
67
68
  end
68
69
 
69
70
  if user.admin?
71
+ # This allows the admin to download any file
72
+ can :show, ActiveStorage::Blob
73
+
74
+ # Allows them to see the index screen
70
75
  can :admin, :effective_storage
71
- can :index, ActiveStorage::Attachment
76
+ can :index, ActiveStorage::Blob
77
+
78
+ # Admin screen actions
79
+ can(:mark_inherited, ActiveStorage::Blob) { |blob| !blob.permission_inherited? }
80
+ can(:mark_public, ActiveStorage::Blob) { |blob| !blob.permission_public? }
72
81
  end
73
82
  ```
74
83
 
@@ -6,15 +6,7 @@ module Admin
6
6
  include Effective::CrudController
7
7
 
8
8
  page_title 'Storage'
9
-
10
- resource_scope -> { ActiveStorage::Attachment.all }
9
+ resource_scope -> { ActiveStorage::Blob.all }
11
10
  datatable -> { Admin::EffectiveStorageDatatable.new }
12
-
13
- private
14
-
15
- def permitted_params
16
- params.require(:active_storage_extension).permit!
17
- end
18
-
19
11
  end
20
12
  end
@@ -0,0 +1,12 @@
1
+ module Effective
2
+ class StorageController < ApplicationController
3
+ include Effective::CrudController
4
+
5
+ resource_scope -> { ActiveStorage::Blob.all }
6
+
7
+ before_action(if: -> { params.key?(:id) }) do
8
+ @blob = ActiveStorage::Blob.find_signed(params[:id])
9
+ end
10
+
11
+ end
12
+ end
@@ -1,62 +1,86 @@
1
1
  module Admin
2
2
  class EffectiveStorageDatatable < Effective::Datatable
3
- datatable do
4
- order :created_at
3
+ filters do
4
+ scope :all
5
+ scope :attached
6
+ scope :unattached
7
+ end
5
8
 
9
+ datatable do
6
10
  col :created_at, as: :date
7
- col :updated_at, visible: false
11
+
8
12
  col :id, visible: false
13
+ col :key, visible: false
9
14
 
10
- col :record_type, visible: false
11
- col :record_id, label: 'Record Id', visible: false
15
+ col :record_type, visible: false do |blob|
16
+ blob.attachments.map do |attachment|
17
+ content_tag(:div, attachment.record_type, class: 'col-resource_item')
18
+ end.join.html_safe
19
+ end
20
+
21
+ col :record_id, visible: false do |blob|
22
+ blob.attachments.map do |attachment|
23
+ content_tag(:div, attachment.record_id, class: 'col-resource_item')
24
+ end.join.html_safe
25
+ end
12
26
 
13
- col :related_type, visible: false do |attachment|
14
- attachment.record.try(:record_type)
27
+ col :related_type, visible: false do |blob|
28
+ blob.attachments.map do |attachment|
29
+ content_tag(:div, attachment.record.try(:record_type), class: 'col-resource_item')
30
+ end.join.html_safe
15
31
  end
16
32
 
17
- col :related_id, label: 'Related Id', visible: false do |attachment|
18
- attachment.record.try(:record_id)
33
+ col :related_id, visible: false, label: 'Related Id' do |blob|
34
+ blob.attachments.map do |attachment|
35
+ content_tag(:div, attachment.record.try(:record_id), class: 'col-resource_item')
36
+ end.join.html_safe
19
37
  end
20
38
 
21
- col :resource_type do |attachment|
22
- attachment.record.try(:record_type) || attachment.record_type
39
+ col :resource_type do |blob|
40
+ blob.attachments.map do |attachment|
41
+ content_tag(:div, class: 'col-resource_item') do
42
+ (attachment.record.try(:record_type) || attachment.record_type)
43
+ end
44
+ end.join.html_safe
23
45
  end
24
46
 
25
- col :resource do |attachment|
26
- record = attachment.record
27
- record = attachment.record.record if record.respond_to?(:record) # ActionText::RichText will
47
+ col :resource do |blob|
48
+ blob.attachments.map do |attachment|
49
+ content_tag(:div, class: 'col-resource_item') do
50
+ record = attachment.record
51
+ record = attachment.record.record if record.respond_to?(:record) # ActionText::RichText will
28
52
 
29
- url = Effective::Resource.new(record, namespace: :admin).action_path(:edit)
30
- link_to(record, url, target: '_blank') if url
53
+ url = Effective::Resource.new(record, namespace: :admin).action_path(:edit)
54
+ link_to(record, url, target: '_blank') if url
55
+ end
56
+ end.join.html_safe
31
57
  end
32
58
 
33
- col :filename, label: 'File' do |attachment|
59
+ col :filename, label: 'File' do |blob|
34
60
  content_tag(:div, class: 'col-resource_item') do
35
- link_to(attachment.blob.filename, url_for(attachment.blob), target: '_blank')
61
+ link_to(blob.filename, url_for(blob), target: '_blank')
36
62
  end
37
63
  end
38
64
 
39
- col :permission, search: Effective::ActiveStorageExtension::PERMISSIONS do |attachment|
40
- if attachment.permission_public?
41
- content_tag(:span, attachment.permission, class: 'badge badge-warning')
65
+ col :permission, search: Effective::ActiveStorageExtension::PERMISSIONS do |blob|
66
+ if blob.permission_public?
67
+ content_tag(:span, blob.permission, class: 'badge badge-warning')
42
68
  else
43
- content_tag(:span, attachment.permission, class: 'badge badge-info')
69
+ content_tag(:span, blob.permission, class: 'badge badge-info')
44
70
  end
45
71
  end
46
72
 
47
- col :content_type do |attachment|
48
- attachment.blob.content_type
49
- end
73
+ col :content_type
50
74
 
51
- col :byte_size do |attachment|
52
- number_to_human_size(attachment.blob.byte_size)
75
+ col :byte_size do |blob|
76
+ number_to_human_size(blob.byte_size)
53
77
  end
54
78
 
55
- actions_col partial: 'admin/storage/datatable_actions', partial_as: :attachment
79
+ actions_col partial: 'admin/storage/datatable_actions', partial_as: :blob
56
80
  end
57
81
 
58
82
  collection do
59
- ActiveStorage::Attachment.all.deep
83
+ ActiveStorage::Blob.all.deep.left_outer_joins(:attachments)
60
84
  end
61
85
 
62
86
  end
@@ -55,6 +55,9 @@ module ActiveStorageAuthorization
55
55
  def authorize_active_storage!
56
56
  return unless @blob.present?
57
57
 
58
+ # If the blob has been given permission
59
+ return true if authorized?(@blob)
60
+
58
61
  # If the blob is not attached to anything, permit the blob
59
62
  return true if @blob.attachments.blank? && authorize_content_download?(@blob)
60
63
 
@@ -1,12 +1,14 @@
1
1
  # This is included into ActiveStorage::Attachment automatically by engine.rb
2
- module ActiveStorageAttachmentExtension
2
+ module ActiveStorageBlobExtension
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- has_many :active_storage_extensions, class_name: 'Effective::ActiveStorageExtension', inverse_of: :attachment, dependent: :destroy
6
+ has_many :active_storage_extensions, class_name: 'Effective::ActiveStorageExtension', inverse_of: :blob, dependent: :destroy
7
7
  accepts_nested_attributes_for :active_storage_extensions, allow_destroy: true
8
8
 
9
- scope :deep, -> { includes(:active_storage_extensions, :blob, record: :record) }
9
+ scope :deep, -> { includes(:active_storage_extensions, attachments: [record: :record]) }
10
+
11
+ scope :attached, -> { joins(:attachments) }
10
12
  end
11
13
 
12
14
  module ClassMethods
@@ -15,7 +17,7 @@ module ActiveStorageAttachmentExtension
15
17
  # Instance methods
16
18
 
17
19
  def to_s
18
- 'attachment'
20
+ filename.presence || 'blob'
19
21
  end
20
22
 
21
23
  # Find or build
@@ -45,4 +47,8 @@ module ActiveStorageAttachmentExtension
45
47
  save!
46
48
  end
47
49
 
50
+ def purge!
51
+ purge
52
+ end
53
+
48
54
  end
@@ -1,6 +1,6 @@
1
1
  module Effective
2
2
  class ActiveStorageExtension < ActiveRecord::Base
3
- belongs_to :attachment, class_name: 'ActiveStorage::Attachment'
3
+ belongs_to :blob, class_name: 'ActiveStorage::Blob'
4
4
 
5
5
  PERMISSIONS = ['inherited', 'public']
6
6
 
@@ -1,8 +1,12 @@
1
1
  = dropdown(variation: :dropleft) do
2
- - if EffectiveResources.authorized?(self, :mark_inherited, attachment)
3
- = dropdown_link_to 'Mark as Inherited', effective_storage.mark_inherited_admin_storage_path(attachment),
2
+ - if EffectiveResources.authorized?(self, :mark_inherited, blob)
3
+ = dropdown_link_to 'Mark as Inherited', effective_storage.mark_inherited_admin_storage_path(blob),
4
4
  data: { method: :post, remote: true, confirm: "Mark as Inherited Permission?" }
5
5
 
6
- - if EffectiveResources.authorized?(self, :mark_public, attachment)
7
- = dropdown_link_to 'Mark as Public', effective_storage.mark_public_admin_storage_path(attachment),
6
+ - if EffectiveResources.authorized?(self, :mark_public, blob)
7
+ = dropdown_link_to 'Mark as Public', effective_storage.mark_public_admin_storage_path(blob),
8
8
  data: { method: :post, remote: true, confirm: "Mark as Public Permission?" }
9
+
10
+ - if EffectiveResources.authorized?(self, :purge, blob)
11
+ = dropdown_link_to 'Delete', effective_storage.purge_admin_storage_path(blob),
12
+ data: { method: :post, remote: true, confirm: "Really delete #{blob.to_s}?" }
data/config/routes.rb CHANGED
@@ -5,13 +5,21 @@ Rails.application.routes.draw do
5
5
  end
6
6
 
7
7
  EffectiveStorage::Engine.routes.draw do
8
+ scope module: 'effective' do
9
+ resources :storage, only: [] do
10
+ post :mark_public, on: :member
11
+ end
12
+ end
13
+
8
14
  namespace :admin do
9
15
  resources :storage, only: [] do
10
16
  post :mark_inherited, on: :member
11
17
  post :mark_public, on: :member
18
+ post :purge, on: :member
12
19
  end
13
20
 
14
21
  get '/storage', to: 'storage#index', as: :storage
22
+
15
23
  end
16
24
 
17
25
  end
@@ -2,7 +2,7 @@ class CreateEffectiveStorage < ActiveRecord::Migration[6.0]
2
2
  def change
3
3
 
4
4
  create_table :active_storage_extensions do |t|
5
- t.integer :attachment_id
5
+ t.integer :blob_id
6
6
  t.string :permission
7
7
 
8
8
  t.datetime :updated_at
@@ -10,7 +10,7 @@ module EffectiveStorage
10
10
  # Include active_storage_attachment_extension concern
11
11
  initializer 'effective_storage.active_storage_attachment_extension' do |app|
12
12
  app.config.to_prepare do
13
- ActiveStorage::Attachment.include(ActiveStorageAttachmentExtension)
13
+ ActiveStorage::Blob.include(ActiveStorageBlobExtension)
14
14
  end
15
15
  end
16
16
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveStorage
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-25 00:00:00.000000000 Z
11
+ date: 2022-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -133,10 +133,11 @@ files:
133
133
  - README.md
134
134
  - Rakefile
135
135
  - app/controllers/admin/storage_controller.rb
136
+ - app/controllers/effective/storage_controller.rb
136
137
  - app/datatables/admin/effective_storage_datatable.rb
137
138
  - app/helpers/effective_storage_helper.rb
138
- - app/models/concerns/active_storage_attachment_extension.rb
139
139
  - app/models/concerns/active_storage_authorization.rb
140
+ - app/models/concerns/active_storage_blob_extension.rb
140
141
  - app/models/effective/active_storage_extension.rb
141
142
  - app/views/admin/storage/_datatable_actions.html.haml
142
143
  - app/views/admin/storage/index.html.haml