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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 => :create
  24. before_action :authorize, :except => [:index, :new]
  25. before_action :find_optional_project, :only => [:index, :new]
  26. accept_rss_auth :index
  27. accept_api_auth :index, :show, :create, :update, :destroy
  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 do
  48. @news = News.new # for adding news inline
  49. render :layout => false if request.xhr?
  50. end
  51. format.api
  52. format.atom do
  53. render_feed(
  54. @newss,
  55. :title =>
  56. (@project ? @project.name : Setting.app_title) +
  57. ": #{l(:label_news_plural)}"
  58. )
  59. end
  60. end
  61. end
  62. def show
  63. @comments = @news.comments.to_a
  64. @comments.reverse! if User.current.wants_comments_in_reverse_order?
  65. end
  66. def new
  67. raise ::Unauthorized unless User.current.allowed_to?(:manage_news, @project, :global => true)
  68. @news = News.new(:project => @project, :author => User.current)
  69. end
  70. def create
  71. @news = News.new(:project => @project, :author => User.current)
  72. @news.safe_attributes = params[:news]
  73. @news.save_attachments(params[:attachments] || (params[:news] && params[:news][:uploads]))
  74. if @news.save
  75. respond_to do |format|
  76. format.html do
  77. render_attachment_warning_if_needed(@news)
  78. flash[:notice] = l(:notice_successful_create)
  79. redirect_to params[:cross_project] ? news_index_path : project_news_index_path(@project)
  80. end
  81. format.api {render_api_ok}
  82. end
  83. else
  84. respond_to do |format|
  85. format.html {render :action => 'new'}
  86. format.api {render_validation_errors(@news)}
  87. end
  88. end
  89. end
  90. def edit
  91. end
  92. def update
  93. @news.safe_attributes = params[:news]
  94. @news.save_attachments(params[:attachments] || (params[:news] && params[:news][:uploads]))
  95. if @news.save
  96. respond_to do |format|
  97. format.html do
  98. render_attachment_warning_if_needed(@news)
  99. flash[:notice] = l(:notice_successful_update)
  100. redirect_to news_path(@news)
  101. end
  102. format.api {render_api_ok}
  103. end
  104. else
  105. respond_to do |format|
  106. format.html {render :action => 'edit'}
  107. format.api {render_validation_errors(@news)}
  108. end
  109. end
  110. end
  111. def destroy
  112. @news.destroy
  113. respond_to do |format|
  114. format.html do
  115. flash[:notice] = l(:notice_successful_delete)
  116. redirect_to project_news_index_path(@project)
  117. end
  118. format.api {render_api_ok}
  119. end
  120. end
  121. end