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.2KB

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