To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / news_controller.rb @ 443:350acce374a2

History | View | Annotate | Download (3.32 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  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

    
18
class NewsController < ApplicationController
19
  default_search_scope :news
20
  model_object News
21
  before_filter :find_model_object, :except => [:new, :create, :index]
22
  before_filter :find_project_from_association, :except => [:new, :create, :index]
23
  before_filter :find_project, :only => [:new, :create]
24
  before_filter :authorize, :except => [:index]
25
  before_filter :find_optional_project, :only => :index
26
  accept_key_auth :index
27
  
28
  helper :watchers
29
  
30
  def index
31
    case params[:format]
32
    when 'xml', 'json'
33
      @offset, @limit = api_offset_and_limit
34
    else
35
      @limit =  10
36
    end
37
    
38
    scope = @project ? @project.news.visible : News.visible
39
    
40
    @news_count = scope.count
41
    @news_pages = Paginator.new self, @news_count, @limit, params['page']
42
    @offset ||= @news_pages.current.offset
43
    @newss = scope.all(:include => [:author, :project],
44
                                       :order => "#{News.table_name}.created_on DESC",
45
                                       :offset => @offset,
46
                                       :limit => @limit)
47
    
48
    respond_to do |format|
49
      format.html { render :layout => false if request.xhr? }
50
      format.api
51
      format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
52
    end
53
  end
54
  
55
  def show
56
    @comments = @news.comments
57
    @comments.reverse! if User.current.wants_comments_in_reverse_order?
58
  end
59

    
60
  def new
61
    @news = News.new(:project => @project, :author => User.current)
62
  end
63

    
64
  def create
65
    @news = News.new(:project => @project, :author => User.current)
66
    if request.post?
67
      @news.attributes = params[:news]
68
      if @news.save
69
        flash[:notice] = l(:notice_successful_create)
70
        redirect_to :controller => 'news', :action => 'index', :project_id => @project
71
      else
72
        render :action => 'new'
73
      end
74
    end
75
  end
76

    
77
  def edit
78
  end
79
  
80
  def update
81
    if request.put? and @news.update_attributes(params[:news])
82
      flash[:notice] = l(:notice_successful_update)
83
      redirect_to :action => 'show', :id => @news
84
    else
85
      render :action => 'edit'
86
    end
87
  end
88

    
89
  def destroy
90
    @news.destroy
91
    redirect_to :action => 'index', :project_id => @project
92
  end
93
  
94
private
95
  def find_project
96
    @project = Project.find(params[:project_id])
97
  rescue ActiveRecord::RecordNotFound
98
    render_404
99
  end
100
  
101
  def find_optional_project
102
    return true unless params[:project_id]
103
    @project = Project.find(params[:project_id])
104
    authorize
105
  rescue ActiveRecord::RecordNotFound
106
    render_404
107
  end
108
end