From 8181cb4bf77548088f959562f24be17f8ec053a3 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 17 Jan 2008 23:12:25 +0000 Subject: [PATCH] Add support for commenting on, and replying to, diary entries. --- app/controllers/diary_entry_controller.rb | 26 ++++++++++++++++------ app/models/diary_comment.rb | 7 ++++++ app/models/diary_entry.rb | 1 + app/views/diary_entry/_diary_comment.rhtml | 3 +++ app/views/diary_entry/_diary_entry.rhtml | 7 +++++- app/views/diary_entry/list.rhtml | 5 ++--- app/views/diary_entry/view.rhtml | 22 ++++++++++++++++++ config/routes.rb | 3 ++- db/migrate/010_diary_comments.rb | 21 +++++++++++++++++ 9 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 app/models/diary_comment.rb create mode 100644 app/views/diary_entry/_diary_comment.rhtml create mode 100644 app/views/diary_entry/view.rhtml create mode 100644 db/migrate/010_diary_comments.rb diff --git a/app/controllers/diary_entry_controller.rb b/app/controllers/diary_entry_controller.rb index 90e53e778..28eb903a3 100644 --- a/app/controllers/diary_entry_controller.rb +++ b/app/controllers/diary_entry_controller.rb @@ -14,19 +14,26 @@ class DiaryEntryController < ApplicationController end end end + + def comment + @entry = DiaryEntry.find(params[:id]) + @diary_comment = @entry.diary_comments.build(params[:diary_comment]) + @diary_comment.user = @user + if @diary_comment.save + redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id + else + render :action => 'view' + end + end def list if params[:display_name] @this_user = User.find_by_display_name(params[:display_name]) @title = @this_user.display_name + "'s diary" - if params[:id] - @entries=DiaryEntry.find(:all, :conditions => ['user_id = ? AND id = ?', @this_user.id, params[:id]]) - else - @entries=DiaryEntry.find(:all, :conditions => ['user_id = ?', @this_user.id], :order => 'created_at DESC') - end + @entries = DiaryEntry.find(:all, :conditions => ['user_id = ?', @this_user.id], :order => 'created_at DESC') else - @title = 'recent diary entries' - @entries=DiaryEntry.find(:all, :order => 'created_at DESC', :limit => 20) + @title = "Users' diaries" + @entries = DiaryEntry.find(:all, :order => 'created_at DESC', :limit => 20) end end @@ -46,4 +53,9 @@ class DiaryEntryController < ApplicationController render :content_type => Mime::RSS end + + def view + user = User.find_by_display_name(params[:display_name]) + @entry = DiaryEntry.find(:first, :conditions => ['user_id = ? AND id = ?', user.id, params[:id]]) + end end diff --git a/app/models/diary_comment.rb b/app/models/diary_comment.rb new file mode 100644 index 000000000..aaf48be4f --- /dev/null +++ b/app/models/diary_comment.rb @@ -0,0 +1,7 @@ +class DiaryComment < ActiveRecord::Base + belongs_to :user + belongs_to :diary_entry + + validates_presence_of :body + validates_associated :diary_entry +end diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index 8894f4435..5b3d34766 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -1,5 +1,6 @@ class DiaryEntry < ActiveRecord::Base belongs_to :user + has_many :diary_comments, :order => "id" validates_presence_of :title, :body validates_numericality_of :latitude, :allow_nil => true diff --git a/app/views/diary_entry/_diary_comment.rhtml b/app/views/diary_entry/_diary_comment.rhtml new file mode 100644 index 000000000..2e7d0a531 --- /dev/null +++ b/app/views/diary_entry/_diary_comment.rhtml @@ -0,0 +1,3 @@ +

Comment from <%= link_to diary_comment.user.display_name, :controller => 'user', :action => 'view', :display_name => diary_comment.user.display_name %> at <%= diary_comment.created_at %>

+<%= htmlize(diary_comment.body) %> +
diff --git a/app/views/diary_entry/_diary_entry.rhtml b/app/views/diary_entry/_diary_entry.rhtml index 4cf824600..128ca8998 100644 --- a/app/views/diary_entry/_diary_entry.rhtml +++ b/app/views/diary_entry/_diary_entry.rhtml @@ -1,8 +1,13 @@ -<%= h(diary_entry.title) %>
+<%= link_to h(diary_entry.title), :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id %>
<%= htmlize(diary_entry.body) %> <% if diary_entry.latitude and diary_entry.longitude %> Coordinates:
<%= diary_entry.latitude %>; <%= diary_entry.longitude %>
(<%=link_to 'map', :controller => 'site', :action => 'index', :lat => diary_entry.latitude, :lon => diary_entry.longitude, :zoom => 14 %> / <%=link_to 'edit', :controller => 'site', :action => 'edit', :lat => diary_entry.latitude, :lon => diary_entry.longitude, :zoom => 14 %>)
<% end %> Posted by <%= link_to diary_entry.user.display_name, :controller => 'user', :action => 'view', :display_name => diary_entry.user.display_name %> at <%= diary_entry.created_at %>
+<% if params[:action] == 'list' %> +<%= link_to 'Comment on this entry', :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id, :anchor => 'comment' %> +| +<%= link_to 'Reply to this entry', :controller => 'message', :action => 'new', :user_id => diary_entry.user.id, :title => "Re: #{diary_entry.title}" %> +<% end %>

