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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 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_filter :find_model_object, :except => [:new, :create, :index]
  21. before_filter :find_project_from_association, :except => [:new, :create, :index]
  22. before_filter :find_project_by_project_id, :only => [:new, :create]
  23. before_filter :authorize, :except => [:index]
  24. before_filter :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 self, @news_count, @limit, params['page']
  39. @offset ||= @news_pages.current.offset
  40. @newss = scope.all(:include => [:author, :project],
  41. :order => "#{News.table_name}.created_on DESC",
  42. :offset => @offset,
  43. :limit => @limit)
  44. respond_to do |format|
  45. format.html {
  46. @news = News.new # for adding news inline
  47. render :layout => false if request.xhr?
  48. }
  49. format.api
  50. format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
  51. end
  52. end
  53. def show
  54. @comments = @news.comments
  55. @comments.reverse! if User.current.wants_comments_in_reverse_order?
  56. end
  57. def new
  58. @news = News.new(:project => @project, :author => User.current)
  59. end
  60. def create
  61. @news = News.new(:project => @project, :author => User.current)
  62. @news.safe_attributes = params[:news]
  63. @news.save_attachments(params[:attachments])
  64. if @news.save
  65. render_attachment_warning_if_needed(@news)
  66. flash[:notice] = l(:notice_successful_create)
  67. redirect_to :controller => 'news', :action => 'index', :project_id => @project
  68. else
  69. render :action => 'new'
  70. end
  71. end
  72. def edit
  73. end
  74. def update
  75. @news.safe_attributes = params[:news]
  76. @news.save_attachments(params[:attachments])
  77. if @news.save
  78. render_attachment_warning_if_needed(@news)
  79. flash[:notice] = l(:notice_successful_update)
  80. redirect_to :action => 'show', :id => @news
  81. else
  82. render :action => 'edit'
  83. end
  84. end
  85. def destroy
  86. @news.destroy
  87. redirect_to :action => 'index', :project_id => @project
  88. end
  89. private
  90. def find_optional_project
  91. return true unless params[:project_id]
  92. @project = Project.find(params[:project_id])
  93. authorize
  94. rescue ActiveRecord::RecordNotFound
  95. render_404
  96. end
  97. end