Commit a482d2bc23b13713075ac410aa400e4da4d05376
1 parent
8791c34e
Exists in
master
and in
27 other branches
Refactoring view_page method of content_viewer_controller
Showing
1 changed file
with
138 additions
and
75 deletions
Show diff stats
app/controllers/public/content_viewer_controller.rb
| ... | ... | @@ -8,49 +8,31 @@ class ContentViewerController < ApplicationController |
| 8 | 8 | helper TagsHelper |
| 9 | 9 | |
| 10 | 10 | def view_page |
| 11 | - path = params[:page] | |
| 12 | - path = path.join('/') if path.kind_of?(Array) | |
| 13 | - path = "#{path}.#{params[:format]}" if params[:format] | |
| 11 | + path = get_path(params[:page], params[:format]) | |
| 12 | + | |
| 14 | 13 | @version = params[:version].to_i |
| 15 | 14 | |
| 16 | 15 | if path.blank? |
| 17 | - @page = profile.home_page | |
| 18 | - if @page.nil? | |
| 19 | - redirect_to :controller => 'profile', :action => 'index', :profile => profile.identifier | |
| 20 | - return | |
| 21 | - end | |
| 16 | + @page = profile.home_page | |
| 17 | + return if redirected_to_profile_index | |
| 22 | 18 | else |
| 23 | 19 | @page = profile.articles.find_by_path(path) |
| 24 | - unless @page | |
| 25 | - page_from_old_path = profile.articles.find_by_old_path(path) | |
| 26 | - if page_from_old_path | |
| 27 | - redirect_to profile.url.merge(:page => page_from_old_path.explode_path) | |
| 28 | - return | |
| 29 | - end | |
| 30 | - end | |
| 20 | + return if redirecetd_page_from_old_path(path) | |
| 31 | 21 | end |
| 32 | 22 | |
| 33 | 23 | return unless allow_access_to_page(path) |
| 34 | 24 | |
| 35 | 25 | if @version > 0 |
| 36 | 26 | return render_access_denied unless @page.display_versions? |
| 37 | - @versioned_article = @page.versions.find_by_version(@version) | |
| 38 | - if @versioned_article && @page.versions.latest.version != @versioned_article.version | |
| 39 | - render :template => 'content_viewer/versioned_article.html.erb' | |
| 40 | - return | |
| 41 | - end | |
| 27 | + return if rendered_versioned_article | |
| 42 | 28 | end |
| 43 | 29 | |
| 44 | 30 | redirect_to_translation and return if @page.profile.redirect_l10n |
| 45 | 31 | |
| 46 | - if request.post? | |
| 47 | - if @page.forum? && @page.has_terms_of_use && params[:terms_accepted] == "true" | |
| 48 | - @page.add_agreed_user(user) | |
| 49 | - end | |
| 50 | - elsif !@page.parent.nil? && @page.parent.forum? | |
| 51 | - unless @page.parent.agrees_with_terms?(user) | |
| 52 | - redirect_to @page.parent.url | |
| 53 | - end | |
| 32 | + if request.post? && @page.forum? | |
| 33 | + process_forum_terms_of_use(user, params[:terms_accepted]) | |
| 34 | + elsif is_a_forum_topic?(@page) | |
| 35 | + redirect_to @page.parent.url unless @page.parent.agrees_with_terms?(user) | |
| 54 | 36 | end |
| 55 | 37 | |
| 56 | 38 | # At this point the page will be showed |
| ... | ... | @@ -58,64 +40,22 @@ class ContentViewerController < ApplicationController |
| 58 | 40 | |
| 59 | 41 | @page = FilePresenter.for @page |
| 60 | 42 | |
| 61 | - if @page.download? params[:view] | |
| 62 | - headers['Content-Type'] = @page.mime_type | |
| 63 | - headers.merge! @page.download_headers | |
| 64 | - data = @page.data | |
| 65 | - | |
| 66 | - # TODO test the condition | |
| 67 | - if data.nil? | |
| 68 | - raise "No data for file" | |
| 69 | - end | |
| 70 | - | |
| 71 | - render :text => data, :layout => false | |
| 72 | - return | |
| 73 | - end | |
| 43 | + return if rendered_file_download(params[:view]) | |
| 74 | 44 | |
| 75 | 45 | @form_div = params[:form] |
| 76 | 46 | |
| 77 | 47 | #FIXME see a better way to do this. It's not need to pass this variable anymore |
| 78 | 48 | @comment = Comment.new |
| 79 | 49 | |
| 80 | - if @page.has_posts? | |
| 81 | - posts = if params[:year] and params[:month] | |
| 82 | - filter_date = DateTime.parse("#{params[:year]}-#{params[:month]}-01") | |
| 83 | - @page.posts.by_range(filter_date..filter_date.at_end_of_month) | |
| 84 | - else | |
| 85 | - @page.posts | |
| 86 | - end | |
| 87 | - | |
| 88 | - #FIXME Need to run this before the pagination because this version of | |
| 89 | - # will_paginate returns a will_paginate collection instead of a | |
| 90 | - # relation. | |
| 91 | - blog_with_translation = @page.blog? && @page.display_posts_in_current_language? | |
| 92 | - posts = posts.native_translations if blog_with_translation | |
| 93 | - | |
| 94 | - @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))).to_a | |
| 95 | - | |
| 96 | - if blog_with_translation | |
| 97 | - @posts.replace @posts.map{ |p| p.get_translation_to(FastGettext.locale) }.compact | |
| 98 | - end | |
| 99 | - end | |
| 50 | + process_page_posts(params) | |
| 100 | 51 | |
| 101 | 52 | if @page.folder? && @page.gallery? |
| 102 | - @images = @page.images.select{ |a| a.display_to? user } | |
| 103 | - @images = @images.paginate(:per_page => per_page, :page => params[:npage]) unless params[:slideshow] | |
| 53 | + @images = get_images(@page, params[:npage], params[:slideshow]) | |
| 104 | 54 | end |
| 105 | 55 | |
| 106 | - @unfollow_form = params[:unfollow] && params[:unfollow] == 'true' | |
| 107 | - if params[:unfollow] && params[:unfollow] == 'commit' && request.post? | |
| 108 | - @page.followers -= [params[:email]] | |
| 109 | - if @page.save | |
| 110 | - session[:notice] = _("Notification of new comments to '%s' was successfully canceled") % params[:email] | |
| 111 | - end | |
| 112 | - end | |
| 56 | + process_page_followers(params) | |
| 113 | 57 | |
| 114 | - @comments = @page.comments.without_spam | |
| 115 | - @comments = @plugins.filter(:unavailable_comments, @comments) | |
| 116 | - @comments_count = @comments.count | |
| 117 | - @comments = @comments.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] ) | |
| 118 | - @comment_order = params[:comment_order].nil? ? 'oldest' : params[:comment_order] | |
| 58 | + process_comments(params) | |
| 119 | 59 | |
| 120 | 60 | if request.xhr? and params[:comment_order] |
| 121 | 61 | if @comment_order == 'newest' |
| ... | ... | @@ -203,4 +143,127 @@ class ContentViewerController < ApplicationController |
| 203 | 143 | user_agent.match(/crawler/) || |
| 204 | 144 | user_agent.match(/\(.*https?:\/\/.*\)/) |
| 205 | 145 | end |
| 146 | + | |
| 147 | + def get_path(page, format = nil) | |
| 148 | + path = page | |
| 149 | + path = path.join('/') if path.kind_of?(Array) | |
| 150 | + path = "#{path}.#{format}" if format | |
| 151 | + | |
| 152 | + return path | |
| 153 | + end | |
| 154 | + | |
| 155 | + def redirected_to_profile_index | |
| 156 | + if @page.nil? | |
| 157 | + redirect_to :controller => 'profile', :action => 'index', :profile => profile.identifier | |
| 158 | + return true | |
| 159 | + end | |
| 160 | + | |
| 161 | + return false | |
| 162 | + end | |
| 163 | + | |
| 164 | + def redirecetd_page_from_old_path(path) | |
| 165 | + unless @page | |
| 166 | + page_from_old_path = profile.articles.find_by_old_path(path) | |
| 167 | + if page_from_old_path | |
| 168 | + redirect_to profile.url.merge(:page => page_from_old_path.explode_path) | |
| 169 | + return true | |
| 170 | + end | |
| 171 | + end | |
| 172 | + | |
| 173 | + return false | |
| 174 | + end | |
| 175 | + | |
| 176 | + def process_forum_terms_of_use(user, terms_accepted = nil) | |
| 177 | + if @page.forum? && @page.has_terms_of_use && terms_accepted == "true" | |
| 178 | + @page.add_agreed_user(user) | |
| 179 | + end | |
| 180 | + end | |
| 181 | + | |
| 182 | + def is_a_forum_topic? (page) | |
| 183 | + return (!@page.parent.nil? && @page.parent.forum?) | |
| 184 | + end | |
| 185 | + | |
| 186 | + def rendered_versioned_article | |
| 187 | + @versioned_article = @page.versions.find_by_version(@version) | |
| 188 | + if @versioned_article && @page.versions.latest.version != @versioned_article.version | |
| 189 | + render :template => 'content_viewer/versioned_article.html.erb' | |
| 190 | + return true | |
| 191 | + end | |
| 192 | + | |
| 193 | + return false | |
| 194 | + end | |
| 195 | + | |
| 196 | + def rendered_file_download(view = nil) | |
| 197 | + if @page.download? view | |
| 198 | + headers['Content-Type'] = @page.mime_type | |
| 199 | + headers.merge! @page.download_headers | |
| 200 | + data = @page.data | |
| 201 | + | |
| 202 | + # TODO test the condition | |
| 203 | + if data.nil? | |
| 204 | + raise "No data for file" | |
| 205 | + end | |
| 206 | + | |
| 207 | + render :text => data, :layout => false | |
| 208 | + return true | |
| 209 | + end | |
| 210 | + | |
| 211 | + return false | |
| 212 | + end | |
| 213 | + | |
| 214 | + def process_page_posts(params) | |
| 215 | + if @page.has_posts? | |
| 216 | + posts = get_posts(params[:year], params[:month]) | |
| 217 | + | |
| 218 | + #FIXME Need to run this before the pagination because this version of | |
| 219 | + # will_paginate returns a will_paginate collection instead of a | |
| 220 | + # relation. | |
| 221 | + posts = posts.native_translations if blog_with_translation?(@page) | |
| 222 | + | |
| 223 | + @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))).to_a | |
| 224 | + | |
| 225 | + if blog_with_translation?(@page) | |
| 226 | + @posts.replace @posts.map{ |p| p.get_translation_to(FastGettext.locale) }.compact | |
| 227 | + end | |
| 228 | + end | |
| 229 | + end | |
| 230 | + | |
| 231 | + def get_posts(year = nil, month = nil) | |
| 232 | + if year && month | |
| 233 | + filter_date = DateTime.parse("#{year}-#{month}-01") | |
| 234 | + return @page.posts.by_range(filter_date..filter_date.at_end_of_month) | |
| 235 | + else | |
| 236 | + return @page.posts | |
| 237 | + end | |
| 238 | + end | |
| 239 | + | |
| 240 | + def blog_with_translation?(page) | |
| 241 | + return (page.blog? && page.display_posts_in_current_language?) | |
| 242 | + end | |
| 243 | + | |
| 244 | + def get_images(page, npage, slideshow) | |
| 245 | + images = page.images.select{ |a| a.display_to? user } | |
| 246 | + images = images.paginate(:per_page => per_page, :page => npage) unless slideshow | |
| 247 | + | |
| 248 | + return images | |
| 249 | + end | |
| 250 | + | |
| 251 | + def process_page_followers(params) | |
| 252 | + @unfollow_form = params[:unfollow] == 'true' | |
| 253 | + if params[:unfollow] == 'commit' && request.post? | |
| 254 | + @page.followers -= [params[:email]] | |
| 255 | + if @page.save | |
| 256 | + session[:notice] = _("Notification of new comments to '%s' was successfully canceled") % params[:email] | |
| 257 | + end | |
| 258 | + end | |
| 259 | + end | |
| 260 | + | |
| 261 | + def process_comments(params) | |
| 262 | + @comments = @page.comments.without_spam | |
| 263 | + @comments = @plugins.filter(:unavailable_comments, @comments) | |
| 264 | + @comments_count = @comments.count | |
| 265 | + @comments = @comments.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] ) | |
| 266 | + @comment_order = params[:comment_order].nil? ? 'oldest' : params[:comment_order] | |
| 267 | + end | |
| 268 | + | |
| 206 | 269 | end | ... | ... |