Project

rails_join

0.0
No commit activity in last 3 years
No release in over 3 years
Use [...].join without worrying
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.0.0
~> 1.6.4
~> 2.3.0

Runtime

 Project Readme

rails_join¶ ↑

Doing a ‘join` on an array containing html safe strings will produce an html unsafe string.

Rails provides a helper called ‘safe_join` that will always produce an html safe string.

This gem modifies ‘Array#join` so that it returns an html safe string when the first element is an html safe string. This is similar to `s1 + s2` returning an html safe string if and only if `s1` is html safe. If the first element is not an html safe string (or does not exist), then `Array#join` returns a normal string, as was previously the case. Again, this is similar to `s1 + s2` returning a normal string if `s1` is a normal string, even if `s2` is an html safe string.

links = [link_to("Home", "/"), link_to("Profile", "/profile")]
desired = links[0] + " > " + links[1]
joined  = links.join(" > ")
joined == desired # => false
joined.html_safe? # => false
require 'rails_join'
joined  = links.join(" > ")
joined == desired # => true
joined.html_safe? # => true

Installation¶ ↑

Simply add to your Gemfile:

gem "rails_join"

Run ‘bundle install` and you’re done.

Runs on all versions of Ruby and all versions of Rails >= 3.0

Why?¶ ↑

See: github.com/rails/rails/pull/3716

Copyright © 2012 Marc-Andre Lafortune. See LICENSE.txt for further details.