Skip to content

Commit

Permalink
Make image_kind an attribute of ImageData
Browse files Browse the repository at this point in the history
This will allow users to choose from a list of image kinds, when models
permit more than one kind of image.

The ImageKind mixin still creates an image_kind method as a fallback for
models which don't have an image_kind attribute (such as
FeaturedImageData)
  • Loading branch information
richardTowers committed Nov 6, 2024
1 parent 5e04391 commit d48da72
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/edition_images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ def enforce_permissions!
end

def image_params
params.fetch(:image, {}).permit(image_data: [:file])
params.fetch(:image, {}).permit(image_data: %i[file image_kind])
end
end
4 changes: 4 additions & 0 deletions app/models/concerns/edition/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def allows_image_attachments?
true
end

def permitted_image_kinds
Whitehall.image_kinds.values_at("default")
end

private

def no_substantive_attributes?(attrs)
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/image_kind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ImageKind

included do
def image_kind
"default"
attributes.fetch(:image_kind, "default")
end

def image_kind_config
Expand Down
15 changes: 15 additions & 0 deletions app/views/admin/edition_images/_image_upload.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
accept: "image/png, image/jpeg, image/gif, image/svg+xml",
} %>
<% if @edition.permitted_image_kinds.size == 1 %>
<%= hidden_field_tag("image[image_data][image_kind]", @edition.permitted_image_kinds.first.name) %>
<% else %>
<%= render "govuk_publishing_components/components/radio", {
heading: "What kind of image is this?",
name: "image[image_data][image_kind]",
items: @edition.permitted_image_kinds.map do |image_kind|
{
value: image_kind.name,
text: image_kind.display_name,
}
end,
} %>
<% end %>
<%= render "govuk_publishing_components/components/details", {
title: "You must use an SVG for charts and diagrams",
} do %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241105092100_add_image_kind_to_image_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddImageKindToImageData < ActiveRecord::Migration[7.1]
def change
add_column :image_data, :image_kind, :string, default: "default", null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_10_15_123028) do
ActiveRecord::Schema[7.1].define(version: 2024_11_05_092100) do
create_table "assets", charset: "utf8mb3", force: :cascade do |t|
t.string "asset_manager_id", null: false
t.string "variant", null: false
Expand Down Expand Up @@ -666,6 +666,7 @@
t.string "carrierwave_image"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
t.string "image_kind", default: "default", null: false
end

create_table "images", id: :integer, charset: "utf8mb3", collation: "utf8mb3_unicode_ci", force: :cascade do |t|
Expand Down

0 comments on commit d48da72

Please sign in to comment.