Skip to content

How to easily integrate Sinatra with Bower using rails-assets.org

License

Notifications You must be signed in to change notification settings

tenex/rails-assets-sinatra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using Rails Assets with Sinatra

This is a minimal demo app showing how to use Rails Assets with Sinatra application.

Rails Assets integrates Bower with Sprockets-based applications.

Integration

Gemfile

This application is using Sinatra Asset Pipeline that integrates Sinatra with Sprockets.

You also need to add appropriate source and rails-assets- gems.

source 'https://rubygems.org'

gem 'bundler', '>= 1.7.0'

gem 'sinatra', require: 'sinatra/base'
gem 'sinatra-asset-pipeline', require: 'sinatra/asset_pipeline'

gem 'uglifier'
gem 'slim'

source 'https://rails-assets.org' do
  gem 'rails-assets-jquery'
  gem 'rails-assets-bootstrap'
end

application.rb

Integrating Rails Assets with Sprockets is as simple as appening its load paths (see application.rb).

class Application < Sinatra::Base
  configure do
    set :assets_precompile, %w(application.js application.css *.png *.jpg *.svg *.eot *.ttf *.woff)
    set :assets_css_compressor, :sass
    set :assets_js_compressor, :uglifier
    register Sinatra::AssetPipeline

    # Actual Rails Assets integration, everything else is Sprockets
    if defined?(RailsAssets)
      RailsAssets.load_paths.each do |path|
        settings.sprockets.append_path(path)
      end
    end
  end

  get '/' do
    slim :index
  end
end

assets/js/application.js.coffee

You use it the same way as in Rails.

#= require jquery
#= require bootstrap

assets/css/application.css.scss

Usually it's better to use @import instead of require:

@import "bootstrap";

views/layout.slim

You can use Sprockets Helpers to include application manifests:

doctype html
html lang="en"
  head
    meta charset="utf-8"
    title Sinatra with Rails Assets
    meta name="viewport" content="width=device-width, initial-scale=1"
    == stylesheet_tag 'application'
  body
    .container
      == yield
    == javascript_tag 'application'

Running application

You can use any ruby server, including bare rack:

bundle exec rackup --port 3000

Heroku deployment

Sinatra Asset Pipeline defines template for assets:precompile task. It is enabled in Rakefile:

require_relative 'application'

require 'sinatra/asset_pipeline/task'
Sinatra::AssetPipeline::Task.define!(Application)

Heroku automatically runs assets:precompile upon deploy, so you can just:

heroku create rails-assets-sinatra
git push heroku master

Deployed demo is available on: http://rails-assets-sinatra.herokuapp.com/

Contributing

If you think you can improve this template in any way, please send a PR.

License

MIT

About

How to easily integrate Sinatra with Bower using rails-assets.org

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •