0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Transform text files into strings on a global JavaScript object.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.14
~> 10.0
~> 3.0
 Project Readme

JsTextRails

Transform text files into strings on a global JavaScript object.

Why do this? Your Rails application might serve up a single-page web application, which requires text assets (like SVGs). You need these assets to render your application, and you don't want to fetch them via AJAX (to avoid lag), nor embed them in the returned HTML (to keep the HTML response small).

Especially in the case of SVGs, their contents are needed so they can be inserted into the document inline, so they can be styled with CSS.

Support is currently available for the following text file types:

  • ".svg" files

If you'd like more, please submit a PR.

Installation

Add this line to your application's Gemfile:

gem 'js-text-rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install js-text-rails

Usage

Ruby on Rails

After adding the gem to your Gemfile, clear your cache:

rake tmp:clear

Normally, text file contents are stored in a global object named TEXT or SVG (for ".svg" files). You may customize the namespace used by all text files by setting config.js_text_rails.namespace. e.g., in app/config/application.rb:

module App
  class Application < Rails::Application
    config.js_text_rails.namespace = 'App.Text'
  end
end

Then all text file contents would instead be stored in the global object App on a property called Text.

To disable the gem, just remove transformer:

JsTextRails.uninstall(Rails.application.assets)

Sprockets

If you use Sinatra or other non-Rails frameworks with Sprockets, just connect your Sprockets environment with it:

assets = Sprockets::Environment.new do |env|
  # Your assets settings
end

require 'js-text-rails'
JsTextRails.install(assets, namespace: 'App.Text')

In your application

require your files, e.g. in app/assets/javascripts/application.js:

//= require_tree ../images/icons

Where app/assets/images/icons contains the file envelope.svg.

Anywhere in your application's JavaScript code, access the text file contents via the global SVG variable:

var envelopeIcon = SVG['icons/envelope'];
// Do something with `envelopeIcon`...

Here is a practical example of inserting an SVG inline:

document.querySelector('#email-icon').innerHTML = SVG['icons/envelope'];

Or, in a JST template:

<span id="email-icon"><%- SVG['icons/envelope'] %></span> Email me at <%= @email %>!

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cloversites/js-text-rails.

License

The gem is available as open source under the terms of the MIT License.