You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

news_controller.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class NewsController < ApplicationController
  18. default_search_scope :news
  19. model_object News
  20. before_action :find_model_object, :except => [:new, :create, :index]
  21. before_action :find_project_from_association, :except => [:new, :create, :index]
  22. before_action :find_project_by_project_id, :only => [:new, :create]
  23. before_action :authorize, :except => [:index]
  24. before_action :find_optional_project, :only => :index
  25. accept_rss_auth :index
  26. accept_api_auth :index
  27. helper :watchers
  28. helper :attachments
  29. def index
  30. case params[:format]
  31. when 'xml', 'json'
  32. @offset, @limit = api_offset_and_limit
  33. else
  34. @limit = 10
  35. end
  36. scope = @project ? @project.news.visible : News.visible
  37. @news_count = scope.count
  38. @news_pages = Paginator.new @news_count, @limit, params['page']
  39. @offset ||= @news_pages.offset
  40. @newss = scope.includes([:author, :project]).
  41. order("#{News.table_name}.created_on DESC").
  42. limit(@limit).
  43. offset(@offset).
  44. to_a
  45. respond_to do |format|
  46. format.html {
  47. @news = News.new # for adding news inline
  48. render :layout => false if request.xhr?
  49. }
  50. format.api
  51. format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
  52. end
  53. end
  54. def show
  55. @comments = @news.comments.to_a
  56. @comments.reverse! if User.current.wants_comments_in_reverse_order?
  57. end
  58. def new
  59. @news = News.new(:project => @project, :author => User.current)
  60. end
  61. def create
  62. @news = News.new(:project => @project, :author => User.current)
  63. @news.safe_attributes = params[:news]
  64. @news.save_attachments(params[:attachments])
  65. if @news.save
  66. render_attachment_warning_if_needed(@news)
  67. flash[:notice] = l(:notice_successful_create)
  68. redirect_to project_news_index_path(@project)
  69. else
  70. render :action => 'new'
  71. end
  72. end
  73. def edit
  74. end
  75. def update
  76. @news.safe_attributes = params[:news]
  77. @news.save_attachments(params[:attachments])
  78. if @news.save
  79. render_attachment_warning_if_needed(@news)
  80. flash[:notice] = l(:notice_successful_update)
  81. redirect_to news_path(@news)
  82. else
  83. render :action => 'edit'
  84. end
  85. end
  86. def destroy
  87. @news.destroy
  88. redirect_to project_news_index_path(@project)
  89. end
  90. end