Skip to content

Commit

Permalink
Re-implemented using Merb.
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyatom committed May 3, 2008
1 parent 30b0d09 commit 658834d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README
Expand Up @@ -9,7 +9,7 @@ Usage

Run the server:

$ ruby backchat.rb
$ merb -I backchat.rb


Then embed the javascript in a page or template:
Expand Down
143 changes: 68 additions & 75 deletions backchat.rb
@@ -1,9 +1,13 @@
require 'rubygems'
require 'sinatra'
require 'markaby'
require 'activerecord'
require 'markaby'
require 'bluecloth'

Merb::Router.prepare do |r|
r.match('/:reference.js').to(:controller => 'backchat', :action => 'embed').name(:embed)
r.match('/comments/:reference').to(:controller => 'backchat', :action => 'show', :method => 'get').name(:show)
r.match('/comments/create/:reference').to(:controller => 'backchat', :action => 'create', :method => 'post').name(:create)
end

ActiveRecord::Base.establish_connection({
:adapter => 'sqlite3',
:database => File.basename(__FILE__, '.rb') + '.db'
Expand All @@ -23,83 +27,72 @@ def self.create_table

Comment.create_table unless ActiveRecord::Base.connection.tables.include?(Comment.table_name)

Styles = <<-EOS
.backchat p { margin: 5px }
.backchat ul { list-style: none; padding-left: 0 }
.backchat li { background-color: #eee; padding: 0.2em 0.5em 0.5em 0.5em; }
.backchat li .content { background-color: #fff; padding: 0.5em }
EOS

get "/embed.js" do
header 'Content-Type' => 'text/javascript'
%{var backchat_reference = encodeURIComponent(encodeURIComponent(window.document.location));
document.write('<script src="#{server_address}/' + backchat_reference + '"><script>')}
end

# Write the comments directly into the requesting page
get "/:reference.:format" do
#puts application.env
header 'Content-Type' => 'text/javascript'
"document.write('#{comments(params)}');"
end

def server_address
# TODO: how to figure this out programmatically?
"http://localhost:4567"
end

get "/test/*" do
params.inspect
end
class Backchat < Merb::Controller
def embed
render "document.write('#{styles}'); document.write('#{render_comments}');", :format => :js
end

# Render the comments HTML
def comments(params)
comments = Comment.find_all_by_reference(params[:reference])
Markaby::Builder.new({:params => params, :comments => comments}) do
div.backchat do
if @comments.any?
h2 "#{@comments.length} comment(s)"
ul do
@comments.each do |comment|
li do
p.author "#{comment.author_name} said (#{comment.created_at.strftime("%Y-%m-%d %H:%M")})"
div.content { BlueCloth.new(comment.content).to_html }
def show
render_comments
end

def create
Comment.create!(params[:comment].merge(:reference => params[:reference]))
store_credentials
redirect request.referer
end

private

def store_credentials
session[:author_name] = params[:comment][:author_name] rescue nil
session[:author_email] = params[:comment][:author_email] rescue nil
end

def styles
"<style>
.backchat p { margin: 5px }
.backchat ul { list-style: none; padding-left: 0 }
.backchat li { background-color: #eee; padding: 0.2em 0.5em 0.5em 0.5em; }
.backchat li .content { background-color: #fff; padding: 0.5em }
.backchat form label { display: block; vertical-align: top }
.backchat form input, .backchat form textarea { width: 20em }
.backchat form textarea { height: 10em }
</style>".gsub("\n", " ")
end

def render_comments
comments = Comment.find_all_by_reference(params[:reference])
Markaby::Builder.new({:params => params, :comments => comments, :controller => self}) do
div.backchat do
if @comments.any?
h2 "#{@comments.length} comment(s)"
ul do
@comments.each do |comment|
li do
p.author "#{comment.author_name} said (#{comment.created_at.strftime("%Y-%m-%d %H:%M")})"
div.content { BlueCloth.new(comment.content).to_html }
end
end
end
else
h2 "No comments"
end
form(:action => "http://#{@controller.request.host}" +
@controller.url(:create, :reference => params[:reference]),
:method => "post") do
label { "Name: " + input(:type => "text", :name => "comment[author_name]", :value => @controller.session[:author_name]) }
label { "Email: " + input(:type => "text", :name => "comment[author_email]", :value => @controller.session[:author_email]) }
label { "Comment: " + textarea(:name => "comment[content]") }
button(:type => "submit") { "Post!" }
end
else
h2 "No comments"
end
form(:action => "#{server_address}/#{@params[:reference]}", :method => "post") do
label { "Name: " + input(:type => "text", :name => "author_name") }
label { "Email: " + input(:type => "text", :name => "author_email") }
label { "Comment: " + textarea(:name => "content") }
button(:type => "submit") { "Post!" }
end
end
end.to_s
end

# Show the comments, but as HTML
get "/:reference" do
Markaby::Builder.new({:params => params}) do
html do
head do
style Styles
end
body do
comments(params)
end
end
end.to_s
end.to_s
end
end

# Post a new comment for this reference
post "/:reference.:format" do
comment_attributes = params.dup.reject { |k,v| k == :format }
p comment_attributes
Comment.create!(comment_attributes)

# TODO: we need to redirect BACK here.
redirect params[:reference]
Merb.config do
session_store "cookie"
session_secret_key "rabbit rabbit rabbit rabiit"
exception_details = true
end
5 changes: 4 additions & 1 deletion test.html
@@ -1,5 +1,8 @@
<html>
<body>
<script src="http://localhost:4567/embed.js"></script>
<h1>Some page</h1>
<a href="#">this page</a>
<hr>
<script src="http://localhost:4000/blah.js"></script>
</body>
</html>

0 comments on commit 658834d

Please sign in to comment.