Skip to content

Instantly share code, notes, and snippets.

@aredridel
Forked from tealtan/combine_highlight.rb
Last active December 19, 2015 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aredridel/5906722 to your computer and use it in GitHub Desktop.
Save aredridel/5906722 to your computer and use it in GitHub Desktop.
module ApplicationHelper
def combine_highlight(selected_text, surrounding_text)
if surrounding_text.include? selected_text
split_text = surrounding_text.split(selected_text)
content_tag(:p, (split_text[0] || '').html_safe + content_tag(:span, selected_text, class:"selected") + (split_text[1] || '').html_safe)
else
content_tag(:p, surrounding_text, class:"selected")
end
end
end
@tealtan
Copy link

tealtan commented Jul 2, 2013

THANK YOU. I guess that translates to (nil or '').html_safe – I didn't know that ('').html_safe works with content_tag like that. Ah-mazing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment