Commit fcbf43047acf55d2c16319f6e1019f2a054a7d64
Committed by
Antonio Terceiro
1 parent
496e8b0c
Exists in
master
and in
29 other branches
Fixing performance issues on media-listing
* Also adding a named_scope for images (ActionItem1698)
Showing
6 changed files
with
27 additions
and
7 deletions
Show diff stats
app/controllers/my_profile/cms_controller.rb
... | ... | @@ -285,20 +285,20 @@ class CmsController < MyProfileController |
285 | 285 | def media_listing |
286 | 286 | if params[:image_folder_id] |
287 | 287 | folder = profile.articles.find(params[:image_folder_id]) if !params[:image_folder_id].blank? |
288 | - @images = (folder ? folder.children : UploadedFile.find(:all, :order => 'created_at desc', :conditions => ["profile_id = ? AND parent_id is NULL", profile ])).select { |c| c.image? } | |
288 | + @images = (folder ? folder.children : profile.top_level_articles).images | |
289 | 289 | elsif params[:document_folder_id] |
290 | 290 | folder = profile.articles.find(params[:document_folder_id]) if !params[:document_folder_id].blank? |
291 | - @documents = (folder ? folder.children : UploadedFile.find(:all, :order => 'created_at desc', :conditions => ["profile_id = ? AND parent_id is NULL", profile ])).select { |c| c.kind_of?(UploadedFile) && !c.image? } | |
291 | + @documents = (folder ? folder.children : profile.top_level_articles) | |
292 | 292 | else |
293 | - @documents = UploadedFile.find(:all, :order => 'created_at desc', :conditions => ["profile_id = ? AND parent_id is NULL", profile ]) | |
294 | - @images = @documents.select(&:image?) | |
293 | + @documents = profile.articles | |
294 | + @images = @documents.images | |
295 | 295 | @documents -= @images |
296 | 296 | end |
297 | 297 | |
298 | - @images = @images.paginate(:per_page => per_page, :page => params[:ipage]) if @images | |
299 | - @documents = @documents.paginate(:per_page => per_page, :page => params[:dpage]) if @documents | |
298 | + @images = @images.paginate(:per_page => per_page, :page => params[:ipage], :order => "updated_at desc") if @images | |
299 | + @documents = @documents.paginate(:per_page => per_page, :page => params[:dpage], :order => "updated_at desc", :conditions => {:is_image => false}) if @documents | |
300 | 300 | |
301 | - @folders = Folder.find(:all, :conditions => { :profile_id => profile }) | |
301 | + @folders = profile.folders | |
302 | 302 | @image_folders = @folders.select {|f| f.children.any? {|c| c.image?} } |
303 | 303 | @document_folders = @folders.select {|f| f.children.any? {|c| !c.image? && c.kind_of?(UploadedFile) } } |
304 | 304 | ... | ... |
app/models/article.rb
... | ... | @@ -238,6 +238,7 @@ class Article < ActiveRecord::Base |
238 | 238 | |
239 | 239 | named_scope :published, :conditions => { :published => true } |
240 | 240 | named_scope :folders, :conditions => { :type => ['Folder', 'Blog'] } |
241 | + named_scope :images, :conditions => { :is_image => true } | |
241 | 242 | |
242 | 243 | def display_unpublished_article_to?(user) |
243 | 244 | self.author == user || allow_view_private_content?(user) || user == self.profile || | ... | ... |
app/models/uploaded_file.rb
... | ... | @@ -16,6 +16,10 @@ class UploadedFile < Article |
16 | 16 | |
17 | 17 | validates_size_of :title, :maximum => 60, :if => (lambda { |file| !file.title.blank? }) |
18 | 18 | |
19 | + before_create do |uploaded_file| | |
20 | + uploaded_file.is_image = true if uploaded_file.image? | |
21 | + end | |
22 | + | |
19 | 23 | def thumbnail_path |
20 | 24 | self.image? ? self.full_filename(:thumb).gsub(File.join(RAILS_ROOT, 'public'), '') : nil |
21 | 25 | end | ... | ... |
... | ... | @@ -0,0 +1,13 @@ |
1 | +class AddIsImageToArticles < ActiveRecord::Migration | |
2 | + def self.up | |
3 | + add_column :articles, :is_image, :boolean, :default => false | |
4 | + add_column :article_versions, :is_image, :boolean, :default => false | |
5 | + | |
6 | + execute ActiveRecord::Base.sanitize_sql(["update articles set is_image = ? where articles.content_type like 'image/%'", true]) | |
7 | + end | |
8 | + | |
9 | + def self.down | |
10 | + remove_column :articles, :is_image | |
11 | + remove_column :article_versions, :is_image | |
12 | + end | |
13 | +end | ... | ... |
test/functional/cms_controller_test.rb
... | ... | @@ -1164,6 +1164,7 @@ class CmsControllerTest < Test::Unit::TestCase |
1164 | 1164 | |
1165 | 1165 | should 'display pagination links of documents' do |
1166 | 1166 | @controller.stubs(:per_page).returns(1) |
1167 | + profile.articles.destroy_all | |
1167 | 1168 | file = UploadedFile.create!(:profile => profile, :uploaded_data => fixture_file_upload('/files/feed.xml', 'text/xml')) |
1168 | 1169 | file2 = UploadedFile.create!(:profile => profile, :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')) |
1169 | 1170 | file2.created_at = 1.day.ago | ... | ... |
test/unit/uploaded_file_test.rb
... | ... | @@ -56,6 +56,7 @@ class UploadedFileTest < Test::Unit::TestCase |
56 | 56 | file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) |
57 | 57 | file.profile = profile |
58 | 58 | assert file.save |
59 | + assert file.is_image | |
59 | 60 | end |
60 | 61 | |
61 | 62 | should 'has attachment_fu validation options' do | ... | ... |