Chris@441: # Redmine - project management software Chris@441: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class NewsController < ApplicationController Chris@0: default_search_scope :news Chris@0: model_object News chris@37: before_filter :find_model_object, :except => [:new, :create, :index] chris@37: before_filter :find_project_from_association, :except => [:new, :create, :index] chris@37: before_filter :find_project, :only => [:new, :create] chris@37: before_filter :authorize, :except => [:index] Chris@0: before_filter :find_optional_project, :only => :index Chris@507: accept_rss_auth :index Chris@507: accept_api_auth :index Chris@909: Chris@441: helper :watchers Chris@909: Chris@0: def index Chris@119: case params[:format] Chris@119: when 'xml', 'json' Chris@119: @offset, @limit = api_offset_and_limit Chris@119: else Chris@119: @limit = 10 Chris@119: end Chris@909: Chris@119: scope = @project ? @project.news.visible : News.visible Chris@909: Chris@119: @news_count = scope.count Chris@119: @news_pages = Paginator.new self, @news_count, @limit, params['page'] Chris@119: @offset ||= @news_pages.current.offset Chris@119: @newss = scope.all(:include => [:author, :project], Chris@119: :order => "#{News.table_name}.created_on DESC", Chris@119: :offset => @offset, Chris@119: :limit => @limit) Chris@909: Chris@0: respond_to do |format| Chris@909: format.html { Chris@909: @news = News.new # for adding news inline Chris@909: render :layout => false if request.xhr? Chris@909: } Chris@119: format.api Chris@0: format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") } Chris@0: end Chris@0: end Chris@909: Chris@0: def show Chris@0: @comments = @news.comments Chris@0: @comments.reverse! if User.current.wants_comments_in_reverse_order? Chris@0: end Chris@0: Chris@0: def new Chris@0: @news = News.new(:project => @project, :author => User.current) chris@22: end chris@22: chris@22: def create chris@22: @news = News.new(:project => @project, :author => User.current) Chris@929: @news.safe_attributes = params[:news] Chris@0: if request.post? Chris@0: if @news.save Chris@0: flash[:notice] = l(:notice_successful_create) Chris@0: redirect_to :controller => 'news', :action => 'index', :project_id => @project chris@22: else chris@22: render :action => 'new' Chris@0: end Chris@0: end Chris@0: end chris@22: chris@22: def edit chris@22: end Chris@909: chris@22: def update Chris@929: @news.safe_attributes = params[:news] Chris@929: if request.put? and @news.save Chris@0: flash[:notice] = l(:notice_successful_update) Chris@0: redirect_to :action => 'show', :id => @news chris@22: else chris@22: render :action => 'edit' Chris@0: end Chris@0: end Chris@0: Chris@0: def destroy Chris@0: @news.destroy Chris@0: redirect_to :action => 'index', :project_id => @project Chris@0: end Chris@909: Chris@0: private Chris@0: def find_project Chris@0: @project = Project.find(params[:project_id]) Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@909: Chris@0: def find_optional_project Chris@0: return true unless params[:project_id] Chris@0: @project = Project.find(params[:project_id]) Chris@0: authorize Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@0: end