Skip to content

Commit

Permalink
SoftDelete for Topic/Reply;
Browse files Browse the repository at this point in the history
Topic/Reply can edit now;
Replace time_ago_in_words as jquery.timeago;
  • Loading branch information
huacnlee committed Sep 28, 2011
1 parent 672d20b commit 63ac287
Show file tree
Hide file tree
Showing 20 changed files with 273 additions and 64 deletions.
10 changes: 5 additions & 5 deletions app/controllers/cpanel/topics_controller.rb
Expand Up @@ -3,7 +3,7 @@ class Cpanel::TopicsController < Cpanel::ApplicationController
# GET /topics
# GET /topics.xml
def index
@topics = Topic.desc(:id).paginate :page => params[:page], :per_page => 30
@topics = Topic.unscoped.desc(:id).paginate :page => params[:page], :per_page => 30

respond_to do |format|
format.html # index.html.erb
Expand All @@ -14,7 +14,7 @@ def index
# GET /topics/1
# GET /topics/1.xml
def show
@topic = Topic.find(params[:id])
@topic = Topic.unscoped.find(params[:id])

respond_to do |format|
format.html # show.html.erb
Expand All @@ -35,7 +35,7 @@ def new

# GET /topics/1/edit
def edit
@topic = Topic.find(params[:id])
@topic = Topic.unscoped.find(params[:id])
end

# POST /topics
Expand All @@ -57,7 +57,7 @@ def create
# PUT /topics/1
# PUT /topics/1.xml
def update
@topic = Topic.find(params[:id])
@topic = Topic.unscoped.find(params[:id])

respond_to do |format|
if @topic.update_attributes(params[:topic])
Expand All @@ -73,7 +73,7 @@ def update
# DELETE /topics/1
# DELETE /topics/1.xml
def destroy
@topic = Topic.find(params[:id])
@topic = Topic.unscoped.find(params[:id])
@topic.destroy

respond_to do |format|
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/replies_controller.rb
@@ -0,0 +1,18 @@
# coding: utf-8
class RepliesController < ApplicationController
before_filter :require_user

def edit
@reply = current_user.replies.find(params[:id])
end

def update
@reply = current_user.replies.find(params[:id])

if @reply.update_attributes(params[:reply])
redirect_to(topic_path(@reply.topic_id), :notice => '回帖删除成功.')
else
render :action => "edit"
end
end
end
19 changes: 5 additions & 14 deletions app/controllers/topics_controller.rb
Expand Up @@ -88,10 +88,7 @@ def reply

# GET /topics/1/edit
def edit
@topic = Topic.find(params[:id])
if @topic.user_id != current_user.id
return render_404
end
@topic = current_user.topics.find(params[:id])
set_seo_meta("改帖子 &raquo; 社区论坛")
end

Expand All @@ -113,17 +110,14 @@ def create
# PUT /topics/1
# PUT /topics/1.xml
def update
@topic = Topic.find(params[:id])
if @topic.user_id != current_user.id
return render_404
end
@topic = current_user.topics.find(params[:id])
pt = params[:topic]
@topic.node_id = pt[:node_id]
@topic.title = pt[:title]
@topic.body = pt[:body]

if @topic.save
redirect_to(topics_path, :notice => '帖子修改成功.')
redirect_to(topic_path(@topic.id), :notice => '帖子修改成功.')
else
render :action => "edit"
end
Expand All @@ -132,11 +126,8 @@ def update
# DELETE /topics/1
# DELETE /topics/1.xml
def destroy
@topic = Topic.find(params[:id])
if @topic.user_id != current_user.id
return render_404
end
@topic = current_user.topics.find(params[:id])
@topic.destroy
redirect_to(topics_path, :notice => '帖子删除成功.')
redirect_to(topics_path, :notice => '帖子删除成功.')
end
end
16 changes: 9 additions & 7 deletions app/helpers/application_helper.rb
Expand Up @@ -16,12 +16,14 @@ def admin?(user)
return false
end

def cachable_time_ago_in_words(from)
if request.xhr?
raw time_ago_in_words from
else
js_call = javascript_tag "document.write(DateHelper.timeAgoInWords(#{(from.to_i * 1000).to_json}));"
raw "<noscript>on #{from.to_formatted_s(:long)}</noscript>#{js_call}"
end
def owner?(item)
return false if item.blank?
return if current_user.blank?
item.user_id == current_user.id
end

