Skip to content
Jeremy Jackson edited this page May 28, 2015 · 1 revision

Micro Applications

Sometimes a full Rails application isn't required -- for instance if you want to utilize the Rails foundation, the asset pipeline and other tools, but don't have a standard rails new install.

I've found these "Micro Apps" to be useful in stubbing out javascript functionality, but don't need a full rails install with all of it's workings.

You can put all of your Rails configuration and loading into a config.ru file and then fire that up with a simple rackup command.

/config.ru

require "rubygems"
require "bundler/setup"
require "action_controller/railtie"
require "sprockets/railtie"
require "teaspoon-mocha"

Bundler.require

module Micro
  class Application < Rails::Application
    config.session_store :cookie_store, :key => "_app_session"
    config.secret_token = "30dd9731a121fb4a3425fb528ffed853"
    config.active_support.deprecation = :log
    config.consider_all_requests_local = true
    config.encoding = "utf-8"
    config.eager_load = false

    config.assets.enabled = true
    config.assets.version = "1.0"
    config.assets.debug = true

    config.assets.paths = []
    config.assets.paths << "lib/dependencies"
    config.assets.paths << "lib/javascripts"
    config.assets.paths << "lib/stylesheets"
  end
end

class ApplicationController < ActionController::Base
  prepend_view_path Rails.application.root

  def page
    render template: params[:page] || "index"
  end
end

Rails.application.initialize! rescue nil
Rails.application.routes.draw do
  # rendering pages
  match "/(:page)" => "application#page", via: :get
end

run Micro::Application rescue nil

Then, you can create a spec/teaspoon_env.rb that loads Rails, and Teaspoon will be available as it would be within a full Rails application.

/teaspoon_env.rb

if defined?(Rails) # keep teaspoon from double loading

  Teaspoon.configure do |config|
    config.asset_paths  = ["spec", "spec/support"]
    config.fixture_paths = ["spec/fixtures"]

    config.suite do |suite|
      suite.use_framework :mocha
      suite.matcher = "spec/**/*_spec.{js,js.coffee,coffee}"
      suite.javascripts += ["support/chai", "support/sinon", "support/sinon-chai"]
    end

    config.coverage do |coverage|
      coverage.ignore << %r(/dependencies)
      coverage.reports = ["text", "html", "cobertura"]
    end
  end

else # load rails so teaspoon is loaded

  ENV["RAILS_ROOT"] = File.expand_path("..", __FILE__)
  load File.expand_path("../config.ru", __FILE__)

end