config.ru 1.42 KB
# -*- mode:ruby -*-

require 'sinatra'
require 'rack-flash'
require 'haml'

require './models'
require './maildrop_serializer'

class App < Sinatra::Base
  enable :sessions
  use Rack::Flash
  
  set :public, 'public'
  
  get '/' do 
    redirect '/filters'
  end
  
  get '/filters' do 
    @filters = Filter.find(:all)
    haml :filters
  end
  
  get '/filter/:id' do 
    if params[:id] == 'new'
      @filter = Filter.new
    else
      @filter = Filter.find(params[:id])
    end
    haml :filter
  end
    
  post '/filters' do 
    @filter = Filter.new(params[:filter])
    @filter.save!
    flash[:success] = "Filter created"
    redirect("/filters")
  end
  
  post '/filter/:id' do 
    @filter = Filter.find(params[:id])
    @filter.update_attributes(params[:filter])
    flash[:success] = "Filter updated"
    @filter.save!
    redirect("/filters")
  end
  
  post '/filter/:id/delete' do 
    @filter = Filter.find(params[:id])
    @filter.destroy
    flash[:notice] = "Filter deleted"
    redirect('/filters')
  end
  
  get '/expression/new' do 
    @expression = Expression.new
    partial(:expression, :expression => @expression)
  end
  
  helpers do 
    def partial(template, locals={}, options={})
      haml(template, options.merge(:locals => locals, :layout => false))
    end
    
    def option(value, label, selected)
      "<option value=\"#{value}\"#{selected && ' selected'}>#{label}</option>"
    end
  end
end

run App