From d77fed5d349a6e254f2bf2f70684cb7d6d8483e7 Mon Sep 17 00:00:00 2001 From: fanqiang <316257774@qq.com> Date: Sun, 24 Nov 2013 20:02:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E5=A5=BD=E8=AF=B4=E5=95=8A=EF=BC=8C?= =?UTF-8?q?=E8=B5=B6=E7=B4=A7=E7=BB=93=E6=9D=9F=E5=90=A7~~~~~~~~~~~~~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/forums_controller.rb | 21 ++++++++++++++---- app/controllers/memos_controller.rb | 6 ++++++ app/models/forum.rb | 2 +- app/views/forums/_form.html.erb | 1 + app/views/forums/_show_topics.html.erb | 4 +++- app/views/forums/edit.html.erb | 1 + app/views/forums/index.html.erb | 30 +++++++++++++++++++++++--- app/views/forums/new.html.erb | 1 + app/views/forums/show.html.erb | 1 + 9 files changed, 58 insertions(+), 9 deletions(-) diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index f524048b4..eac43daf7 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -1,10 +1,18 @@ +# added by fq class ForumsController < ApplicationController # GET /forums # GET /forums.json - layout "base_admin" + def index - @forums = Forum.all + @offset, @limit = api_offset_and_limit({:limit => 10}) + @forums_all = Forum.all + @forums_count = @forums_all.count + @forums_pages = Paginator.new @forums_count, @limit, params['page'] + + @offset ||= @forums_pages.offset + # @forums = @forums_all.offset(@offset).limit(@limit).all + @forums = Forum.all respond_to do |format| format.html # index.html.erb format.json { render json: @forums } @@ -14,9 +22,14 @@ class ForumsController < ApplicationController # GET /forums/1 # GET /forums/1.json def show + @offset, @limit = api_offset_and_limit({:limit => 10}) @forum = Forum.find(params[:id]) - @memos = @forum.topics - + @memos_all = @forum.topics + @topic_count = @memos_all.count + @topic_pages = Paginator.new @topic_count, @limit, params['page'] + + @offset ||= @topic_pages.offset + @memos = @memos_all.offset(@offset).limit(@limit).all respond_to do |format| format.html { render :layout => 'base_forums' diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index fdb400bd8..2d241681b 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -24,6 +24,7 @@ class MemosController < ApplicationController @memo = Memo.new(params[:memo]) @memo.forum_id = params[:forum_id] @memo.author_id = User.current.id + @forum = Forum.find(params[:forum_id]) if @memo.parent_id @parent_memo = Memo.find_by_id(@memo.parent_id) @@ -32,11 +33,16 @@ class MemosController < ApplicationController respond_to do |format| if @memo.save + @forum.memo_count += 1 + @forum.last_memo_id = @memo.id @back_memo_id = (@memo.parent_id.nil? ? @memo.id : @memo.parent_id) if @parent_memo @parent_memo.last_reply_id = @memo.id @parent_memo.save + else + @forum.topic_count += 1 end + @forum.save format.html { redirect_to forum_memo_path(@memo.forum_id, @back_memo_id), notice: 'Memo was successfully created.' } format.json { render json: @memo, status: :created, location: @memo } diff --git a/app/models/forum.rb b/app/models/forum.rb index 6a56972b3..8a15171dc 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -1,6 +1,6 @@ class Forum < ActiveRecord::Base include Redmine::SafeAttributes - has_many :topics, :class_name => 'Memo', :conditions => "#{Memo.table_name}.parent_id IS NULL" + has_many :topics, :class_name => 'Memo', :conditions => "#{Memo.table_name}.parent_id IS NULL", :order => "#{Memo.table_name}.created_at DESC" has_many :memos, :dependent => :destroy belongs_to :creator, :class_name => "User", :foreign_key => 'creator_id' safe_attributes 'name', diff --git a/app/views/forums/_form.html.erb b/app/views/forums/_form.html.erb index cbda66a04..04053dcef 100644 --- a/app/views/forums/_form.html.erb +++ b/app/views/forums/_form.html.erb @@ -1,3 +1,4 @@ + <%= labelled_form_for(@forum) do |f| %> <% if @forum.errors.any? %> diff --git a/app/views/forums/_show_topics.html.erb b/app/views/forums/_show_topics.html.erb index d0adb4ba9..b7f68196a 100644 --- a/app/views/forums/_show_topics.html.erb +++ b/app/views/forums/_show_topics.html.erb @@ -1,3 +1,4 @@ +
<%=h @forum.name %>
共有 <%=link_to memos.count %> 个贴子
@@ -7,7 +8,7 @@
<%= link_to image_tag(url_to_avatar(topic.author), :class => "avatar"), user_path(topic.author) %> - +
<%= link_to h(topic.subject), forum_memo_path(@forum, topic) %> @@ -32,6 +33,7 @@
<% end %> + <% else %>

<%= l(:label_no_data) %>

<% end %> diff --git a/app/views/forums/edit.html.erb b/app/views/forums/edit.html.erb index 483e3b5fd..e8111d61c 100644 --- a/app/views/forums/edit.html.erb +++ b/app/views/forums/edit.html.erb @@ -1,3 +1,4 @@ +

Editing forum

<%= render 'form' %> diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index d9ad7b901..687a9b176 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -1,4 +1,29 @@ -

Listing forums

+ +
+ + + + + + + + + + + +
讨论区 <%= l(:label_user_location) %> : + <% if User.current.logged? %> + <%= link_to("新建讨论区", new_forum_path, :class => 'icon icon-add') %> + <% end %> + + +
<%= link_to "forge.trustie.net/forums", forums_path %> <%=link_to l(:field_homepage), home_path %> > <%=link_to "讨论区", forums_path %>
+
+ +<%= render :partial => 'forums/forum_list', :locals => {:forums => @forums} %> + + + -<%= link_to 'New Forum', new_forum_path %> diff --git a/app/views/forums/new.html.erb b/app/views/forums/new.html.erb index 703217a1a..565816a1e 100644 --- a/app/views/forums/new.html.erb +++ b/app/views/forums/new.html.erb @@ -1,3 +1,4 @@ +

New forum

<%= render 'form' %> diff --git a/app/views/forums/show.html.erb b/app/views/forums/show.html.erb index 1d12ab6db..fcb4769d1 100644 --- a/app/views/forums/show.html.erb +++ b/app/views/forums/show.html.erb @@ -1,3 +1,4 @@ +

<%= notice %>