Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix select helper with block returning non-string #51743

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Commits on May 6, 2024

  1. Fix select helper with block returning non-string

    As suggested by the docs for [FormOptionsHelper#select][1], the helper
    can be called with a block to customize option tag attributes:
    
        select(report, :campaign_ids) do
          available_campaigns.each do |c|
            tag.option(c.name, value: c.id, data: { tags: c.tags.to_json })
          end
        end
    
    Prior to this commit, a NoMethodError error would be raised when
    available_campaigns in the above example was the empty array. This
    happened because the block passed to select would return a non-string
    value (the empty array) causing the call to capture in
    Helpers::Tags::Select#initialize to return nil:
    
        def initialize(object_name, method_name, template_object, choices, options, html_options)
          @Choices = block_given? ? template_object.capture { yield || "" } : choices
    
    This commit fixes this by moving the `|| ""` outside the block passed to
    capture.
    
    [1]: https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
    calleluks committed May 6, 2024
    Configuration menu
    Copy the full SHA
    75c80c3 View commit details
    Browse the repository at this point in the history
  2. Use an explicit block argument

    As requested by RuboCop.
    calleluks committed May 6, 2024
    Configuration menu
    Copy the full SHA
    33bd853 View commit details
    Browse the repository at this point in the history