Access helpers on format_using
# avo-2
s
@lemon-wall-20836 @loud-jewelry-99127 I am sorry to bother you. I have a question about field. Since updating to Avo 2.38.0, I am unable to call the FieldHelper module functions from within content_tag. Is there a solution? What I want to do with the TITLE FILLED is to set the width and make line breaks. We also want to limit the number of characters displayed. We can handle this by creating custom fields, but I asked the question because I would like to complete the process using only FIELD if possible.
Copy code
---
  field :title,
        as: :text, hide_on: %i[show edit new],
        format_using: -> {
          content_tag(:div, class: 'whitespace-pre-line', style: 'width: 200px; word-wrap: break-word;') do
            truncate40(value) # it doesn't work. (undefined method `truncate40'). It worked with Avo 2.29.1.
          end
        }
Copy code
---
module FieldHelper
  def truncate40(value)
    return if value.blank?

    ActionView::Base.full_sanitizer.sanitize(value).truncate 40
  end
end
l
Hi @straight-pager-31449, thank you for getting in touch and providing a thorough explanation. I appreciate it. We've implemented the
Avo::ExecutionContext
to ensure proper isolation of the context in which the Procs are executed. Since the
format_using
lambda runs in isolation, it only has access to what we pass inside the
Avo::ExecutionContext
. I noticed that the includes are not being passed currently. To address this, I'll create a pull request (PR) that will fix it. In the meantime, you can use the following temporary fix:
Copy code
ruby

field :title,
  as: :text, hide_on: %i[show edit new],
  format_using: -> {
    content_tag(:div, class: 'whitespace-pre-line', style: 'width: 200px; word-wrap: break-word;') do
      extend field.class.included_modules.find { |m| m.name == "FieldHelper" }

      truncate40(value)
    end
  }
Notice that is only a temporary fix, I'll keep you updated on the PR progress in this thread. Let me know if it works.
s
@loud-jewelry-99127 Thank you for always checking and responding promptly! The temporary response you taught us worked well! Thank you very much! Thank you for planning to fix this in the PR. I'm looking forward to update.
l
There is an PR in review that fixes this issue. https://github.com/avo-hq/avo/pull/1880 I can't predict exactly when it will be released, until then I'm glad that we found an workaround.
s
Thank you very much for your prompt alternative proposal and PR response.