diff --git a/app/views/diary_entry/list.rhtml b/app/views/diary_entry/list.rhtml index a7aa84c6f..e05e90531 100644 --- a/app/views/diary_entry/list.rhtml +++ b/app/views/diary_entry/list.rhtml @@ -1,10 +1,9 @@ +

<%= @title %>

<% if @this_user %> -

<%= @this_user.display_name %>'s diary

<% if @user == @this_user %> <%= link_to 'New diary post', :controller => 'diary_entry', :action => 'new', :display_name => @user.display_name %> <% end %> <% else %> -

Users' diaries

<% if @user %> <%= link_to 'New diary post', :controller => 'diary_entry', :action => 'new', :display_name => @user.display_name %> <% end %> @@ -12,7 +11,7 @@

Recent diary entries:

-<%= render :partial => 'diary_entry/diary_entry', :collection => @entries %> +<%= render :partial => 'diary_entry', :collection => @entries %> <%= link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), :action => 'rss') %> <%= auto_discovery_link_tag(:atom, :action => 'rss') %> diff --git a/app/views/diary_entry/view.rhtml b/app/views/diary_entry/view.rhtml new file mode 100644 index 000000000..fbc316cc1 --- /dev/null +++ b/app/views/diary_entry/view.rhtml @@ -0,0 +1,22 @@ +

<%= @entry.user.display_name %>'s diary

+ +<%= render :partial => 'diary_entry', :object => @entry %> + +<%= render :partial => 'diary_comment', :collection => @entry.diary_comments %> + +<% if @user %> + +

Leave a comment

+<%= error_messages_for 'diary_comment' %> +<% form_for :diary_comment, @diary_comment, :url => { :action => 'comment' } do |f| %> +<%= f.text_area :body, :cols => 80, :rows => 5 %> +
+
+<%= submit_tag 'Save' %> +<% end %> + +<% else %> + +

<%= link_to "Login", :controller => 'user', :action => 'login', :referer => request.request_uri %> to leave a comment

+ +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 3cfc51977..92d849b4b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -97,7 +97,8 @@ ActionController::Routing::Routes.draw do |map| map.connect '/user/:display_name/make_friend', :controller => 'user', :action => 'make_friend' map.connect '/user/:display_name/remove_friend', :controller => 'user', :action => 'remove_friend' map.connect '/user/:display_name/diary', :controller => 'diary_entry', :action => 'list' - map.connect '/user/:display_name/diary/:id', :controller => 'diary_entry', :action => 'list', :id => /\d+/ + map.connect '/user/:display_name/diary/:id', :controller => 'diary_entry', :action => 'view', :id => /\d+/ + map.connect '/user/:display_name/diary/:id/newcomment', :controller => 'diary_entry', :action => 'comment', :id => /\d+/ map.connect '/user/:display_name/diary/rss', :controller => 'diary_entry', :action => 'rss' map.connect '/user/:display_name/diary/newpost', :controller => 'diary_entry', :action => 'new' map.connect '/user/:display_name/account', :controller => 'user', :action => 'account' diff --git a/db/migrate/010_diary_comments.rb b/db/migrate/010_diary_comments.rb new file mode 100644 index 000000000..be07d851a --- /dev/null +++ b/db/migrate/010_diary_comments.rb @@ -0,0 +1,21 @@ +class DiaryComments < ActiveRecord::Migration + def self.up + create_table "diary_comments", myisam_table do |t| + t.column "id", :bigint, :limit => 20, :null => false + t.column "diary_entry_id", :bigint, :limit => 20, :null => false + t.column "user_id", :bigint, :limit => 20, :null => false + t.column "body", :text, :null => false + t.column "created_at", :datetime, :null => false + t.column "updated_at", :datetime, :null => false + end + + add_primary_key "diary_comments", ["id"] + add_index "diary_comments", ["diary_entry_id", "id"], :name => "diary_comments_entry_id_idx", :unique => true + + change_column "diary_comments", "id", :bigint, :limit => 20, :null => false, :options => "AUTO_INCREMENT" + end + + def self.down + drop_table "diary_comments" + end +end -- 2.43.2