def timeago(time, options = {})
options[:class] ||= "timeago"
content_tag(:abbr, time.to_s, options.merge(:title => time.getutc.iso8601)) if time
end
end
29 changes: 29 additions & 0 deletions app/models/mongoid/soft_delete.rb
@@ -0,0 +1,29 @@
# coding: utf-8
# 软删除
module Mongoid
module SoftDelete
extend ActiveSupport::Concern

def self.included(base)
base.instance_eval do
field :deleted_at, :type => DateTime

default_scope where(:deleted_at => nil)
alias_method :destroy!, :destroy

include InstanceMethods
end
end

module InstanceMethods
def destroy
if persisted?
self.update_attributes(:deleted_at => Time.now.utc)
end

@destroyed = true
freeze
end
end
end
end
1 change: 1 addition & 0 deletions app/models/reply.rb
Expand Up @@ -2,6 +2,7 @@
class Reply
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::SoftDelete

field :body
field :source
Expand Down
1 change: 1 addition & 0 deletions app/models/topic.rb
Expand Up @@ -2,6 +2,7 @@
class Topic
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::SoftDelete

field :title
field :body
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -2,7 +2,7 @@
class User
include Mongoid::Document
include Mongoid::Timestamps

include Mongoid::SoftDelete

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
Expand Down
2 changes: 1 addition & 1 deletion app/views/cpanel/topics/index.html.erb
Expand Up @@ -15,7 +15,7 @@
</tr>

<% @topics.each do |topic| %>
<tr>
<tr class="<%= 'deleted' if !topic.deleted_at.blank? %>">
<td class="first"><%= truncate(topic.title,:length => 30) %></td>
<td><%= topic.node.name if topic.node %></td>
<td><%= topic.user.name if topic.user %></td>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Expand Up @@ -9,7 +9,7 @@
<link rel="shortcut icon" href="/favicon.ico?v=001" />
<%= csrf_meta_tag %>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<%= javascript_include_tag "rails","jquery.jdialog","application", :cache => "cached_application" %>
<%= javascript_include_tag "rails","jquery.jdialog","jquery.timeago","application", :cache => "cached_application" %>
<%= auto_discovery_link_tag(:rss,feed_topics_url,:title => '订阅最新贴') %>
<%= yield :scripts %>
</head>
Expand Down
2 changes: 1 addition & 1 deletion app/views/notes/index.html.erb
Expand Up @@ -12,7 +12,7 @@
<% @notes.each do |note| %>
<li>
<p><a class="title" href="<%= note_path(note.id) %>"><%= truncate(note.title, :length => 50) %></a></p>
<p class="info"><%= note.word_count %> 个字符, 创建于 <%= time_ago_in_words(note.created_at) %>, <%= note.changes_count %> 次编辑</p>
<p class="info"><%= note.word_count %> 个字符, 创建于 <%= timeago(note.created_at) %>, <%= note.changes_count %> 次编辑</p>
</li>
<% end %>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion app/views/notes/show.html.erb
Expand Up @@ -6,7 +6,7 @@
<div id="notes" class="box">
<div class="note">
<div class="body"><%= format_topic_body @note.body %></div>
<div class="info"><%= @note.word_count %> 个字符, 创建于 <%= time_ago_in_words(@note.created_at) %>, <%= @note.changes_count %> 次编辑</div>
<div class="info"><%= @note.word_count %> 个字符, 创建于 <%= timeago(@note.created_at) %>, <%= @note.changes_count %> 次编辑</div>
<div class="buttons">
<a href="<%= edit_note_path(@note) %>" class="button submit">编辑</a> &nbsp; &nbsp; &nbsp;
<%= link_to "删除", note_path(@note),:confirm => "确定要删除吗?", :method => :delete, :class => "button" %>
Expand Down
26 changes: 26 additions & 0 deletions app/views/replies/edit.html.erb
@@ -0,0 +1,26 @@
<%= render '/topics/base' %>
<% content_for :sitemap do %>
<span class="current">修改回帖</span>
<% end %>

