]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entry_controller.rb
Optimise data loading for browsing ways.
[rails.git] / app / controllers / diary_entry_controller.rb
1 class DiaryEntryController < ApplicationController
2   layout 'site', :except => :rss
3
4   before_filter :authorize_web
5   before_filter :require_user, :only => [:new, :edit]
6   before_filter :check_database_readable
7   before_filter :check_database_writable, :only => [:new, :edit]
8
9   def new
10     @title = 'New diary entry'
11
12     if params[:diary_entry]     
13       @diary_entry = DiaryEntry.new(params[:diary_entry])
14       @diary_entry.user = @user
15
16       if @diary_entry.save 
17         redirect_to :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name 
18       else
19         render :action => 'edit'
20       end
21     else
22       render :action => 'edit'
23     end
24   end
25
26   def edit
27     @title= 'Edit diary entry'
28     @diary_entry = DiaryEntry.find(params[:id])
29
30     if @user != @diary_entry.user
31       redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
32     elsif params[:diary_entry]
33       @diary_entry.title = params[:diary_entry][:title]
34       @diary_entry.body = params[:diary_entry][:body]
35       @diary_entry.latitude = params[:diary_entry][:latitude]
36       @diary_entry.longitude = params[:diary_entry][:longitude]
37
38       if @diary_entry.save
39         redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
40       end
41     end
42   rescue ActiveRecord::RecordNotFound
43     render :action => "no_such_entry", :status => :not_found
44   end
45
46   def comment
47     @entry = DiaryEntry.find(params[:id])
48     @diary_comment = @entry.diary_comments.build(params[:diary_comment])
49     @diary_comment.user = @user
50     if @diary_comment.save
51       Notifier::deliver_diary_comment_notification(@diary_comment)
52       redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
53     else
54       render :action => 'view'
55     end
56   end
57
58   def list
59     if params[:display_name]
60       @this_user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
61
62       if @this_user
63         @title = @this_user.display_name + "'s diary"
64         @entry_pages, @entries = paginate(:diary_entries,
65                                           :conditions => ['user_id = ?', @this_user.id],
66                                           :order => 'created_at DESC',
67                                           :per_page => 20)
68       else
69         @not_found_user = params[:display_name]
70
71         render :action => 'no_such_user', :status => :not_found
72       end
73     else
74       @title = "Users' diaries"
75       @entry_pages, @entries = paginate(:diary_entries, :include => :user,
76                                         :conditions => ["users.visible = ?", true],
77                                         :order => 'created_at DESC',
78                                         :per_page => 20)
79     end
80   end
81
82   def rss
83     if params[:display_name]
84       user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
85
86       if user
87         @entries = DiaryEntry.find(:all, :conditions => ['user_id = ?', user.id], :order => 'created_at DESC', :limit => 20)
88         @title = "OpenStreetMap diary entries for #{user.display_name}"
89         @description = "Recent OpenStreetmap diary entries from #{user.display_name}"
90         @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
91
92         render :content_type => Mime::RSS
93       else
94         render :nothing => true, :status => :not_found
95       end
96     else
97       @entries = DiaryEntry.find(:all, :include => :user,
98                                  :conditions => ["users.visible = ?", true],
99                                  :order => 'created_at DESC', :limit => 20)
100       @title = "OpenStreetMap diary entries"
101       @description = "Recent diary entries from users of OpenStreetMap"
102       @link = "http://#{SERVER_URL}/diary"
103
104       render :content_type => Mime::RSS
105     end
106   end
107
108   def view
109     user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
110
111     if user
112       @entry = DiaryEntry.find(:first, :conditions => ['user_id = ? AND id = ?', user.id, params[:id]])
113       @title = "Users' diaries | #{params[:display_name]}"
114     else
115       @not_found_user = params[:display_name]
116
117       render :action => 'no_such_user', :status => :not_found
118     end
119   end
120 end