Skip to content

Commit 328668c

Browse files
committed
refactor(AttachmentsController): Only use Attachment.by_file_type scope
We never used the not_file_type scope, but used the by_file_type scope with a inverted collection instead. Also use Attachment.file_types(from_extensions:) instead of inline Marcel::MimeType.for conversion.
1 parent 07f317c commit 328668c

4 files changed

Lines changed: 6 additions & 39 deletions

File tree

app/controllers/alchemy/admin/attachments_controller.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,11 @@ def search_filter_params
8585
params[:q] ||= ActionController::Parameters.new
8686

8787
if params[:only].present?
88-
params[:q][:by_file_type] ||= Array(params[:only]).map do |extension|
89-
Marcel::MimeType.for(extension:)
90-
end
88+
params[:q][:by_file_type] ||= Attachment.file_types(from_extensions: params[:only])
9189
end
9290

9391
if params[:except].present?
94-
params[:q][:by_file_type] ||= Attachment.file_types - params[:except].map do |extension|
95-
Marcel::MimeType.for(extension:)
96-
end
92+
params[:q][:by_file_type] ||= Attachment.file_types - Attachment.file_types(from_extensions: params[:except])
9793
end
9894

9995
params.except(*COMMON_SEARCH_FILTER_EXCLUDES + [:attachment]).permit(
@@ -108,9 +104,7 @@ def search_filter_params
108104

109105
def permitted_ransack_search_fields
110106
super + [
111-
{by_file_type: []},
112-
:not_file_type,
113-
{not_file_type: []}
107+
{by_file_type: []}
114108
]
115109
end
116110

app/models/alchemy/attachment.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ class Attachment < BaseRecord
3535
Alchemy.storage_adapter.by_file_type_scope(file_type)
3636
end
3737

38-
scope :not_file_type, ->(*file_type) do
39-
Alchemy.storage_adapter.not_file_type_scope(file_type)
40-
end
41-
4238
scope :recent, -> { where("#{table_name}.created_at > ?", Time.current - 24.hours).order(:created_at) }
4339
scope :without_tag, -> { left_outer_joins(:taggings).where(gutentag_taggings: {id: nil}) }
4440

@@ -85,7 +81,7 @@ def allowed_filetypes
8581
end
8682

8783
def ransackable_scopes(_auth_object = nil)
88-
%i[by_file_type not_file_type recent last_upload without_tag deletable]
84+
%i[by_file_type recent last_upload without_tag deletable]
8985
end
9086
end
9187

spec/controllers/alchemy/admin/attachments_controller_spec.rb

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,29 +107,6 @@ module Alchemy
107107
expect(assigns(:attachments).to_a).to_not eq([png])
108108
end
109109
end
110-
end
111-
112-
describe "not_file_type filter" do
113-
let!(:png) { create(:alchemy_attachment) }
114-
115-
let!(:jpg) do
116-
create :alchemy_attachment,
117-
file: fixture_file_upload("image3.jpeg")
118-
end
119-
120-
it "loads all but attachments with matching content type" do
121-
get :index, params: {q: {not_file_type: "image/jpeg"}}
122-
expect(assigns(:attachments).to_a).to_not eq([jpg])
123-
expect(assigns(:attachments).to_a).to eq([png])
124-
end
125-
126-
context "with multiple content types" do
127-
it "loads all but attachments with matching content type" do
128-
get :index, params: {q: {not_file_type: ["image/jpeg"]}}
129-
expect(assigns(:attachments).to_a).to_not eq([jpg])
130-
expect(assigns(:attachments).to_a).to eq([png])
131-
end
132-
end
133110

134111
context "with except param" do
135112
it "populates by_file_type query" do

spec/models/alchemy/attachment_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ module Alchemy
4747
end
4848

4949
describe ".ransackable_scopes" do
50-
it "delegates to storage adapter" do
51-
expect(described_class.ransackable_scopes).to eq %i[by_file_type not_file_type recent last_upload without_tag deletable]
50+
it do
51+
expect(described_class.ransackable_scopes).to eq %i[by_file_type recent last_upload without_tag deletable]
5252
end
5353
end
5454

0 commit comments

Comments
 (0)