controller tweaks

This commit is contained in:
albert
2013-02-23 15:58:21 -05:00
parent 096f1be22b
commit 56dd8707fd
17 changed files with 43 additions and 28 deletions

View File

@@ -2,7 +2,7 @@ class ArtistVersionsController < ApplicationController
respond_to :html, :xml, :json respond_to :html, :xml, :json
def index def index
@artist_versions = ArtistVersion.search(params[:search]).order("id desc").paginate(params[:page]) @artist_versions = ArtistVersion.search(params[:search]).order("id desc").paginate(params[:page], :search_count => params[:search])
respond_with(@artist_versions) respond_with(@artist_versions)
end end
end end

View File

@@ -67,14 +67,14 @@ private
end end
def index_by_post def index_by_post
@posts = Post.commented_before(Time.now).tag_match(params[:tags]).paginate(params[:page], :limit => 5) @posts = Post.commented_before(Time.now).tag_match(params[:tags]).paginate(params[:page], :limit => 5, :search_count => params[:search])
respond_with(@posts) do |format| respond_with(@posts) do |format|
format.html {render :action => "index_by_post"} format.html {render :action => "index_by_post"}
end end
end end
def index_by_comment def index_by_comment
@comments = Comment.search(params[:search]).order("comments.id DESC").paginate(params[:page]) @comments = Comment.search(params[:search]).order("comments.id DESC").paginate(params[:page], :search_count => params[:search])
respond_with(@comments) do |format| respond_with(@comments) do |format|
format.html {render :action => "index_by_comment"} format.html {render :action => "index_by_comment"}
end end

View File

@@ -17,7 +17,7 @@ class ForumPostsController < ApplicationController
def index def index
@search = ForumPost.active.search(params[:search]) @search = ForumPost.active.search(params[:search])
@forum_posts = @search.order("forum_posts.id DESC").paginate(params[:page]) @forum_posts = @search.order("forum_posts.id DESC").paginate(params[:page], :search_count => params[:search])
respond_with(@forum_posts) respond_with(@forum_posts)
end end

View File

@@ -19,7 +19,7 @@ class ForumTopicsController < ApplicationController
def index def index
@search = ForumTopic.active.search(params[:search]) @search = ForumTopic.active.search(params[:search])
@forum_topics = @search.order("is_sticky DESC, updated_at DESC").paginate(params[:page]) @forum_topics = @search.order("is_sticky DESC, updated_at DESC").paginate(params[:page], :search_count => params[:search])
respond_with(@forum_topics) respond_with(@forum_topics)
end end

View File

@@ -63,8 +63,7 @@ private
end end
def index_by_note def index_by_note
@search = Note.search(params[:search]) @notes = Note.search(params[:search]).order("id desc").paginate(params[:page], :search_count => params[:search])
@notes = @search.order("id desc").paginate(params[:page])
respond_with(@notes) do |format| respond_with(@notes) do |format|
format.html {render :action => "index_by_note"} format.html {render :action => "index_by_note"}
end end

View File

@@ -1,6 +1,5 @@
class PoolVersionsController < ApplicationController class PoolVersionsController < ApplicationController
def index def index
@search = PoolVersion.search(params[:search]) @pool_versions = PoolVersion.search(params[:search]).order("updated_at desc").paginate(params[:page], :search_count => params[:search])
@pool_versions = @search.order("updated_at desc").paginate(params[:page])
end end
end end

View File

@@ -15,8 +15,7 @@ class PoolsController < ApplicationController
end end
def index def index
@search = Pool.active.search(params[:search]) @pools = Pool.active.search(params[:search]).order("updated_at desc").paginate(params[:page], :search_count => params[:search])
@pools = @search.order("updated_at desc").paginate(params[:page])
respond_with(@pools) respond_with(@pools)
end end

View File

@@ -2,7 +2,7 @@ class PostVersionsController < ApplicationController
respond_to :html, :xml, :json respond_to :html, :xml, :json
def index def index
@post_versions = PostVersion.search(params[:search]).order("updated_at desc").paginate(params[:page], :count => (params[:search].present? ? nil : 1_000_000)) @post_versions = PostVersion.search(params[:search]).order("updated_at desc").paginate(params[:page], :search_count => params[:search])
respond_with(@post_versions) respond_with(@post_versions)
end end

View File

@@ -8,7 +8,7 @@ class TagsController < ApplicationController
end end
def index def index
@tags = Tag.search(params[:search]).paginate(params[:page]) @tags = Tag.search(params[:search]).paginate(params[:page], :search_count => params[:search])
respond_with(@tags) respond_with(@tags)
end end

View File

@@ -15,8 +15,7 @@ class UsersController < ApplicationController
end end
def index def index
@search = User.search(params[:search]) @users = User.search(params[:search]).order("users.id desc").paginate(params[:page], :search_count => params[:search])
@users = @search.order("users.id desc").paginate(params[:page])
respond_with(@users) respond_with(@users)
end end

View File

@@ -2,8 +2,7 @@ class WikiPageVersionsController < ApplicationController
respond_to :json, :html, :xml respond_to :json, :html, :xml
def index def index
@search = WikiPageVersion.search(params[:search]) @wiki_page_versions = WikiPageVersion.search(params[:search]).order("id desc").paginate(params[:page], :search_count => params[:search])
@wiki_page_versions = @search.order("id desc").paginate(params[:page])
respond_with(@wiki_page_versions) respond_with(@wiki_page_versions)
end end

View File

@@ -15,8 +15,7 @@ class WikiPagesController < ApplicationController
end end
def index def index
@search = WikiPage.search(params[:search]) @wiki_pages = WikiPage.search(params[:search]).order("id desc").paginate(params[:page], :search_count => params[:search])
@wiki_pages = @search.order("id desc").paginate(params[:page])
respond_with(@wiki_pages) do |format| respond_with(@wiki_pages) do |format|
format.html do format.html do
if @wiki_pages.count == 1 if @wiki_pages.count == 1

View File

@@ -5,7 +5,7 @@ class NoteVersion < ActiveRecord::Base
def self.search(params) def self.search(params)
q = scoped q = scoped
return q if params.blank? params = {} if params.blank?
if params[:updater_id] if params[:updater_id]
q = q.where("updater_id = ?", params[:updater_id].to_i) q = q.where("updater_id = ?", params[:updater_id].to_i)

View File

@@ -22,7 +22,7 @@ class Pool < ActiveRecord::Base
def search(params) def search(params)
q = scoped q = scoped
return q if params.blank? params = {} if params.blank?
if params[:name_matches].present? if params[:name_matches].present?
params[:name_matches] = params[:name_matches].tr(" ", "_") params[:name_matches] = params[:name_matches].tr(" ", "_")
@@ -42,10 +42,10 @@ class Pool < ActiveRecord::Base
q = q.where("creator_id = ?", params[:creator_id].to_i) q = q.where("creator_id = ?", params[:creator_id].to_i)
end end
if params[:sort] == "updated_at" if params[:sort] == "name"
q = q.order("updated_at desc")
else
q = q.order("name") q = q.order("name")
else
q = q.order("updated_at desc")
end end
q q

View File

@@ -14,7 +14,7 @@ class PostVersion < ActiveRecord::Base
def search(params) def search(params)
q = scoped q = scoped
return q if params.blank? params = {} if params.blank?
if params[:updater_name].present? if params[:updater_name].present?
q = q.updater_name(params[:updater_name]) q = q.updater_name(params[:updater_name])

View File

@@ -137,7 +137,7 @@ module Danbooru
# After this many pages, the paginator will switch to sequential mode. # After this many pages, the paginator will switch to sequential mode.
def max_numbered_pages def max_numbered_pages
10 1_000
end end
# Max number of tag subscriptions per user # Max number of tag subscriptions per user

View File

@@ -69,12 +69,33 @@ module Danbooru
end end
def records_per_page def records_per_page
(@paginator_options.try(:[], :limit) || Danbooru.config.posts_per_page).to_i option_for(:limit).to_i
end
# When paginating large tables, we want to avoid doing an expensive count query
# when the result won't even be used. So when calling paginate you can pass in
# an optional :search_count key which points to the search params. If these params
# exist, then assume we're doing a search and don't override the default count
# behavior. Otherwise, just return some large number so the paginator skips the
# count.
def option_for(key)
case key
when :limit
@paginator_options.try(:[], :limit) || Danbooru.config.posts_per_page
when :count
if @paginator_options.has_key?(:search_count) && @paginator_options[:search_count].blank?
1_000_000
else
nil
end
end
end end
# taken from kaminari (https://github.com/amatsuda/kaminari) # taken from kaminari (https://github.com/amatsuda/kaminari)
def total_count def total_count
return @paginator_options[:count] if @paginator_options[:count] return option_for(:count) if option_for(:count)
c = except(:offset, :limit, :order) c = except(:offset, :limit, :order)
c = c.reorder(nil) c = c.reorder(nil)