Permalink
Browse files

added message posts model and a mess of supporting stuff

  • Loading branch information...
jlapier committed Sep 17, 2009
1 parent a115f19 commit b7469b96bc4a9d29c785f7295074207882ec9ed9
Showing with 1,591 additions and 122 deletions.
  1. +2 −0 app/controllers/forums_controller.rb
  2. +98 −0 app/controllers/message_posts_controller.rb
  3. +48 −0 app/helpers/gravatar_helper.rb
  4. +11 −0 app/helpers/message_posts_helper.rb
  5. +4 −1 app/models/forum.rb
  6. +63 −0 app/models/message_post.rb
  7. +7 −2 app/models/user.rb
  8. +80 −0 app/views/forums/_textile_ref.html.erb
  9. +8 −63 app/views/forums/index.html.erb
  10. +6 −27 app/views/forums/new.html.erb
  11. +16 −22 app/views/forums/show.html.erb
  12. +1 −0 app/views/layouts/application.html.erb
  13. +21 −0 app/views/message_posts/_message_form.html.erb
  14. +24 −0 app/views/message_posts/_message_in_forum.html.erb
  15. +19 −0 app/views/message_posts/_whole_message.html.erb
  16. +3 −0 app/views/message_posts/edit.html.erb
  17. +34 −0 app/views/message_posts/index.html.erb
  18. +3 −0 app/views/message_posts/new.html.erb
  19. +32 −0 app/views/message_posts/show.html.erb
  20. +3 −0 config/environment.rb
  21. +2 −0 config/routes.rb
  22. +27 −0 db/migrate/20090908180148_create_message_posts.rb
  23. +70 −2 public/stylesheets/application.css
  24. +44 −0 public/stylesheets/text_and_colors.css
  25. +131 −0 spec/controllers/message_posts_controller_spec.rb
  26. +1 −1 spec/fixtures/forums.yml
  27. +39 −0 spec/fixtures/message_posts.yml
  28. +1 −2 spec/fixtures/users.yml
  29. +11 −0 spec/helpers/message_posts_helper_spec.rb
  30. +4 −0 spec/integration/message_posts_spec.rb
  31. +20 −0 spec/models/message_post_spec.rb
  32. +63 −0 spec/routing/message_posts_routing_spec.rb
  33. +32 −0 spec/views/message_posts/edit.html.erb_spec.rb
  34. +39 −0 spec/views/message_posts/index.html.erb_spec.rb
  35. +32 −0 spec/views/message_posts/new.html.erb_spec.rb
  36. +27 −0 spec/views/message_posts/show.html.erb_spec.rb
  37. BIN test.db
  38. +1 −2 test/fixtures/users.yml
  39. +20 −0 vendor/plugins/searchable_by/MIT-LICENSE
  40. +55 −0 vendor/plugins/searchable_by/README
  41. +23 −0 vendor/plugins/searchable_by/Rakefile
  42. +2 −0 vendor/plugins/searchable_by/init.rb
  43. +1 −0 vendor/plugins/searchable_by/install.rb
  44. +137 −0 vendor/plugins/searchable_by/lib/searchable_by.rb
  45. +4 −0 vendor/plugins/searchable_by/tasks/searchable_by_tasks.rake
  46. +21 −0 vendor/plugins/searchable_by/test/boot.rb
  47. +22 −0 vendor/plugins/searchable_by/test/database.yml
  48. +10 −0 vendor/plugins/searchable_by/test/fixtures/companies.yml
  49. +5 −0 vendor/plugins/searchable_by/test/fixtures/company.rb
  50. +5 −0 vendor/plugins/searchable_by/test/fixtures/employee.rb
  51. +28 −0 vendor/plugins/searchable_by/test/fixtures/employees.yml
  52. +18 −0 vendor/plugins/searchable_by/test/fixtures/schema.rb
  53. +12 −0 vendor/plugins/searchable_by/test/helper.rb
  54. +43 −0 vendor/plugins/searchable_by/test/lib/activerecord_test_case.rb
  55. +75 −0 vendor/plugins/searchable_by/test/lib/activerecord_test_connector.rb
  56. +9 −0 vendor/plugins/searchable_by/test/lib/load_fixtures.rb
  57. +73 −0 vendor/plugins/searchable_by/test/searchable_by_test.rb
  58. +1 −0 vendor/plugins/searchable_by/uninstall.rb
@@ -16,6 +16,8 @@ def index
# GET /forums/1
# GET /forums/1.xml
def show
+ @message_posts = @forum.message_posts.paginate :page => params[:page], :order => 'created_at DESC'
+ @new_message_post = MessagePost.new :forum => @forum
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @forum }
@@ -0,0 +1,98 @@
+class MessagePostsController < ApplicationController
+ before_filter :require_user, :except => [:index, :show]
+
+ # GET /message_posts
+ # GET /message_posts.xml
+ def index
+ @message_posts = MessagePost.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @message_posts }
+ end
+ end
+
+ # GET /message_posts/1
+ # GET /message_posts/1.xml
+ def show
+ @message_post = MessagePost.find(params[:id])
+ if @message_post.thread
+ redirect_to message_post_url(@message_post.thread, :anchor => @message_post.id)
+ else
+ @child_posts = @message_post.child_posts.paginate :page => params[:page], :order => 'created_at ASC'
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @message_post }
+ end
+ end
+ end
+
+ # GET /message_posts/new
+ # GET /message_posts/new.xml
+ def new
+ @message_post = MessagePost.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @message_post }
+ end
+ end
+
+ # GET /message_posts/1/edit
+ def edit
+ @message_post = MessagePost.find(params[:id])
+ end
+
+ # POST /message_posts
+ # POST /message_posts.xml
+ def create
+ @message_post = MessagePost.new(params[:message_post])
+ @message_post.user = current_user
+ respond_to do |format|
+ if @message_post.save
+ flash[:notice] = "Posted: #{@message_post.subject}"
+ format.html do
+ if @message_post.thread
+ redirect_to message_post_url(@message_post.thread, :anchor => @message_post.id,
+ :page => @message_post.thread.child_posts.last_page_number_for)
+ else
+ redirect_to(@message_post)
+ end
+ end
+ format.xml { render :xml => @message_post, :status => :created, :location => @message_post }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @message_post.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /message_posts/1
+ # PUT /message_posts/1.xml
+ def update
+ @message_post = MessagePost.find(params[:id])
+
+ respond_to do |format|
+ if @message_post.update_attributes(params[:message_post])
+ flash[:notice] = 'MessagePost was successfully updated.'
+ format.html { redirect_to(@message_post) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @message_post.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /message_posts/1
+ # DELETE /message_posts/1.xml
+ def destroy
+ @message_post = MessagePost.find(params[:id])
+ @message_post.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(message_posts_url) }
+ format.xml { head :ok }
+ end
+ end
+end
@@ -0,0 +1,48 @@
+require 'digest/md5'
+require 'cgi'
+
+module GravatarHelper
+
+ DEFAULT_OPTIONS = {
+ :default => "http://aethora.com/images/hunting.gif",
+ :size => 50,
+ :rating => 'PG',
+ :alt => 'avatar',
+ :class => 'gravatar',
+ }
+
+
+
+ # Return the HTML img tag for the given user's gravatar. Presumes that
+ # the given user object will respond_to "email", and return the user's
+ # email address.
+ def gravatar_for(user, options={})
+ gravatar(user.email, options)
+ end
+
+ # Return the HTML img tag for the given email address's gravatar.
+ def gravatar(email, options={})
+ src = h(gravatar_url(email, options))
+ options = DEFAULT_OPTIONS.merge(options)
+ [:class, :alt, :size].each { |opt| options[opt] = h(options[opt]) }
+ "<img class=\"#{options[:class]}\" alt=\"#{options[:alt]}\" width=\"#{options[:size]}\" "+
+ "height=\"#{options[:size]}\" src=\"#{src}\" />"
+ end
+
+ # Return the gravatar URL for the given email address.
+ def gravatar_url(email, options={})
+ email_hash = Digest::MD5.hexdigest(email)
+ options = DEFAULT_OPTIONS.merge(options)
+ options[:default] = CGI::escape(options[:default]) unless options[:default].nil?
+ returning "http://www.gravatar.com/avatar/#{email_hash}.jpg?" do |url|
+ [:rating, :size, :default].each do |opt|
+ unless options[opt].nil?
+ value = h(options[opt])
+ url << "#{opt}=#{value}&"
+ end
+ end
+ end
+ end
+
+
+end
@@ -0,0 +1,11 @@
+module MessagePostsHelper
+ # TODO: change this to use the zoned plugin or something
+ def post_time(time)
+ if (Time.now - time) > 2600000
+ time.strftime "on %b %d, %Y"
+ else
+ time_ago_in_words(time) + " ago"
+ end
+ end
+
+end
View
@@ -1,5 +1,5 @@
# == Schema Information
-# Schema version: 20090904211126
+# Schema version: 20090908180148
#
# Table name: forums
#
@@ -15,4 +15,7 @@
class Forum < ActiveRecord::Base
validates_presence_of :title
+
+ belongs_to :most_recent_post, :class_name => 'MessagePost', :foreign_key => :newest_message_post_id
+ has_many :message_posts, :dependent => :destroy
end
View
@@ -0,0 +1,63 @@
+# == Schema Information
+# Schema version: 20090908180148
+#
+# Table name: message_posts
+#
+# id :integer not null, primary key
+# subject :string(255)
+# body :text(16777215
+# forum_id :integer
+# parent_id :integer
+# user_id :integer
+# to_user_id :integer
+# thread_id :integer
+# replied_to_at :datetime
+# created_at :datetime
+# updated_at :datetime
+# End Schema
+
+class MessagePost < ActiveRecord::Base
+ belongs_to :forum
+ belongs_to :user
+ belongs_to :thread, :class_name => 'MessagePost', :foreign_key => 'thread_id', :include => :user
+
+ has_many :child_posts, :class_name => 'MessagePost', :foreign_key => 'thread_id', :include => :user
+
+ validates_presence_of :subject, :body, :user_id
+
+ searchable_by :subject, :body
+
+ before_validation :fix_blank_subject
+
+ class << self
+ def search_forums(term)
+ # uses search from "searchable_by"
+ search term, nil, :limit => 20, :order => "messages.updated_at DESC"
+ end
+
+ def per_page
+ 10
+ end
+
+ def last_page_number_for(conditions=nil)
+ total = count :all, :conditions => conditions
+ [((total - 1) / per_page) + 1, 1].max
+ end
+ end
+
+ # only for threads
+ def most_recent_reply
+ @most_recent_reply ||= child_posts.find :first, :order => "message_posts.created_at DESC",
+ :include => :user
+ end
+
+ def fix_blank_subject
+ if subject.blank?
+ if thread
+ self.subject = "RE: #{thread.subject}"
+ else
+ self.subject = nil
+ end
+ end
+ end
+end
View
@@ -2,7 +2,7 @@
# == Schema Information
-# Schema version: 20090904211126
+# Schema version: 20090908180148
#
# Table name: users
#
@@ -23,17 +23,22 @@
# last_login_ip :string(255)
# current_login_ip :string(255)
# is_admin :boolean
-# is_moderator :boolean
# End Schema
class User < ActiveRecord::Base
attr_protected :is_admin
attr_protected :is_moderator
+ has_many :message_posts
acts_as_authentic
# TODO define this
def is_moderator_for_forum?(forum)
false
end
+
+
+ def name
+ display_name.blank? ? login : display_name
+ end
end
@@ -0,0 +1,80 @@
+<h4>Textile Quick Reference</h4>
+
+<p>
+ <b>Phrase</b> modifiers:<br />
+ _<em>emphasis</em>_<br />
+ *<strong>strong</strong>*<br />
+ ??<cite>citation</cite>??<br />
+ -deleted text-<br />
+ +inserted text+<br />
+ ^superscript^<br />
+ ~subscript~<br />
+</p>
+
+<p>
+ To insert a <b>link</b>:<br />
+ "linktext":url<br />
+</p>
+
+<p>
+ To insert an <b>image</b>:<br />
+ !imageurl!<br />
+ !imageurl!:url<br />
+</p>
+
+<p>
+ <b>Block</b> modifiers:<br />
+ Header: <strong>h<em>n</em>.</strong><br />
+ Blockquote: <strong>bq.</strong><br />
+ Numeric list: <strong>#</strong><br />
+ Bulleted list: <strong>*</strong><br />
+</p>
+
+<div class="textile_advanced" style="display:none">
+
+ <p>
+ To apply <b>attributes:</b><br />
+ (class)<br />
+ (#id)<br />
+ {style}<br />
+ [language]<br />
+ </p>
+
+ <p>
+ To insert a <b>footnote</b>:<br />
+ See foo<strong>[<em>n</em>]</strong>.<br />
+ <strong>fn<em>n</em></strong>. Foo.<br />
+ </p>
+
+ <p>
+ To <b>align</b> blocks:<br />
+ <b>&#60;</b> right<br />
+ <b>&#62;</b> left<br /><b>=</b> center<br />
+ <b>&#60;&#62;</b> justify<br />
+ </p>
+
+ <p>
+ To insert a <b>table</b>:<br />
+ |a|table|row|<br />
+ |a|table|row|<br />
+ </p>
+
+ <p>
+ To define an <b>acronym</b>:<br />
+ ABC(Always Be Closing)<br />
+ </p>
+
+ <p>
+ <b>Punctuation</b>:<br>
+ <b>"</b>quotes<b>"</b> &rarr; &#8220;quotes&#8221;<br />
+ <b>'</b>quotes<b>'</b> &rarr; &#8216;quotes&#8217;<br />
+ it<b>'</b>s &rarr; it&#8217;s<br />
+ -&gt; &rarr; &rarr;<br />
+ em <b>--</b> dash</b> &rarr; em &#8212; dash<br />
+ en <b>-</b> dash &rarr; en &#8211; dash<br />
+ 2 <b>x</b> 4 &rarr; 2 &#215; 4<br />
+ foo<b>(tm)</b> &rarr; foo&#8482;<br />
+ foo<b>(r)</b> &rarr; foo&#174;<br />
+ foo<b>(c)</b> &rarr; foo&#169;<br />
+ </p>
+</div>
Oops, something went wrong.

0 comments on commit b7469b9

Please sign in to comment.