annotate app/controllers/news_controller.rb @ 904:0a8317a50fa0 redmine-1.1

Close obsolete branch redmine-1.1
author Chris Cannam
date Fri, 14 Jan 2011 12:53:21 +0000
parents af80e5618e9b
children cbce1fd3b1b7
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class NewsController < ApplicationController
Chris@0 19 default_search_scope :news
Chris@0 20 model_object News
chris@37 21 before_filter :find_model_object, :except => [:new, :create, :index]
chris@37 22 before_filter :find_project_from_association, :except => [:new, :create, :index]
chris@37 23 before_filter :find_project, :only => [:new, :create]
chris@37 24 before_filter :authorize, :except => [:index]
Chris@0 25 before_filter :find_optional_project, :only => :index
Chris@0 26 accept_key_auth :index
Chris@0 27
Chris@0 28 def index
Chris@117 29 case params[:format]
Chris@117 30 when 'xml', 'json'
Chris@117 31 @offset, @limit = api_offset_and_limit
Chris@117 32 else
Chris@117 33 @limit = 10
Chris@117 34 end
Chris@117 35
Chris@117 36 scope = @project ? @project.news.visible : News.visible
Chris@117 37
Chris@117 38 @news_count = scope.count
Chris@117 39 @news_pages = Paginator.new self, @news_count, @limit, params['page']
Chris@117 40 @offset ||= @news_pages.current.offset
Chris@117 41 @newss = scope.all(:include => [:author, :project],
Chris@117 42 :order => "#{News.table_name}.created_on DESC",
Chris@117 43 :offset => @offset,
Chris@117 44 :limit => @limit)
Chris@117 45
Chris@0 46 respond_to do |format|
Chris@0 47 format.html { render :layout => false if request.xhr? }
Chris@117 48 format.api
Chris@0 49 format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
Chris@0 50 end
Chris@0 51 end
Chris@0 52
Chris@0 53 def show
Chris@0 54 @comments = @news.comments
Chris@0 55 @comments.reverse! if User.current.wants_comments_in_reverse_order?
Chris@0 56 end
Chris@0 57
Chris@0 58 def new
Chris@0 59 @news = News.new(:project => @project, :author => User.current)
chris@22 60 end
chris@22 61
chris@22 62 def create
chris@22 63 @news = News.new(:project => @project, :author => User.current)
Chris@0 64 if request.post?
Chris@0 65 @news.attributes = params[:news]
Chris@0 66 if @news.save
Chris@0 67 flash[:notice] = l(:notice_successful_create)
Chris@0 68 redirect_to :controller => 'news', :action => 'index', :project_id => @project
chris@22 69 else
chris@22 70 render :action => 'new'
Chris@0 71 end
Chris@0 72 end
Chris@0 73 end
chris@22 74
chris@22 75 def edit
chris@22 76 end
Chris@0 77
chris@22 78 def update
chris@22 79 if request.put? and @news.update_attributes(params[:news])
Chris@0 80 flash[:notice] = l(:notice_successful_update)
Chris@0 81 redirect_to :action => 'show', :id => @news
chris@22 82 else
chris@22 83 render :action => 'edit'
Chris@0 84 end
Chris@0 85 end
Chris@0 86
Chris@0 87 def destroy
Chris@0 88 @news.destroy
Chris@0 89 redirect_to :action => 'index', :project_id => @project
Chris@0 90 end
Chris@0 91
Chris@0 92 private
Chris@0 93 def find_project
Chris@0 94 @project = Project.find(params[:project_id])
Chris@0 95 rescue ActiveRecord::RecordNotFound
Chris@0 96 render_404
Chris@0 97 end
Chris@0 98
Chris@0 99 def find_optional_project
Chris@0 100 return true unless params[:project_id]
Chris@0 101 @project = Project.find(params[:project_id])
Chris@0 102 authorize
Chris@0 103 rescue ActiveRecord::RecordNotFound
Chris@0 104 render_404
Chris@0 105 end
Chris@0 106 end