katalyst-koi 4.5.5 → 4.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 07c62ea19ccf07a39adeccee7aaefeb2b1df95a6305f80d1fec30391127e85c8
4
- data.tar.gz: 9bd56feee1ba02ebec96c682fea435149bc65747bc171d9ef21df73a748f16b3
3
+ metadata.gz: 13cd00ba07e90a830f3a858db1fdd1f22181bf68448b0716441b66c2ce17eb84
4
+ data.tar.gz: 96bbf769605e6b8b4091f500e475cdb033039e037b784c7469a9ce52ade9866c
5
5
  SHA512:
6
- metadata.gz: c7ab93457d42276b7ff576a01222eeb8504ea710166779d71d35cc823e09e57d0fc36ef4d83cf3345c03e7590e62741823e7469a202b6f16e1568325750fae2f
7
- data.tar.gz: 3f6de676c47f5801c822d985e8f469d2831fc292704f303b1923ebd304b1b69aa81360450daa2981ea77065751233b5394ba3d9bbeb7ca19b62bcc6e454cbc7f
6
+ metadata.gz: b8c065374fc75587b31288078435eb2e9ad801a3e1dbf19984868d4377f0e6cf31b8e992c9b2f7a0b6761669506087c08d742faebe31a79356d6fe869c67c921
7
+ data.tar.gz: b159be789bf941acab6882e460ccf85858b4a5016f10195d4351b2050eb33260ae1835e404a43cee3c763a68af4a1321d017e879a7fe56225d608a9e0a58b945
@@ -10,12 +10,38 @@ module Koi
10
10
  end
11
11
 
12
12
  def attribute_value
13
- if raw_value.try(:representable?)
14
- image_tag(@variant.nil? ? raw_value : raw_value.variant(@variant))
13
+ representation
14
+ end
15
+
16
+ def representation
17
+ if raw_value.try(:variable?) && named_variant.present?
18
+ image_tag(raw_value.variant(@variant))
15
19
  elsif raw_value.try(:attached?)
16
- link_to raw_value.blob.filename, rails_blob_path(raw_value, disposition: :attachment)
20
+ filename.to_s
21
+ else
22
+ ""
17
23
  end
18
24
  end
25
+
26
+ def filename
27
+ raw_value.blob.filename
28
+ end
29
+
30
+ # Utility for accessing the path Rails provides for retrieving the
31
+ # attachment for use in cells. Example:
32
+ # <% row.attachment :file do |cell| %>
33
+ # <%= link_to "Download", cell.internal_path %>
34
+ # <% end %>
35
+ def internal_path
36
+ rails_blob_path(raw_value, disposition: :attachment)
37
+ end
38
+
39
+ private
40
+
41
+ # Find the reflective variant by name (i.e. :thumb by default)
42
+ def named_variant
43
+ @model.attachment_reflections[@attribute.to_s].named_variants[@variant.to_sym]
44
+ end
19
45
  end
20
46
  end
21
47
  end
@@ -190,14 +190,38 @@ module Koi
190
190
  end
191
191
 
192
192
  def rendered_value
193
- if value.try(:representable?)
194
- image_tag(@variant.nil? ? value : value.variant(@variant))
193
+ representation
194
+ end
195
+
196
+ def representation
197
+ if value.try(:variable?) && named_variant.present?
198
+ image_tag(value.variant(@variant))
195
199
  elsif value.try(:attached?)
196
- link_to value.blob.filename, rails_blob_path(value, disposition: :attachment)
200
+ filename.to_s
197
201
  else
198
202
  ""
199
203
  end
200
204
  end
205
+
206
+ def filename
207
+ value.blob.filename
208
+ end
209
+
210
+ # Utility for accessing the path Rails provides for retrieving the
211
+ # attachment for use in cells. Example:
212
+ # <% row.attachment :file do |cell| %>
213
+ # <%= link_to "Download", cell.internal_path %>
214
+ # <% end %>
215
+ def internal_path
216
+ rails_blob_path(value, disposition: :attachment)
217
+ end
218
+
219
+ private
220
+
221
+ # Find the reflective variant by name (i.e. :thumb by default)
222
+ def named_variant
223
+ object.attachment_reflections[@attribute.to_s].named_variants[@variant.to_sym]
224
+ end
201
225
  end
202
226
  end
203
227
  end
@@ -12,6 +12,10 @@ module Koi
12
12
  def rendered_value
13
13
  value.to_s
14
14
  end
15
+
16
+ def to_s
17
+ rendered_value
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koi
4
+ module Controller
5
+ module HasAttachments
6
+ extend ActiveSupport::Concern
7
+
8
+ # Store attachments in the given object so that they can be preserved for
9
+ # the next form submission.
10
+ #
11
+ # Example:
12
+ # def create
13
+ # @document = Document.new(document_params)
14
+ # if @document.save
15
+ # redirect_to [:admin, @document], status: :see_other
16
+ # else
17
+ # store_attachments(@document)
18
+ # render :new, status: :unprocessable_entity
19
+ # end
20
+ # end
21
+ #
22
+ # @param resource [ActiveRecord::Base] The object being edited
23
+ def save_attachments!(resource)
24
+ resource.attachment_changes.each_value do |change|
25
+ case change
26
+ when ActiveStorage::Attached::Changes::CreateOne
27
+ change.upload
28
+ change.blob.save!
29
+ when ActiveStorage::Attached::Changes::CreateMany
30
+ change.upload
31
+ change.blobs.each(&:save!)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -13,6 +13,7 @@ module Koi
13
13
 
14
14
  included do
15
15
  include HasAdminUsers
16
+ include HasAttachments
16
17
  include Katalyst::Tables::Backend
17
18
  include Pagy::Backend
18
19
 
@@ -55,7 +55,7 @@ module GOVUKDesignSystemFormBuilder
55
55
  # Generates a +div+ element with an +input+ with +type=file+ with a label, optional hint.
56
56
  #
57
57
  # @example A upload field with label as a proc
58
- # = f.govuk_file_field :data, label: -> { tag.h3('Upload your document') }
58
+ # = f.govuk_document_field :data, label: -> { tag.h3('Upload your document') }
59
59
  #
60
60
  def govuk_document_field(attribute_name, label: {}, caption: {}, hint: {}, form_group: {}, **kwargs, &block)
61
61
  Elements::Document.new(self, object_name, attribute_name, label:, caption:, hint:, form_group:, **kwargs,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-koi
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.5.5
4
+ version: 4.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-20 00:00:00.000000000 Z
11
+ date: 2024-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -331,6 +331,7 @@ files:
331
331
  - app/controllers/admin/tokens_controller.rb
332
332
  - app/controllers/admin/url_rewrites_controller.rb
333
333
  - app/controllers/concerns/koi/controller/has_admin_users.rb
334
+ - app/controllers/concerns/koi/controller/has_attachments.rb
334
335
  - app/controllers/concerns/koi/controller/has_webauthn.rb
335
336
  - app/controllers/concerns/koi/controller/is_admin_controller.rb
336
337
  - app/controllers/concerns/koi/controller/json_web_token.rb