diff --git a/app/models/forum.rb b/app/models/forum.rb index 993c00b23..a235a30c4 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -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 diff --git a/app/models/memo.rb b/app/models/memo.rb index 9a87009ec..b494064be 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -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) diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 80c379ed0..b38ecb358 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -34,12 +34,12 @@ <% @forums.each do |forum| %> -<%= forum.name %> -<%= forum.description %> -<%= forum.creator.show_name %> -<%= link_to 'Show', forum %> -<%= link_to 'Edit', edit_forum_path(forum) %> -<%= link_to 'Destroy', forum, method: :delete, data: { confirm: 'Are you sure?' } %> +< %= forum.name %> +< %= forum.description %> +< %= forum.creator.show_name %> +< %= link_to 'Show', forum %> +< %= link_to 'Edit', edit_forum_path(forum) %> +< %= link_to 'Destroy', forum, method: :delete, data: { confirm: 'Are you sure?' } %> <% end %> diff --git a/lib/redmine.rb b/lib/redmine.rb index ee3967aae..661cbaaf1 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -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