public
Description: Your own personal hoodwink'd. Add commenting to any site.
Homepage:
Clone URL: git://github.com/lazyatom/backchat.git
lazyatom (author)
Sun May 04 03:56:29 -0700 2008
commit  3acd3e6ec1065b1bb44a1a6441bf282f5daefa8c
tree    0cd14d703a787ff7ba5be587a02adfb5ce0882f3
parent  c71ff0da14b56f881ee8d07c9224d1eb7eb94042
backchat / backchat.rb
100644 100 lines (88 sloc) 3.352 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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'
})
 
class Comment < ActiveRecord::Base
  def self.create_table
    ActiveRecord::Migration.create_table(table_name) do |t|
      t.string :reference
      t.string :author_email
      t.string :author_name
      t.text :content
      t.datetime :created_at
    end
  end
end
 
Comment.create_table unless ActiveRecord::Base.connection.tables.include?(Comment.table_name)
 
class Backchat < Merb::Controller
  def embed
    script = "document.write('#{render_comments}');"
    script += "document.write('#{styles}');" if params[:css]
    render script, :format => :js
  end
 
  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 || 'someone'} 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
      end
    end.to_s
  end
end
 
Merb.config do
  session_store "cookie"
  session_secret_key "rabbit rabbit rabbit rabiit"
  exception_details = true
end