news_controller.rb 3.33 KB
Newer Older
1
# Redmine - project management software
marutosijp's avatar
marutosijp committed
2
# Copyright (C) 2006-2014  Jean-Philippe Lang
3 4 5 6 7
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
8
#
9 10 11 12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
13
#
14 15 16 17 18
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class NewsController < ApplicationController
jplang's avatar
jplang committed
19
  default_search_scope :news
20
  model_object News
21 22
  before_filter :find_model_object, :except => [:new, :create, :index]
  before_filter :find_project_from_association, :except => [:new, :create, :index]
23
  before_filter :find_project_by_project_id, :only => [:new, :create]
24
  before_filter :authorize, :except => [:index]
25
  before_filter :find_optional_project, :only => :index
jplang's avatar
jplang committed
26 27
  accept_rss_auth :index
  accept_api_auth :index
28

29
  helper :watchers
jplang's avatar
jplang committed
30
  helper :attachments
31

32
  def index
33 34 35 36 37 38
    case params[:format]
    when 'xml', 'json'
      @offset, @limit = api_offset_and_limit
    else
      @limit =  10
    end
39

40
    scope = @project ? @project.news.visible : News.visible
41

42
    @news_count = scope.count
jplang's avatar
jplang committed
43 44
    @news_pages = Paginator.new @news_count, @limit, params['page']
    @offset ||= @news_pages.offset
45 46 47 48
    @newss = scope.includes([:author, :project]).
                      order("#{News.table_name}.created_on DESC").
                      limit(@limit).
                      offset(@offset).
jplang's avatar
jplang committed
49
                      to_a
50
    respond_to do |format|
51 52 53 54
      format.html {
        @news = News.new # for adding news inline
        render :layout => false if request.xhr?
      }
55
      format.api
56 57 58
      format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
    end
  end
59

jplang's avatar
jplang committed
60
  def show
61 62
    @comments = @news.comments
    @comments.reverse! if User.current.wants_comments_in_reverse_order?
jplang's avatar
jplang committed
63 64
  end

65 66
  def new
    @news = News.new(:project => @project, :author => User.current)
67 68 69 70
  end

  def create
    @news = News.new(:project => @project, :author => User.current)
71
    @news.safe_attributes = params[:news]
jplang's avatar
jplang committed
72
    @news.save_attachments(params[:attachments])
jplang's avatar
jplang committed
73 74 75
    if @news.save
      render_attachment_warning_if_needed(@news)
      flash[:notice] = l(:notice_successful_create)
jplang's avatar
jplang committed
76
      redirect_to project_news_index_path(@project)
jplang's avatar
jplang committed
77 78
    else
      render :action => 'new'
79 80
    end
  end
81

jplang's avatar
jplang committed
82
  def edit
83
  end
84

85
  def update
86
    @news.safe_attributes = params[:news]
jplang's avatar
jplang committed
87
    @news.save_attachments(params[:attachments])
88
    if @news.save
jplang's avatar
jplang committed
89
      render_attachment_warning_if_needed(@news)
jplang's avatar
jplang committed
90
      flash[:notice] = l(:notice_successful_update)
jplang's avatar
jplang committed
91
      redirect_to news_path(@news)
92 93
    else
      render :action => 'edit'
jplang's avatar
jplang committed
94 95
    end
  end
96

jplang's avatar
jplang committed
97 98
  def destroy
    @news.destroy
jplang's avatar
jplang committed
99
    redirect_to project_news_index_path(@project)
100
  end
101

102
  private
103

104 105 106 107 108 109 110
  def find_optional_project
    return true unless params[:project_id]
    @project = Project.find(params[:project_id])
    authorize
  rescue ActiveRecord::RecordNotFound
    render_404
  end
jplang's avatar
jplang committed
111
end