Skip to content

Instantly share code, notes, and snippets.

@edwinv
Created May 21, 2012 12:05
Show Gist options
  • Save edwinv/2762033 to your computer and use it in GitHub Desktop.
Save edwinv/2762033 to your computer and use it in GitHub Desktop.
What's wrong with current Rails asset gems

Rails 3.1 introduced the asset pipeline. One of the very cool things you can do now, is using a regular Ruby gem to include assets for your project:

group :assets do
   gem "jquery-rails"
   gem "chosen-rails"
end

Many asset libraries are not maintained as a Ruby gem and with the Rails Engine folder structure. Think jQuery, but also something like the Chosen library (https://github.com/harvesthq/chosen). As a result, many gems are popping up that transform the original library into a Ruby/Rails Engine gem. An example for chosen is the chosen-rails gem (https://github.com/tsechingho/chosen-rails).

So far, so good: we can include the Chosen library as an asset into our Rails 3.1 project. But the following problems can arise:

  1. The original library is updated, but the Rails gem needs an update too. By clever use of Thor this can be as simple as a single rake update-chosen, but we are still in the favors of the chosen-rails maintainers how fast the new gem is pushed.
  2. You want to improve the original Chosen library by forking the Chosen Github repository. It just happens that changes are not pulled into master and you want to keep using an alternative repository with the library. Because the chosen-rails gem has an hardcoded link to the original Chosen library, you also need to fork chosen-rails and update the source files with the files from your own repository.

These two problems have forced me to include some libraries in the old fashion way into our Rails projects. In my opinion the upswing of small asset gems for other libraries is not a good thing. We should find a way to translate the original source code into a Rails asset gem.

# Pseudocode for a mapping from the original remote library to a valid Rails Engine
#
# Partly borrowed from the chosen-rails gem at
# https://github.com/tsechingho/chosen-rails/blob/master/lib/chosen-rails/source_file.rb
class ChosenMapping < AssetMapping
def fetch
map "#{remote}/raw/master/chosen/chosen-sprite.png", "images/chosen-sprite.png"
map "#{remote}/raw/master/chosen/chosen.css", "stylesheets/chosen.css"
map "#{remote}/raw/master/coffee/lib/abstract-chosen.coffee", "javascripts/lib/abstract-chosen.coffee"
map "#{remote}/raw/master/coffee/lib/select-parser.coffee", "javascripts/lib/select-parser.coffee"
map "#{remote}/raw/master/coffee/chosen.jquery.coffee", "javascripts/chosen.jquery.coffee"
map "#{remote}/raw/master/coffee/chosen.proto.coffee", "javascripts/chosen.proto.coffee"
end
def convert
run("sass-convert -F css -T sass stylesheets/chosen.css stylesheets/chosen.css.sass")
gsub_file 'stylesheets/chosen.css.sass', '(chosen-sprite.png)', "('chosen-sprite.png')"
gsub_file 'stylesheets/chosen.css.sass', ' url', ' image-url'
end
end
# Define the original location of the library and the location of a mapping from this library
# to a valid Rails Engine. Making it easy to create a custom mapping on the local filesystem
# makes it very flexible, but sharing mappings with a Git repository should also be possible.
group :assets do
gem "chosen", :git => "git://github.com/harvesthq/chosen", :asset_mapping => "git://gist.github.com/..."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment