katalyst / koi

Koi Gem
https://github.com/katalyst/koi
MIT License
8 stars 4 forks source link

Cannot use hint with a proc for GovUkImageField #587

Closed hasarindaKI closed 5 months ago

hasarindaKI commented 5 months ago

If you pass a proc to the hint option, it will error with wrong number of arguments (given 1, expected 0)

This is because in Koi, Koi::FormBuilder -> govuk_image_field method we are trying to get the max size supported.

max_size = hint[:max_size] || Koi.config.image_size_limit

Also, if you pass a block to the govuk_image_field it will be displayed twice(second one as a string content) This is because the super method correctly appends the passed block, but concat will treat it as a string.

super(attribute_name, hint:, **kwargs) do
  if block
    concat(yield)
  else
    concat(t("helpers.hint.default.image", max_size: @template.number_to_human_size(max_size)))
  end
end

Full method for reference.

def govuk_image_field(attribute_name, hint: {}, **kwargs, &block)
  max_size = hint[:max_size] || Koi.config.image_size_limit
  super(attribute_name, hint:, **kwargs) do
    if block
      concat(yield)
    else
      concat(t("helpers.hint.default.image", max_size: @template.number_to_human_size(max_size)))
    end
  end
end

Example

<%= form.govuk_image_field :image do %>
  <div class="govuk-hint">
    If you are creating a list of image + text items, ensure all images are sized the same.<br>
    Max 500px wide.
  </div>
<% end %>

Rendered view

Screenshot 2024-03-20 at 3 50 02 PM

sfnelson commented 5 months ago

Fixed in v4.5.9