Keep ABC size down for conditional string concatenation in Ruby This method takes a comment object and displays it correctly depending on what columns are available. EVENT_TEXT and REASON_TEXT are hashes that map properties to more human readable strings, like so

EVENT_TEXT = { edit_item: 'edited an item' }
REASON_TEXT = { pickup_date: 'a pickup date change' }


The method that calls this one prepends this text with a username, so the text output expected is something along the lines of edited an item due to a pickup date change: the supplier is not ready yet.

  def comment_text(comment)
    text = ''
    if comment.event.present?
      text += EVENT_TEXT[comment.event.to_sym]
      if comment.reason.present?
        text += ' due to ' + REASON_TEXT[comment.reason.to_sym]
      end
    end
    text + ': ' + comment.text if comment.text.present?
  end


Which has a an Assignement Branch Condition size of 17, higher than Rubocop's default of 15. Is there a better way to concatenate strings conditionally in Ruby? I really don't feel like this method is doing too much.

Should I just increase Rubocop's threshhold? But even if I did, how would I handle a scenario like this with 10 conditions?
Perhaps just extracting the hash lookups would help:

def comment_event_string(comment)
  EVENT_TEXT[comment.event.to_sym]
end

def comment_reason_string(comment)
  " due to #{REASON_TEXT[comment.reason.to_sym]}"
end

def comment_text_string(comment)
  ": #{comment.text}"
end

def comment_text(comment)
  text = ''
  if comment.event.present?
    text += comment_event_string(comment)
    text += comment_reason_string(comment) if comment.reason.present?
  end
  text + comment_text_string(comment) if comment.text.present?
end


Passing comment around seems to be to be something of a code smell though, and I wonder if this code would be better placed inside the Comment class.

private def event_string
  EVENT_TEXT[event.to_sym]
end

private def reason_string
  " due to #{REASON_TEXTreason.to_sym]}"
end

private def text_string
  ": #{text}"
end

def nice_text(comment)
  nice_text = ''
  if event.present?
    nice_text += event_string
    nice_text += reason_string if reason.present?
  end
  nice_text + text_string if text.present?
end