Permalink
Browse files

add simple shoutbox system

Fixes #14.
Progress on #57.
  • Loading branch information...
1 parent 7cf9aae commit 4132e59607a3aa03ca9ec220bd9feab13c72496a @colindean committed Nov 5, 2011
View
@@ -32,6 +32,9 @@ gem 'challonge-api' #https://bitbucket.org/corneldm/challonge-api/wiki/Home
gem 'icalendar'
+#for widgets, such as the shoutbox
+gem 'apotomo'
+
group :development,:test do
gem 'factory_girl_rails'
gem 'watchr'
View
@@ -42,6 +42,10 @@ GEM
activesupport (3.1.0)
multi_json (~> 1.0)
ansi (1.3.0)
+ apotomo (1.2.1)
+ cells (>= 3.6.7)
+ hooks (~> 0.2.0)
+ onfire (~> 0.2.0)
arel (2.2.1)
authlogic (3.0.3)
activerecord (>= 3.0.7)
@@ -58,6 +62,9 @@ GEM
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
+ cells (3.7.0)
+ actionpack (~> 3.0)
+ railties (~> 3.0)
challonge-api (0.1.1)
activeresource
childprocess (0.2.2)
@@ -74,6 +81,7 @@ GEM
gettext_i18n_rails (0.2.20)
fast_gettext
hike (1.2.1)
+ hooks (0.2.0)
i18n (0.6.0)
icalendar (1.1.6)
jquery-rails (1.0.14)
@@ -91,6 +99,7 @@ GEM
i18n (~> 0.4)
multi_json (1.0.3)
nokogiri (1.5.0)
+ onfire (0.2.0)
polyglot (0.3.2)
rack (1.3.2)
rack-cache (1.0.3)
@@ -154,6 +163,7 @@ PLATFORMS
DEPENDENCIES
activemerchant
acts_as_tree!
+ apotomo
authlogic
capybara
challonge-api
@@ -1,5 +1,11 @@
class HomeController < ApplicationController
+ has_widgets do |root|
+ @cu = current_user
+ root << widget(:shoutbox, :current_user => current_user)
+ puts current_user
+ end
+
def index
firebug "Initializing FirebugLogger..."
end
View
@@ -0,0 +1,8 @@
+class Shout < ActiveRecord::Base
+ belongs_to :user
+ validates :user, :presence => true
+ validates :text, :presence => true, :length => {:minimum => 0}
+
+ alias author user
+
+end
@@ -42,23 +42,7 @@
</div>
<div class="column" id="right">
- <section class="widget expanded" id="widget-shoutbox">
- <header>
- <h1>Shoutbox</h1>
- </header>
- <section class="content">
- <p>Got something to say? Say it! Note that this doesn't actually work yet.</p>
-
- <ul class="shouts">
- <% begin %>
- <li><%= link_to User.find(1).display_name, User.find(1) %>: Hip hip hooray!</li>
- <% rescue %>
-
- <% end %>
- </ul>
- <input type="text" name="shout[text]" placeholder="Shout it here!"/>
- </section>
- </section>
+ <%= render_widget :shoutbox %>
<section class="widget expanded" id="widget-opensource">
<header>
@@ -0,0 +1,23 @@
+<section class="widget expanded" id="shoutbox">
+ <header>
+ <h1>Shoutbox</h1>
+ </header>
+ <section class="content">
+ <% if @shouts and !@shouts.empty? %>
+ <ul class="shouts">
+ <% for shout in @shouts %>
+ <li>
+ <span class="user"><%= link_to shout.user.display_name, shout.user %></span><span class="colon">:</span>
+ <span class="text"><%= shout.text %></span>
+ </li>
+ <% end %>
+ </ul>
+ <% else %>
+ <p><%= _("Got something to say? Say it!") %></p>
+ <% end %>
+ <%= form_tag url_for_event(:write), :remote => true do %>
+ <%= text_field_tag 'text', nil, :placeholder => _("Shout it here!") %>
+ <%= submit_tag _("Shout!") %>
+ <% end %>
+ </section>
+</section>
@@ -0,0 +1,24 @@
+class ShoutboxWidget < Apotomo::Widget
+ responds_to_event :write
+
+ def display
+ @shouts = Shout.find(:all)
+ render
+ end
+
+ def write(event)
+ s = Shout.new
+ s.text = event[:text]
+ s.user = options[:current_user]
+
+
+ if s.save
+ @shouts = Shout.find(:all)
+ replace :view => :display
+ else
+ render :text => 'alert("{'+options[:current_user]+'} '+ s.errors.full_messages.join(_("and")) + '");'
+ end
+ end
+
+
+end
@@ -0,0 +1,11 @@
+class CreateShouts < ActiveRecord::Migration
+ def change
+ create_table :shouts do |t|
+ t.string :text
+ t.references :user
+
+ t.timestamps
+ end
+ add_index :shouts, :user_id
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111028234925) do
+ActiveRecord::Schema.define(:version => 20111105021131) do
create_table "addresses", :force => true do |t|
t.string "line1"
@@ -119,6 +119,15 @@
add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"
+ create_table "shouts", :force => true do |t|
+ t.string "text"
+ t.integer "user_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "shouts", ["user_id"], :name => "index_shouts_on_user_id"
+
create_table "tickets", :force => true do |t|
t.integer "event_id"
t.float "cents"
View
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
+
+one:
+ text: MyString
+ user:
+
+two:
+ text: MyString
+ user:
View
@@ -0,0 +1,26 @@
+require 'test_helper'
+
+class ShoutTest < ActiveSupport::TestCase
+ test "blank should not be valid" do
+ assert !Shout.new.valid?
+ end
+
+ test "without text but with user should not be valid" do
+ s = Shout.new
+ s.user = User.new
+ assert !s.valid?
+ end
+
+ test "without user but with text should not be valid" do
+ s = Shout.new
+ s.text = "LJSDFLKSJF"
+ assert !s.valid?
+ end
+
+ test "should be valid" do
+ s = Shout.new
+ s.text = "LKSJDLFKJSF"
+ s.user = User.new
+ assert s.valid?
+ end
+end
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class ShoutboxWidgetTest < Apotomo::TestCase
+ has_widgets do |root|
+ root << widget(:shoutbox)
+ end
+
+ test "display" do
+ render_widget :shoutbox
+ assert_select "h1"
+ end
+end

0 comments on commit 4132e59

Please sign in to comment.