フォーラムの返答を新着順(日付の降順)で表示する

323 views
Skip to first unread message

Shin Wemon

unread,
Jun 8, 2015, 10:37:16 PM6/8/15
to redmine-...@googlegroups.com
お世話になります。

フォーラム内、各トピック内の返答部分は、個人設定の如何にかかわらず
古いものから順番に表示されますが、これを個人設定に依存させたく試行錯誤しております。

常に新着順にする場合は app/controllers/messages_controller.rb を
    @replies =  @topic.children.
      includes(:author, :attachments, {:board => :project}).
-     reorder("#{Message.table_name}.created_on ASC, #{Message.table_name}.id ASC").
+    reorder("#{Message.table_name}.created_on DESC, #{Message.table_name}.id DESC").
      limit(@reply_pages.per_page).
      offset(@reply_pages.offset).
      to_a
のように修正すればよさそうですが、
ここで個人設定の情報を引っ張ってきたいです。

探した所 User.current.wants_comments_in_reverse_order という定義を見つけましたので
分からないなりに app/controllers/messages_controller.rb を以下のように改造してみました。

    if User.current.wants_comments_in_reverse_order?
      @replies =  @topic.children.
        includes(:author, :attachments, {:board => :project}).
        reorder("#{Message.table_name}.created_on DESC, #{Message.table_name}.id DESC").
        limit(@reply_pages.per_page).
        offset(@reply_pages.offset).
        to_a
    else
      @replies =  @topic.children.
        includes(:author, :attachments, {:board => :project}).
        reorder("#{Message.table_name}.created_on ASC, #{Message.table_name}.id ASC").
        limit(@reply_pages.per_page).
        offset(@reply_pages.offset).
        to_a
    end

こう書くと動いているように見えますが、プログラム側はなんか冗長と言うか、ダサいです。
どう改造するのが良いでしょうか?

ngyuki

unread,
Jun 8, 2015, 11:16:25 PM6/8/15
to redmine-...@googlegroups.com
wants_comments_in_reverse_order が使われている箇所を真似てみると良いのでは無いでしょうか?

    https://github.com/redmine/redmine/blob/391140d280dc636794748d341d9c676ea61cd217/app/controllers/news_controller.rb#L62

この方法が最適かどうかはともかく、以下のように使われているようです。

    @comments = @news.comments.to_a
    @comments.reverse! if User.current.wants_comments_in_reverse_order?

ので、次のような感じではないでしょうか(実際に動かしてはいません)。


    @replies =  @topic.children.
      includes(:author, :attachments, {:board => :project}).
      reorder("#{Message.table_name}.created_on ASC, #{Message.table_name}.id ASC").
      limit(@reply_pages.per_page).
      offset(@reply_pages.offset).
      to_a
    @replies.reverse! if User.current.wants_comments_in_reverse_order?

Shin Wemon

unread,
Jun 9, 2015, 2:59:25 AM6/9/15
to redmine-...@googlegroups.com
ありがとうございます。
試してみた所、完全な逆順にはなりませんでした。

例えば 100 返答があり 1 ページあたり 25 件表示するような場合
 1 ページ目は 25 件め、 24 件め ・・・  1 件め の順に、
 2 ページ目は 50 件め、 49 件め ・・・ 26 件め の順に
並びます。

1 ページ目に 100 件めの返答が来て欲しいのですが、
ページをまたいで全ての返答の逆順にはならなかったようです。

おそらく limit / offset で絞り込みする前に
並べ替えを終わらせないといけないようです。

2015年6月9日 12:16 ngyuki <ngyu...@gmail.com>:

--
このメールは Google グループのグループ「Redmine Users (japanese)」のトピックを登録しているユーザーに送られています。
このトピックの登録を解除するには https://groups.google.com/d/topic/redmine-users-ja/pOdJLF3dHVM/unsubscribe にアクセスしてください。
このグループを退会し、グループのすべてのトピックの登録を解除するには redmine-users-...@googlegroups.com にメールを送信してください。
このグループに投稿するには redmine-...@googlegroups.com にメールを送信してください。
http://groups.google.com/group/redmine-users-ja からこのグループにアクセスしてください。
その他のオプションについては https://groups.google.com/d/optout にアクセスしてください。

ngyuki

unread,
Jun 9, 2015, 3:16:11 AM6/9/15
to redmine-...@googlegroups.com
なるほど、フォーラムにはページ送りがあるんですね。
だとするとおっしゃる通りこの方法だとダメですね、すみません。
Reply all
Reply to author
Forward
0 new messages