Skip to content

Commit cd69a59

Browse files
committed
feat(Picture): Add support for only and except setting
This allows to use only and except setting on Picture ingredients like on File ingredients. Signed-off-by: Thomas von Deyen <vondeyen@blish.cloud>
1 parent 192921b commit cd69a59

8 files changed

Lines changed: 47 additions & 10 deletions

File tree

app/controllers/alchemy/admin/pictures_controller.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class PicturesController < Alchemy::Admin::ResourcesController
1919
@picture = Picture.find(params[:id])
2020
end
2121

22-
add_alchemy_filter :by_file_format, type: :select, options: ->(query) do
23-
Alchemy::Picture.file_formats(query.result)
22+
add_alchemy_filter :by_file_format, type: :select, options: ->(_q) do
23+
Alchemy::Picture.file_formats
2424
end
2525
add_alchemy_filter :recent, type: :checkbox
2626
add_alchemy_filter :last_upload, type: :checkbox
@@ -183,12 +183,23 @@ def redirect_to_index
183183
end
184184

185185
def search_filter_params
186-
@_search_filter_params ||= params.except(*COMMON_SEARCH_FILTER_EXCLUDES + [:picture_ids]).permit(
187-
*common_search_filter_includes + [
188-
:size,
189-
:form_field_id
190-
]
191-
)
186+
@_search_filter_params ||= begin
187+
params[:q] ||= ActionController::Parameters.new
188+
params.except(*COMMON_SEARCH_FILTER_EXCLUDES + [:picture_ids]).permit(
189+
*common_search_filter_includes + [
190+
:size,
191+
:form_field_id
192+
]
193+
)
194+
end
195+
end
196+
197+
def permitted_ransack_search_fields
198+
super + [
199+
{by_file_format: []},
200+
:not_file_format,
201+
{not_file_format: []}
202+
]
192203
end
193204

194205
def picture_params

app/models/alchemy/ingredients/picture.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ class Picture < Alchemy::Ingredient
3030
allow_settings %i[
3131
crop
3232
css_classes
33+
except
3334
fixed_ratio
3435
linkable
36+
only
3537
size
3638
sizes
3739
srcset

app/models/alchemy/picture.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,15 @@ def allowed_filetypes
8080
scope :named, ->(name) { where("#{table_name}.name LIKE ?", "%#{name}%") }
8181
scope :recent, -> { where("#{table_name}.created_at > ?", Time.current - 24.hours).order(:created_at) }
8282
scope :without_tag, -> { left_outer_joins(:taggings).where(gutentag_taggings: {id: nil}) }
83+
8384
scope :by_file_format, ->(file_format) do
8485
Alchemy.storage_adapter.by_file_format_scope(file_format)
8586
end
8687

88+
scope :not_file_format, ->(file_format) do
89+
Alchemy.storage_adapter.not_file_format_scope(file_format)
90+
end
91+
8792
# Case insensitive Ransack searching and sorting for name attribute
8893
ransacker :name, type: :string do
8994
arel_table[:name].lower
@@ -125,7 +130,7 @@ def last_upload
125130
end
126131

127132
def ransackable_scopes(_auth_object = nil)
128-
[:by_file_format, :recent, :last_upload, :without_tag, :deletable]
133+
[:by_file_format, :not_file_format, :recent, :last_upload, :without_tag, :deletable]
129134
end
130135

131136
def file_formats(scope = all)

app/models/alchemy/storage_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class UnknownAdapterError < StandardError; end
77
delegate(
88
:attachment_url_class,
99
:by_file_format_scope,
10+
:not_file_format_scope,
1011
:by_file_type_scope,
1112
:not_file_type_scope,
1213
:file_extension,

app/models/alchemy/storage_adapter/active_storage.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def by_file_format_scope(file_format)
8484
Picture.with_attached_image_file.joins(:image_file_blob).where(active_storage_blobs: {content_type: file_format})
8585
end
8686

87+
# @param [String, Array<String>]
88+
# @return [Alchemy::Picture::ActiveRecord_Relation]
89+
def not_file_format_scope(file_format)
90+
Picture.with_attached_image_file.joins(:image_file_blob).where.not(active_storage_blobs: {content_type: file_format})
91+
end
92+
8793
# @param [String, Array<String>]
8894
# @return [Alchemy::Atachment::ActiveRecord_Relation]
8995
def by_file_type_scope(file_type)

app/models/alchemy/storage_adapter/dragonfly.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ def by_file_format_scope(file_format)
109109
Picture.where(image_file_format: file_format)
110110
end
111111

112+
# @param [String, Array<String>]
113+
# @return [Alchemy::Picture::ActiveRecord_Relation]
114+
def not_file_format_scope(file_format)
115+
Picture.where.not(image_file_format: file_format)
116+
end
117+
112118
# @param [String, Array<String>]
113119
# @return [Alchemy::Attachment::ActiveRecord_Relation]
114120
def by_file_type_scope(file_type)

app/views/alchemy/ingredients/shared/_picture_tools.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
<%= content_tag "sl-tooltip", content: picture_editor.picture ? Alchemy.t(:swap_image) : Alchemy.t(:insert_image) do %>
2323
<%= link_to_dialog render_icon("image-add"),
2424
alchemy.admin_pictures_path(
25-
form_field_id: picture_editor.form_field_id(:picture_id)
25+
form_field_id: picture_editor.form_field_id(:picture_id),
26+
q: {
27+
by_file_format: Array(picture_editor.settings[:only]),
28+
not_file_format: Array(picture_editor.settings[:except])
29+
}
2630
),
2731
{
2832
title: Alchemy.t(:choose_image),

spec/dummy/config/alchemy/elements.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
type: Picture
77
settings:
88
linkable: false
9+
except: gif
910

1011
- name: headline
1112
ingredients:
@@ -123,6 +124,7 @@
123124
settings:
124125
size: 1200x480
125126
crop: true
127+
only: [jpeg]
126128
css_classes:
127129
- "align-right"
128130
- "align-left"

0 commit comments

Comments
 (0)