#!/usr/bin/env ruby # Run app with # rm -rf forum.db ; camping forum.rb -d forum.db require 'rubygems' require 'camping' require 'camping/db' require 'camping/session' require 'dcss' require 'fixtures' Camping.goes :Forum module Forum include Camping::Session module Models class CreateForum < V 0.1 def self.up create_table :forum_topics do |t| t.column :title, :string end create_table :forum_posts do |t| t.column :user, :string # should be auth id t.column :body, :text t.column :topic_id, :integer end end end class Topic < Base ; has_many :posts end class Post < Base ; belongs_to :topic end end def self.create unless Models::SchemaInfo.table_exists? # create has already been called Models.create_schema Camping::Models::Session.create_schema # works? $FIXTURES[:topics].each do |topic| t = Models::Topic.create :title => topic[:title] topic[:posts].each do |post| t.posts.build(post).save end end end end module Controllers class JS < R '/js/(.+\.js)' def get(file) @headers['Content-Type'] = 'text/javascript' @headers['X-Sendfile'] = "#{File.dirname(__FILE__)}/scriptaculous-js-1.7.1_beta1/" + ( (file == 'prototype.js') ? "/lib/#{file}" : "/src/#{file}" ) end end class CSS < R '/css/(.+)\.css' def get(file) @headers['Content-Type'] = 'text/css' DuckCSS::CSSParser.new(File.read(file+'.dcss')).to_css end end class Index < R '/' def get #render :index render :main_component end end class Topics < R '/topics', '/topics/(\d+)' def get(id = :all) @headers['X-JSON'] = Models::Topic.find( id ).to_json end def post # int id is update # else create end # update def put(id) end # delete def delete(id) end end class Posts < R '/posts', '/posts/(\d+)' def get(id = :all) @headers['X-JSON'] = Models::Post.find( id ).to_json end def post # int id is update # else create end # update def put(id) end # delete def delete(id) end end class Session < R '/session' def get @headers['X-JSON'] = @state.to_json end def post @state.username = input.username end end end module Views def layout html { head { title "My App" script :src => "/js/prototype.js" script :src => "/js/scriptaculous.js" link :rel => 'stylesheet', :type => 'text/css', :href => '/css/forum.css', :media => 'screen' } body { yield script File.read('lib.js').gsub('λ','function') # should be included ... later script File.read('forum.js').gsub('λ','function') } } end def main_component "loading ..." end end end if __FILE__ == $0 require 'mongrel' require 'mongrel/camping' Forum::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'forum.db' Forum::Models::Base.logger = Logger.new('camping.log') Forum::Models::Base.threaded_connections = false Forum.create server = Mongrel::Camping::start("0.0.0.0",4000,"/",Forum) puts "** Forum example is running at http://localhost:4000/" server.run.join end