<div class="leftbox">
<div class="box">
<h1>修改回帖</h1>
<%= form_for(@reply) do |f| %>
<%= render "shared/error_messages", :target => @reply %>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body, :class => "topic_body_text_area long", :style => "height:400px" %>
</div>
<div class="actions">
<button class="button submit">保存</button>
</div>
<% end %>
</div>
</div>
<div class="sidebar">
<div class="box">
回帖所在文章:<br />
<a href="<%= topic_path(@reply.topic_id) %>"><%= @reply.topic.title %></a>
</div>
</div>
4 changes: 2 additions & 2 deletions app/views/topics/index.html.erb
Expand Up @@ -59,9 +59,9 @@
</p>
<p class="info time">
<% if topic.last_reply_user.blank? %>
发布于 <%= time_ago_in_words(topic.created_at) %>
发布于 <%= timeago(topic.created_at) %>
<% else %>
最后由 <%= user_name_tag(topic.last_reply_user) %> 回复于 <%= time_ago_in_words(topic.replied_at) %>
最后由 <%= user_name_tag(topic.last_reply_user) %> 回复于 <%= timeago(topic.replied_at) %>
<% end %>
</p>
</td>
Expand Down
19 changes: 16 additions & 3 deletions app/views/topics/show.html.erb
Expand Up @@ -36,9 +36,9 @@
</p>
<p class="info time">
<% if @topic.last_reply_user.blank? %>
发布于 <%= time_ago_in_words(@topic.created_at) %>
发布于 <%= timeago(@topic.created_at) %>
<% else %>
最后由 <a href="<%= user_path(@topic.last_reply_user.login) %>"><%= @topic.last_reply_user.name %></a> 回复于 <%= time_ago_in_words(@topic.replied_at) %>
最后由 <a href="<%= user_path(@topic.last_reply_user.login) %>"><%= @topic.last_reply_user.name %></a> 回复于 <%= timeago(@topic.replied_at) %>
<% end %>
</p>
</td>
Expand All @@ -50,6 +50,12 @@
<div class="body">
<%= format_topic_body(@topic.body) %>
</div>
<div class="tools">
<% if owner?(@topic) %>
<a href="<%= edit_topic_path(@topic.id) %>">修改</a>
<%= link_to "删除", topic_path, :method => :delete, :confirm => "确定要删除么?" %>
<% end %>
</div>
</div>
<% if @replies.blank? %>
<div class="no_result">
Expand All @@ -68,11 +74,18 @@
<td>
<div class="info clearfix">
<span class="name"><%= user_name_tag(reply.user,:location => true) %></span>
<span class="time"><%= i + 1 %>楼, 回复于 <%= time_ago_in_words(reply.created_at) %><a href="#" onclick="return reply(<%= i + 1 %>);" title="回复此楼" class="reply_link"><%= image_tag("reply.png") %></a></span>
<span class="time"><%= i + 1 %>楼, 回复于 <%= timeago(reply.created_at) %>
<% if owner?(reply) %>
<a href="<%= edit_reply_path(reply.id) %>">修改</a>
<% end %>
<a href="#" onclick="return reply(<%= i + 1 %>);" title="回复此楼" class="reply_link"><%= image_tag("reply.png") %></a></span>
</div>
<div class="body">
<%= format_topic_body(reply.body,"",false) %>
</div>
<div class="tools">

</div>
</td>
</tr>
</table>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -20,6 +20,7 @@
get :feed
end
end
resources :replies
resources :photos do
collection do
get :tiny_new
Expand Down
30 changes: 3 additions & 27 deletions public/javascripts/application.js
@@ -1,27 +1,3 @@
// Like Rails DataHelper
var DateHelper = {
timeAgoInWords: function(from) {
return this.distanceOfTimeInWords(new Date().getTime(), from);
},

distanceOfTimeInWords: function(to, from) {
seconds_ago = ((to - from) / 1000);
minutes_ago = Math.floor(seconds_ago / 60)

if(minutes_ago == 0) { return "不到一分钟";}
if(minutes_ago == 1) { return "一分钟";}
if(minutes_ago < 45) { return minutes_ago + "分钟";}
if(minutes_ago < 90) { return "大约一小时";}
hours_ago = Math.round(minutes_ago / 60);
if(minutes_ago < 1440) { return hours_ago + "小时";}
if(minutes_ago < 2880) { return "一天";}
days_ago = Math.round(minutes_ago / 1440);
if(minutes_ago < 43200) { return days_ago + "天";}
if(minutes_ago < 86400) { return "大约一月";}
months_ago = Math.round(minutes_ago / 43200);
if(minutes_ago < 525960) { return months_ago + "月";}
if(minutes_ago < 1051920) { return "大约一年";}
years_ago = Math.round(minutes_ago / 525960);
return "超过" + years_ago + "年"
}
}
$(document).ready(function() {
$("abbr.timeago").timeago();
});

0 comments on commit 63ac287

Please sign in to comment.