修复帖子统计问题

This commit is contained in:
yanxd 2013-11-28 15:29:19 +08:00
parent 49327905f8
commit 8f707f3987
4 changed files with 32 additions and 20 deletions

View File

@ -4,11 +4,11 @@ class Forum < ActiveRecord::Base
has_many :memos, :dependent => :destroy
belongs_to :creator, :class_name => "User", :foreign_key => 'creator_id'
safe_attributes 'name',
'description',
'topic_count',
'memo_count',
'last_memo_id',
'creator_id'
'description',
'topic_count',
'memo_count',
'last_memo_id',
'creator_id'
validates_presence_of :name, :creator_id
validates_length_of :name, maximum: 50
validates_length_of :description, maximum: 255
@ -16,5 +16,17 @@ class Forum < ActiveRecord::Base
acts_as_taggable
scope :by_join_date, order("created_at DESC")
def reset_counters!
self.class.reset_counters!(id)
end
# Updates topic_count, memo_count and last_memo_id attributes for +board_id+
def self.reset_counters!(forum_id)
forum_id = forum_id.to_i
update_all("topic_count = (SELECT COUNT(*) FROM #{Memo.table_name} WHERE forum_id=#{forum_id} AND parent_id IS NULL)," +
" memo_count = (SELECT COUNT(*) FROM #{Memo.table_name} WHERE forum_id=#{forum_id})," +
" last_memo_id = (SELECT MAX(id) FROM #{Memo.table_name} WHERE forum_id=#{forum_id})",
["id = ?", forum_id])
end
end

View File

@ -1,6 +1,6 @@
class Memo < ActiveRecord::Base
include Redmine::SafeAttributes
belongs_to :forums
belongs_to :forum
belongs_to :author, :class_name => "User", :foreign_key => 'author_id'
validates_presence_of :author_id, :forum_id, :subject
@ -13,10 +13,10 @@ class Memo < ActiveRecord::Base
acts_as_attachable
belongs_to :last_reply_id, :class_name => 'Memo', :foreign_key => 'last_reply_id'
# acts_as_searchable :column => ['subject', 'content'],
# #:include => { :forums => :p}
# #:include => { :forum => :p}
# #:project_key => "#{Forum.table_name}.project_id"
# :date_column => "#{table_name}.created_at"
# acts_as_event :title => Proc.new {|o| "#{o.forums.name}: #{o.subject}"},
# acts_as_event :title => Proc.new {|o| "#{o.forum.name}: #{o.subject}"},
# :description => :content,
# :group => :parent,
# :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
@ -29,7 +29,7 @@ class Memo < ActiveRecord::Base
"subject",
"content",
"forum_id",
"last_reply_id",
"last_memo_id",
"lock",
"parent_id",
"replies_count",
@ -39,8 +39,8 @@ class Memo < ActiveRecord::Base
# after_update :update_memos_forum
after_destroy :reset_counters!
# after_create :send_notification
after_save :plusParentAndForum
after_destroy :minusParentAndForum
# after_save :plusParentAndForum
# after_destroy :minusParentAndForum
# scope :visible, lambda { |*args|
# includes(:forum => ).where()
@ -62,7 +62,7 @@ class Memo < ActiveRecord::Base
if parent && parent.id
Memo.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id})
end
# forums.reset_counters!
forum.reset_counters!
end
def sticky=(arg)

View File

@ -34,12 +34,12 @@
<% @forums.each do |forum| %>
<tr>
<td><%= forum.name %></td>
<td><%= forum.description %></td>
<td><%= forum.creator.show_name %></td>
<td><%= link_to 'Show', forum %></td>
<td><%= link_to 'Edit', edit_forum_path(forum) %></td>
<td><%= link_to 'Destroy', forum, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td>< %= forum.name %></td>
<td>< %= forum.description %></td>
<td>< %= forum.creator.show_name %></td>
<td>< %= link_to 'Show', forum %></td>
<td>< %= link_to 'Edit', edit_forum_path(forum) %></td>
<td>< %= link_to 'Destroy', forum, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>

View File

@ -203,7 +203,7 @@ Redmine::MenuManager.map :top_menu do |menu|
menu.push :software_user, {:controller => 'users', :action => 'index'}
menu.push :contest_innovate, {:controller => 'bids', :action => 'contest', :project_type => 1}
menu.push :requirement_enterprise, {:controller => 'bids', :action => 'index'}
menu.push :project_module_forums, {:controller => 'forums', :action => 'index'}
menu.push :project_module_forums, forums_path
# menu.push :investor, :home_path