annotate app/controllers/news_controller.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children 40f7cfd4df19
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@0 21 before_filter :find_model_object, :except => [:new, :index, :preview]
Chris@0 22 before_filter :find_project_from_association, :except => [:new, :index, :preview]
Chris@0 23 before_filter :find_project, :only => [:new, :preview]
Chris@0 24 before_filter :authorize, :except => [:index, :preview]
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@0 29 @news_pages, @newss = paginate :news,
Chris@0 30 :per_page => 10,
Chris@0 31 :conditions => Project.allowed_to_condition(User.current, :view_news, :project => @project),
Chris@0 32 :include => [:author, :project],
Chris@0 33 :order => "#{News.table_name}.created_on DESC"
Chris@0 34 respond_to do |format|
Chris@0 35 format.html { render :layout => false if request.xhr? }
Chris@0 36 format.xml { render :xml => @newss.to_xml }
Chris@0 37 format.json { render :json => @newss.to_json }
Chris@0 38 format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
Chris@0 39 end
Chris@0 40 end
Chris@0 41
Chris@0 42 def show
Chris@0 43 @comments = @news.comments
Chris@0 44 @comments.reverse! if User.current.wants_comments_in_reverse_order?
Chris@0 45 end
Chris@0 46
Chris@0 47 def new
Chris@0 48 @news = News.new(:project => @project, :author => User.current)
Chris@0 49 if request.post?
Chris@0 50 @news.attributes = params[:news]
Chris@0 51 if @news.save
Chris@0 52 flash[:notice] = l(:notice_successful_create)
Chris@0 53 redirect_to :controller => 'news', :action => 'index', :project_id => @project
Chris@0 54 end
Chris@0 55 end
Chris@0 56 end
Chris@0 57
Chris@0 58 def edit
Chris@0 59 if request.post? and @news.update_attributes(params[:news])
Chris@0 60 flash[:notice] = l(:notice_successful_update)
Chris@0 61 redirect_to :action => 'show', :id => @news
Chris@0 62 end
Chris@0 63 end
Chris@0 64
Chris@0 65 def add_comment
Chris@0 66 @comment = Comment.new(params[:comment])
Chris@0 67 @comment.author = User.current
Chris@0 68 if @news.comments << @comment
Chris@0 69 flash[:notice] = l(:label_comment_added)
Chris@0 70 redirect_to :action => 'show', :id => @news
Chris@0 71 else
Chris@0 72 show
Chris@0 73 render :action => 'show'
Chris@0 74 end
Chris@0 75 end
Chris@0 76
Chris@0 77 def destroy_comment
Chris@0 78 @news.comments.find(params[:comment_id]).destroy
Chris@0 79 redirect_to :action => 'show', :id => @news
Chris@0 80 end
Chris@0 81
Chris@0 82 def destroy
Chris@0 83 @news.destroy
Chris@0 84 redirect_to :action => 'index', :project_id => @project
Chris@0 85 end
Chris@0 86
Chris@0 87 def preview
Chris@0 88 @text = (params[:news] ? params[:news][:description] : nil)
Chris@0 89 render :partial => 'common/preview'
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