From bd3af50e107200309ebdb60495264560129d102c Mon Sep 17 00:00:00 2001 From: albert Date: Sat, 2 Jul 2011 21:04:29 -0400 Subject: [PATCH] changes --- app/assets/stylesheets/application.css.scss | 6 +++- app/controllers/notes_controller.rb | 8 +++-- app/helpers/pagination_helper.rb | 36 +++++++++---------- app/logical/post_sets/note.rb | 10 ++++++ app/models/note.rb | 3 +- app/models/post.rb | 9 ++++- app/models/tag.rb | 1 + app/views/comments/search.html.erb | 6 ++-- app/views/layouts/default.html.erb | 2 +- app/views/notes/index_by_note.html.erb | 6 +++- app/views/notes/index_by_post.html.erb | 11 ++++++ app/views/notes/search.html.erb | 11 ++++++ .../posts/partials/index/_posts.html.erb | 4 +-- config/danbooru_default_config.rb | 2 +- .../paginator/active_record_extension.rb | 5 +++ lib/danbooru/paginator/error.rb | 6 ++++ 16 files changed, 94 insertions(+), 32 deletions(-) create mode 100644 app/logical/post_sets/note.rb create mode 100644 lib/danbooru/paginator/error.rb diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index e49cb7055..a1cfac4eb 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -194,7 +194,7 @@ menu { li { margin: 0; - padding: 0 1em 0 0; + padding: 0 0.25em; list-style-type: none; display: inline; } @@ -262,6 +262,10 @@ table tfoot { } table.striped { + p { + margin: 0; + } + tbody { tr:hover { background-color: #FFE; diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index ded7f8c31..55861c5af 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -3,6 +3,10 @@ class NotesController < ApplicationController before_filter :member_only, :except => [:index, :show] before_filter :pass_html_id, :only => [:create] + def search + @search = Note.search(params[:search]) + end + def index if params[:group_by] == "post" index_by_post @@ -52,7 +56,8 @@ private end def index_by_post - @posts = Post.tag_match(params[:tags]).noted_before(params[:before_date] || Time.now).paginate(params[:page]) + @post_set = PostSets::Note.new(params) + @posts = @post_set.posts respond_with(@posts) do |format| format.html {render :action => "index_by_post"} end @@ -65,5 +70,4 @@ private format.html {render :action => "index_by_note"} end end - end diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb index 758dd20b4..0e9202ab9 100644 --- a/app/helpers/pagination_helper.rb +++ b/app/helpers/pagination_helper.rb @@ -18,8 +18,8 @@ module PaginationHelper params[:page] =~ /[ab]/ || records.current_page > Danbooru.config.max_numbered_pages end - def numbered_paginator(records, &block) - if use_sequential_paginator?(records) + def numbered_paginator(records, switch_to_sequential = true) + if use_sequential_paginator?(records) && switch_to_sequential return sequential_paginator(records) end @@ -27,49 +27,49 @@ module PaginationHelper window = 3 if records.total_pages <= (window * 2) + 5 1.upto(records.total_pages) do |page| - html << numbered_paginator_item(page, records.current_page, &block) + html << numbered_paginator_item(page, records.current_page) end elsif records.current_page <= window + 2 1.upto(records.current_page + window) do |page| - html << numbered_paginator_item(page, records.current_page, &block) + html << numbered_paginator_item(page, records.current_page) end - html << numbered_paginator_item("...", records.current_page, &block) - html << numbered_paginator_final_item(records.total_pages, records.current_page, &block) + html << numbered_paginator_item("...", records.current_page) + html << numbered_paginator_final_item(records.total_pages, records.current_page) elsif records.current_page >= records.total_pages - (window + 1) - html << numbered_paginator_item(1, records.current_page, &block) - html << numbered_paginator_item("...", records.current_page, &block) + html << numbered_paginator_item(1, records.current_page) + html << numbered_paginator_item("...", records.current_page) (records.current_page - window).upto(records.total_pages) do |page| - html << numbered_paginator_item(page, records.current_page, &block) + html << numbered_paginator_item(page, records.current_page) end else - html << numbered_paginator_item(1, records.current_page, &block) - html << numbered_paginator_item("...", records.current_page, &block) + html << numbered_paginator_item(1, records.current_page) + html << numbered_paginator_item("...", records.current_page) (records.current_page - window).upto(records.current_page + window) do |page| - html << numbered_paginator_item(page, records.current_page, &block) + html << numbered_paginator_item(page, records.current_page) end - html << numbered_paginator_item("...", records.current_page, &block) - html << numbered_paginator_final_item(records.total_pages, records.current_page, &block) + html << numbered_paginator_item("...", records.current_page) + html << numbered_paginator_final_item(records.total_pages, records.current_page) end html << "" html.html_safe end - def numbered_paginator_final_item(total_pages, current_page, &block) + def numbered_paginator_final_item(total_pages, current_page) if total_pages <= Danbooru.config.max_numbered_pages - numbered_paginator_item(total_pages, current_page, &block) + numbered_paginator_item(total_pages, current_page) else "" end end - def numbered_paginator_item(page, current_page, &block) + def numbered_paginator_item(page, current_page) html = "
  • " if page == "..." html << "..." elsif page == current_page html << '' + page.to_s + '' else - html << capture(page, &block) + html << link_to(page, params.merge(:page => page)) end html << "
  • " html.html_safe diff --git a/app/logical/post_sets/note.rb b/app/logical/post_sets/note.rb new file mode 100644 index 000000000..c2217184c --- /dev/null +++ b/app/logical/post_sets/note.rb @@ -0,0 +1,10 @@ +module PostSets + class Note < Post + def initialize(params) + # don't call super because we don't want to repeat these queries + @tag_array = Tag.scan_query(params[:tags]) + @page = params[:page] + @posts = ::Post.tag_match(tag_string).has_notes.paginate(page) + end + end +end diff --git a/app/models/note.rb b/app/models/note.rb index 0bd6f3eb7..85ba99c35 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -14,7 +14,8 @@ class Note < ActiveRecord::Base attr_accessible :x, :y, :width, :height, :body, :updater_id, :updater_ip_addr, :is_active, :post_id, :html_id scope :active, where("is_active = TRUE") scope :body_matches, lambda {|query| where("text_index @@ plainto_tsquery(?)", query.scan(/\S+/).join(" & "))} - search_methods :body_matches + scope :post_tag_match, lambda {|query| joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)} + search_methods :body_matches, :post_tag_match def presenter @presenter ||= NotePresenter.new(self) diff --git a/app/models/post.rb b/app/models/post.rb index 97a929a63..31725a0ac 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -35,7 +35,7 @@ class Post < ActiveRecord::Base scope :deleted, where(["is_deleted = ?", true]) scope :visible, lambda {|user| Danbooru.config.can_user_see_post_conditions(user)} scope :commented_before, lambda {|date| where("last_commented_at < ?", date).order("last_commented_at DESC")} - scope :noted_before, lambda {|date| where("last_noted_at < ?", date).order("last_noted_at DESC")} + scope :has_notes, where("last_noted_at is not null") scope :for_user, lambda {|user_id| where(["uploader_string = ?", "uploader:#{user_id}"])} scope :available_for_moderation, lambda {where(["id NOT IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])} scope :hidden_from_moderation, lambda {where(["id IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])} @@ -861,6 +861,12 @@ class Post < ActiveRecord::Base end end + module NoteMethods + def last_noted_at_as_integer + last_noted_at.to_i + end + end + include FileMethods include ImageMethods include ApprovalMethods @@ -876,6 +882,7 @@ class Post < ActiveRecord::Base include ParentMethods include DeletionMethods include VersionMethods + include NoteMethods def reload(options = nil) super diff --git a/app/models/tag.rb b/app/models/tag.rb index c42564ede..f4423aa1c 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -293,6 +293,7 @@ class Tag < ActiveRecord::Base when "status" q[:status] = $2 + end else diff --git a/app/views/comments/search.html.erb b/app/views/comments/search.html.erb index 167e8a2dd..818b02998 100644 --- a/app/views/comments/search.html.erb +++ b/app/views/comments/search.html.erb @@ -3,9 +3,9 @@

    Search Comments

    <%= simple_form_for(@search) do |f| %> <%= hidden_field_tag "group_by", "comment" %> - <%= f.input :body_matches, :label => "Body" %> - <%= f.input :creator_name_equals, :label => "User" %> - <%= f.input :post_tag_match, :label => "Tags" %> + <%= f.input :body_matches, :label => "Body", :required => false %> + <%= f.input :creator_name_equals, :label => "User", :required => false %> + <%= f.input :post_tag_match, :label => "Tags", :required => false %> <%= f.button :submit, "Search" %> <% end %> diff --git a/app/views/layouts/default.html.erb b/app/views/layouts/default.html.erb index 0f12b7ec4..a8435c406 100644 --- a/app/views/layouts/default.html.erb +++ b/app/views/layouts/default.html.erb @@ -32,7 +32,7 @@ <% end %> <%= nav_link_to("Posts", posts_path) %> <%= nav_link_to("Comments", comments_path(:group_by => "post")) %> - <%= nav_link_to("Notes", notes_path) %> + <%= nav_link_to("Notes", notes_path(:group_by => "post")) %> <%= nav_link_to("Artists", artists_path(:order => "date")) %> <%= nav_link_to("Tags", tags_path(:order => "date")) %> <% if CurrentUser.is_moderator? %> diff --git a/app/views/notes/index_by_note.html.erb b/app/views/notes/index_by_note.html.erb index e606e94b9..840f055fd 100644 --- a/app/views/notes/index_by_note.html.erb +++ b/app/views/notes/index_by_note.html.erb @@ -1,6 +1,6 @@
    - +
    @@ -22,6 +22,10 @@ <% end %>
    Post
    + +
    + <%= sequential_paginator(@notes) %> +
    diff --git a/app/views/notes/index_by_post.html.erb b/app/views/notes/index_by_post.html.erb index e69de29bb..705ecc8b4 100644 --- a/app/views/notes/index_by_post.html.erb +++ b/app/views/notes/index_by_post.html.erb @@ -0,0 +1,11 @@ +
    +
    + <%= @post_set.presenter.post_previews_html(self) %> + +
    + <%= numbered_paginator(@posts, false) %> +
    +
    +
    + +<%= render "secondary_links" %> \ No newline at end of file diff --git a/app/views/notes/search.html.erb b/app/views/notes/search.html.erb index e69de29bb..ca9d1298f 100644 --- a/app/views/notes/search.html.erb +++ b/app/views/notes/search.html.erb @@ -0,0 +1,11 @@ +
    + +
    + +<%= render "secondary_links" %> diff --git a/app/views/posts/partials/index/_posts.html.erb b/app/views/posts/partials/index/_posts.html.erb index 8a9d83b90..3f7e18c63 100644 --- a/app/views/posts/partials/index/_posts.html.erb +++ b/app/views/posts/partials/index/_posts.html.erb @@ -5,7 +5,5 @@
    - <%= numbered_paginator(post_set.posts) do |page| %> - <%= link_to(page, posts_path(:page => page, :tags => post_set.tag_string)) %> - <% end %> + <%= numbered_paginator(post_set.posts) %>
    diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 6b8acd3f9..858e89679 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -111,7 +111,7 @@ module Danbooru def max_numbered_pages 2 end - + # Max number of tag subscriptions per user def max_tag_subscriptions 5 diff --git a/lib/danbooru/paginator/active_record_extension.rb b/lib/danbooru/paginator/active_record_extension.rb index e0954b313..1abaf8937 100644 --- a/lib/danbooru/paginator/active_record_extension.rb +++ b/lib/danbooru/paginator/active_record_extension.rb @@ -50,6 +50,11 @@ module Danbooru def paginate_numbered(page) page = [page.to_i, 1].max + + if page > Danbooru.config.max_numbered_pages + raise Error.new("You cannot go beyond page #{Danbooru.config.max_numbered_pages}. Please narrow your search terms.") + end + limit(records_per_page).offset((page - 1) * records_per_page).tap do |obj| obj.extend(NumberedCollectionExtension) obj.total_pages = (obj.total_count.to_f / records_per_page).ceil diff --git a/lib/danbooru/paginator/error.rb b/lib/danbooru/paginator/error.rb new file mode 100644 index 000000000..4acfa49fc --- /dev/null +++ b/lib/danbooru/paginator/error.rb @@ -0,0 +1,6 @@ +module Danbooru + module Paginator + class Error < Exception + end + end +end