Add support for commenting on, and replying to, diary entries.
This commit is contained in:
parent
3eecc3ee5b
commit
8181cb4bf7
9 changed files with 83 additions and 12 deletions
|
@ -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
|
||||
|
|
7
app/models/diary_comment.rb
Normal file
7
app/models/diary_comment.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
class DiaryComment < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :diary_entry
|
||||
|
||||
validates_presence_of :body
|
||||
validates_associated :diary_entry
|
||||
end
|
|
@ -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
|
||||
|
|
3
app/views/diary_entry/_diary_comment.rhtml
Normal file
3
app/views/diary_entry/_diary_comment.rhtml
Normal file
|
@ -0,0 +1,3 @@
|
|||
<h4>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 %></h4>
|
||||
<%= htmlize(diary_comment.body) %>
|
||||
<hr />
|
|
@ -1,8 +1,13 @@
|
|||
<b><%= h(diary_entry.title) %></b><br />
|
||||
<b><%= link_to h(diary_entry.title), :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id %></b><br />
|
||||
<%= htmlize(diary_entry.body) %>
|
||||
<% if diary_entry.latitude and diary_entry.longitude %>
|
||||
Coordinates: <div class="geo" style="display: inline"><span class="latitude"><%= diary_entry.latitude %></span>; <span class="longitude"><%= diary_entry.longitude %></span></div> (<%=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 %>)<br/>
|
||||
<% end %>
|
||||
Posted by <b><%= link_to diary_entry.user.display_name, :controller => 'user', :action => 'view', :display_name => diary_entry.user.display_name %></b> at <%= diary_entry.created_at %><br />
|
||||
<% 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 %>
|
||||
<br />
|
||||
<hr />
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
<h2><%= @title %></h2>
|
||||
<% if @this_user %>
|
||||
<h2><%= @this_user.display_name %>'s diary</h2>
|
||||
<% if @user == @this_user %>
|
||||
<%= link_to 'New diary post', :controller => 'diary_entry', :action => 'new', :display_name => @user.display_name %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<h2>Users' diaries</h2>
|
||||
<% if @user %>
|
||||
<%= link_to 'New diary post', :controller => 'diary_entry', :action => 'new', :display_name => @user.display_name %>
|
||||
<% end %>
|
||||
|
@ -12,7 +11,7 @@
|
|||
|
||||
<h3>Recent diary entries:</h3>
|
||||
|
||||
<%= 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') %>
|
||||
|
|
22
app/views/diary_entry/view.rhtml
Normal file
22
app/views/diary_entry/view.rhtml
Normal file
|
@ -0,0 +1,22 @@
|
|||
<h2><%= @entry.user.display_name %>'s diary</h2>
|
||||
|
||||
<%= render :partial => 'diary_entry', :object => @entry %>
|
||||
|
||||
<%= render :partial => 'diary_comment', :collection => @entry.diary_comments %>
|
||||
|
||||
<% if @user %>
|
||||
|
||||
<h4 id="comment">Leave a comment</h4>
|
||||
<%= error_messages_for 'diary_comment' %>
|
||||
<% form_for :diary_comment, @diary_comment, :url => { :action => 'comment' } do |f| %>
|
||||
<%= f.text_area :body, :cols => 80, :rows => 5 %>
|
||||
<br />
|
||||
<br />
|
||||
<%= submit_tag 'Save' %>
|
||||
<% end %>
|
||||
|
||||
<% else %>
|
||||
|
||||
<h4 id="comment"><%= link_to "Login", :controller => 'user', :action => 'login', :referer => request.request_uri %> to leave a comment</h4>
|
||||
|
||||
<% end %>
|
|
@ -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'
|
||||
|
|
21
db/migrate/010_diary_comments.rb
Normal file
21
db/migrate/010_diary_comments.rb
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue