Sinatra on Nginx on Ubuntu on Passenger

Just wanted to share our setup, as reminder and help for others 😉

(do not forget to create a tmp and public folder inside your projects folder)

#/opt/nginx/conf/nginx.conf
server {
  listen 80;
  server_name xxx.yyy.com;

  access_log /var/log/xxx_access.log  main;
  error_log /var/log/xxx_error.log debug;
  root /srv/xxx/public;   # <--- be sure to point to 'public'!
  passenger_use_global_queue on;
}

#/etc/hosts
127.0.0.1       xxx.yyy.com

#config.ru
require 'app'
disable :run
set :root, Pathname(__FILE__).dirname
run Sinatra::Application

#app.rb
require 'rubygems'
require 'sinatra'

get "/" do
  "Hello world from xxx"
end

ActiveRecord without default scope

This is a pretty hacky implementation of the feature we needed:
remove the default scope for some queries (e.g. update deleted records)

Code:

class ActiveRecord::Base
  def self.without_default_scope(&block)
    scope = scoped({})
    defaults = scope.current_scoped_methods_when_defined

    old = defaults[:find][:conditions]
    defaults[:find][:conditions] = {}

    begin
      yield(scope)
    ensure
      defaults[:find][:conditions] = old
    end
  end
end

Usage

MYModel.without_default_scope{|s| s.update_all(:deleted_at => nil)}

There is an less hacky alternative, but it did not work for us (AR 2.3.2)

Joined environment file for production and staging

Often production and staging share much, so why not use a common file for that…

#config/environments/production.rb and config/environments/staging.rb
eval(File.read("#{File.dirname(__FILE__)}/production_like.rb"))
...environment specific code ...

#config/environments/production_like.rb
config.cache_classes = true
...

the File.read/eval is rather hacky, but works nevertheless 😉