[Solved] `Case/when` statement works some of the time


For some reason you iterate over REGEXS, ignore the item in the iteration, then hard-code them again… you actually do text.gsub(Supertag::Tag::USERTAG_REGEX) ... 3 times – once for each REGEX in your list.

Also, you misuse the case when construct, I suggest you read more about it

You should either drop the each entirely, and use only the explicit constants, or refactor you code to work dynamically, maybe something like:

  REGEXS = [[Supertag::Tag::USERTAG_REGEX, :usertag_path], 
            [Supertag::Tag::HASHTAG_REGEX, :hashtag_path], 
            [Supertag::Tag::MONEYTAG_REGEX, :moneytag_path]]

  def linkify_tags(taggable_content)
    text = taggable_content.to_s

    REGEXS.each do |regex, path|
      text = text.gsub(regex) {link_to($&, send(path, $2), class: 'tag')}
    end     

    text.html_safe
  end

1

solved `Case/when` statement works some of the time