diff options
Diffstat (limited to 'examples/camping')
-rw-r--r-- | examples/camping/README | 3 | ||||
-rwxr-xr-x | examples/camping/blog.rb | 294 | ||||
-rw-r--r-- | examples/camping/tepee.rb | 149 |
3 files changed, 0 insertions, 446 deletions
diff --git a/examples/camping/README b/examples/camping/README deleted file mode 100644 index 73ae9a0..0000000 --- a/examples/camping/README +++ /dev/null @@ -1,3 +0,0 @@ -To get these examples running, install Camping. - -Instructions here: http://code.whytheluckystiff.net/camping/ diff --git a/examples/camping/blog.rb b/examples/camping/blog.rb deleted file mode 100755 index 81a87da..0000000 --- a/examples/camping/blog.rb +++ /dev/null @@ -1,294 +0,0 @@ -#!/usr/bin/env ruby - -$:.unshift File.dirname(__FILE__) + "/../../lib" -require 'rubygems' -require_gem 'camping', '>=1.4' -require 'camping/session' - -Camping.goes :Blog - -module Blog - include Camping::Session -end - -module Blog::Models - def self.schema(&block) - @@schema = block if block_given? - @@schema - end - - class Post < Base; belongs_to :user; end - class Comment < Base; belongs_to :user; end - class User < Base; end -end - -Blog::Models.schema do - create_table :blog_posts, :force => true do |t| - t.column :id, :integer, :null => false - t.column :user_id, :integer, :null => false - t.column :title, :string, :limit => 255 - t.column :body, :text - end - create_table :blog_users, :force => true do |t| - t.column :id, :integer, :null => false - t.column :username, :string - t.column :password, :string - end - create_table :blog_comments, :force => true do |t| - t.column :id, :integer, :null => false - t.column :post_id, :integer, :null => false - t.column :username, :string - t.column :body, :text - end - execute "INSERT INTO blog_users (username, password) VALUES ('admin', 'camping')" -end - -module Blog::Controllers - class Index < R '/' - def get - @posts = Post.find :all - render :index - end - end - - class Add - def get - unless @state.user_id.blank? - @user = User.find @state.user_id - @post = Post.new - end - render :add - end - def post - post = Post.create :title => input.post_title, :body => input.post_body, - :user_id => @state.user_id - redirect View, post - end - end - - class Info < R '/info/(\d+)', '/info/(\w+)/(\d+)', '/info', '/info/(\d+)/(\d+)/(\d+)/([\w-]+)' - def get(*args) - div do - code args.inspect; br; br - code ENV.inspect; br - code "Link: #{R(Info, 1, 2)}" - end - end - end - - class View < R '/view/(\d+)' - def get post_id - @post = Post.find post_id - @comments = Models::Comment.find :all, :conditions => ['post_id = ?', post_id] - render :view - end - end - - class Edit < R '/edit/(\d+)', '/edit' - def get post_id - unless @state.user_id.blank? - @user = User.find @state.user_id - end - @post = Post.find post_id - render :edit - end - - def post - @post = Post.find input.post_id - @post.update_attributes :title => input.post_title, :body => input.post_body - redirect View, @post - end - end - - class Comment - def post - Models::Comment.create(:username => input.post_username, - :body => input.post_body, :post_id => input.post_id) - redirect View, input.post_id - end - end - - class Login - def post - @user = User.find :first, :conditions => ['username = ? AND password = ?', input.username, input.password] - - if @user - @login = 'login success !' - @state.user_id = @user.id - else - @login = 'wrong user name or password' - end - render :login - end - end - - class Logout - def get - @state.user_id = nil - render :logout - end - end - - class Style < R '/styles.css' - def get - @headers["Content-Type"] = "text/css; charset=utf-8" - @body = %{ - body { - font-family: Utopia, Georga, serif; - } - h1.header { - background-color: #fef; - margin: 0; padding: 10px; - } - div.content { - padding: 10px; - } - } - end - end -end - -module Blog::Views - - def layout - html do - head do - title 'blog' - link :rel => 'stylesheet', :type => 'text/css', - :href => '/styles.css', :media => 'screen' - end - body do - h1.header { a 'blog', :href => R(Index) } - div.content do - self << yield - end - end - end - end - - def index - if @posts.empty? - p 'No posts found.' - p { a 'Add', :href => R(Add) } - else - for post in @posts - _post(post) - end - end - end - - def login - p { b @login } - p { a 'Continue', :href => R(Add) } - end - - def logout - p "You have been logged out." - p { a 'Continue', :href => R(Index) } - end - - def add - if @user - _form(post, :action => R(Add)) - else - _login - end - end - - def edit - if @user - _form(post, :action => R(Edit)) - else - _login - end - end - - def view - _post(post) - - p "Comment for this post:" - for c in @comments - h1 c.username - p c.body - end - - form :action => R(Comment), :method => 'post' do - label 'Name', :for => 'post_username'; br - input :name => 'post_username', :type => 'text'; br - label 'Comment', :for => 'post_body'; br - textarea :name => 'post_body' do; end; br - input :type => 'hidden', :name => 'post_id', :value => post.id - input :type => 'submit' - end - end - - # partials - def _login - form :action => R(Login), :method => 'post' do - label 'Username', :for => 'username'; br - input :name => 'username', :type => 'text'; br - - label 'Password', :for => 'password'; br - input :name => 'password', :type => 'text'; br - - input :type => 'submit', :name => 'login', :value => 'Login' - end - end - - def _post(post) - h1 post.title - p post.body - p do - a "Edit", :href => R(Edit, post) - a "View", :href => R(View, post) - end - end - - def _form(post, opts) - p do - text "You are logged in as #{@user.username} | " - a 'Logout', :href => R(Logout) - end - form({:method => 'post'}.merge(opts)) do - label 'Title', :for => 'post_title'; br - input :name => 'post_title', :type => 'text', - :value => post.title; br - - label 'Body', :for => 'post_body'; br - textarea post.body, :name => 'post_body'; br - - input :type => 'hidden', :name => 'post_id', :value => post.id - input :type => 'submit' - end - end -end - -def Blog.create - Camping::Models::Session.create_schema - unless Blog::Models::Post.table_exists? - ActiveRecord::Schema.define(&Blog::Models.schema) - end -end - -if __FILE__ == $0 - require 'mongrel/camping' - - Blog::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog.db' - Blog::Models::Base.logger = Logger.new('camping.log') - Blog::Models::Base.threaded_connections=false - Blog.create - - # Use the Configurator as an example rather than Mongrel::Camping.start - config = Mongrel::Configurator.new :host => "0.0.0.0" do - listener :port => 3002 do - uri "/blog", :handler => Mongrel::Camping::CampingHandler.new(Blog) - uri "/favicon", :handler => Mongrel::Error404Handler.new("") - trap("INT") { stop } - run - end - end - - puts "** Blog example is running at http://localhost:3002/blog" - puts "** Default username is `admin', password is `camping'" - config.join -end diff --git a/examples/camping/tepee.rb b/examples/camping/tepee.rb deleted file mode 100644 index 199be37..0000000 --- a/examples/camping/tepee.rb +++ /dev/null @@ -1,149 +0,0 @@ -#!/usr/bin/ruby -$:.unshift File.dirname(__FILE__) + "/../../lib" -%w(rubygems redcloth camping acts_as_versioned).each { |lib| require lib } - -Camping.goes :Tepee - -module Tepee::Models - def self.schema(&block) - @@schema = block if block_given? - @@schema - end - - class Page < Base - PAGE_LINK = /\[\[([^\]|]*)[|]?([^\]]*)\]\]/ - validates_uniqueness_of :title - before_save { |r| r.title = r.title.underscore } - acts_as_versioned - end -end - -Tepee::Models.schema do - create_table :tepee_pages, :force => true do |t| - t.column :title, :string, :limit => 255 - t.column :body, :text - end - Tepee::Models::Page.create_versioned_table -end - -module Tepee::Controllers - class Index < R '/' - def get - redirect Show, 'home_page' - end - end - - class List < R '/list' - def get - @pages = Page.find :all, :order => 'title' - render :list - end - end - - class Show < R '/s/(\w+)', '/s/(\w+)/(\d+)' - def get page_name, version = nil - redirect(Edit, page_name, 1) and return unless @page = Page.find_by_title(page_name) - @version = (version.nil? or version == @page.version.to_s) ? @page : @page.versions.find_by_version(version) - render :show - end - end - - class Edit < R '/e/(\w+)/(\d+)', '/e/(\w+)' - def get page_name, version = nil - @page = Page.find_or_create_by_title(page_name) - @page = @page.versions.find_by_version(version) unless version.nil? or version == @page.version.to_s - render :edit - end - - def post page_name - Page.find_or_create_by_title(page_name).update_attributes :body => input.post_body and redirect Show, page_name - end - end -end - -module Tepee::Views - def layout - html do - head do - title 'test' - end - body do - p do - small do - span "welcome to " ; a 'tepee', :href => "http://code.whytheluckystiff.net/svn/camping/trunk/examples/tepee/" - span '. go ' ; a 'home', :href => R(Show, 'home_page') - span '. list all ' ; a 'pages', :href => R(List) - end - end - div.content do - self << yield - end - end - end - end - - def show - h1 @page.title - div { _markup @version.body } - p do - a 'edit', :href => R(Edit, @version.title, @version.version) - a 'back', :href => R(Show, @version.title, @version.version-1) unless @version.version == 1 - a 'next', :href => R(Show, @version.title, @version.version+1) unless @version.version == @page.version - a 'current', :href => R(Show, @version.title) unless @version.version == @page.version - end - end - - def edit - form :method => 'post', :action => R(Edit, @page.title) do - p do - label 'Body' ; br - textarea @page.body, :name => 'post_body', :rows => 50, :cols => 100 - end - - p do - input :type => 'submit' - a 'cancel', :href => R(Show, @page.title, @page.version) - end - end - end - - def list - h1 'all pages' - ul { @pages.each { |p| li { a p.title, :href => R(Show, p.title) } } } - end - - def _markup body - return '' if body.blank? - body.gsub!(Tepee::Models::Page::PAGE_LINK) do - page = title = $1 - title = $2 unless $2.empty? - page = page.gsub /\W/, '_' - if Tepee::Models::Page.find(:all, :select => 'title').collect { |p| p.title }.include?(page) - %Q{<a href="#{self/R(Show, page)}">#{title}</a>} - else - %Q{<span>#{title}<a href="#{self/R(Edit, page, 1)}">?</a></span>} - end - end - RedCloth.new(body, [ :hard_breaks ]).to_html - end -end - -def Tepee.create - unless Tepee::Models::Page.table_exists? - ActiveRecord::Schema.define(&Tepee::Models.schema) - Tepee::Models::Page.reset_column_information - end -end - -if __FILE__ == $0 - require 'mongrel/camping' - - Tepee::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'tepee.db' - Tepee::Models::Base.logger = Logger.new('camping.log') - Tepee::Models::Base.threaded_connections=false - Tepee.create - - server = Mongrel::Camping::start("0.0.0.0",3000,"/tepee",Tepee) - puts "** Tepee example is running at http://localhost:3000/tepee" - server.acceptor.join -end |