From 27d602542598e0316166f01801aff06ba74b2e52 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 29 Apr 2017 11:38:40 -0500 Subject: [PATCH 1/4] dmails: convert search form to simple form. --- app/views/dmails/search.html.erb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/views/dmails/search.html.erb b/app/views/dmails/search.html.erb index 9e3aa21d1..9e18c44fa 100644 --- a/app/views/dmails/search.html.erb +++ b/app/views/dmails/search.html.erb @@ -1,13 +1,13 @@
From d1216f260e0fd5048e2affefcd7591759873a5c0 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 29 Apr 2017 11:39:18 -0500 Subject: [PATCH 2/4] dmails: add search[title_matches] param. --- app/models/dmail.rb | 9 +++++++++ app/views/dmails/search.html.erb | 1 + 2 files changed, 10 insertions(+) diff --git a/app/models/dmail.rb b/app/models/dmail.rb index 977a63e18..688e50c69 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -102,6 +102,11 @@ class Dmail < ActiveRecord::Base where("is_deleted = ?", true) end + def title_matches(query) + query = "*#{query}*" unless query =~ /\*/ + where("lower(dmails.title) LIKE ?", query.to_escaped_for_sql_like) + end + def search_message(query) if query =~ /\*/ && CurrentUser.user.is_builder? escaped_query = query.to_escaped_for_sql_like @@ -131,6 +136,10 @@ class Dmail < ActiveRecord::Base q = where("true") return q if params.blank? + if params[:title_matches].present? + q = q.title_matches(params[:title_matches]) + end + if params[:message_matches].present? q = q.search_message(params[:message_matches]) end diff --git a/app/views/dmails/search.html.erb b/app/views/dmails/search.html.erb index 9e18c44fa..23c2700c0 100644 --- a/app/views/dmails/search.html.erb +++ b/app/views/dmails/search.html.erb @@ -4,6 +4,7 @@ <%= simple_form_for(:search, url: dmails_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> <%= f.hidden_field :folder, value: params[:folder] %> + <%= f.input :title_matches, label: "Title", hint: "Use * for wildcard", input_html: { value: params[:search][:title_matches] } %> <%= f.input :message_matches, label: "Message", hint: "Use * for wildcard", input_html: { value: params[:search][:messages_matches] } %> <%= f.input :to_name, label: "To", input_html: { value: params[:search][:to_name] } %> <%= f.input :from_name, label: "From", input_html: { value: params[:search][:from_name] } %> From 0652b907a9cac71b112ae7ff56e712574b71ea9e Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 29 Apr 2017 11:43:02 -0500 Subject: [PATCH 3/4] dmails: inline search form on /dmails page. --- app/controllers/dmails_controller.rb | 3 --- app/views/dmails/_search.html.erb | 8 ++++++++ app/views/dmails/_secondary_links.html.erb | 1 - app/views/dmails/index.html.erb | 2 ++ app/views/dmails/search.html.erb | 20 -------------------- 5 files changed, 10 insertions(+), 24 deletions(-) create mode 100644 app/views/dmails/_search.html.erb delete mode 100644 app/views/dmails/search.html.erb diff --git a/app/controllers/dmails_controller.rb b/app/controllers/dmails_controller.rb index 8c01895e3..3a27011af 100644 --- a/app/controllers/dmails_controller.rb +++ b/app/controllers/dmails_controller.rb @@ -27,9 +27,6 @@ class DmailsController < ApplicationController end end - def search - end - def show @dmail = Dmail.find(params[:id]) check_privilege(@dmail) diff --git a/app/views/dmails/_search.html.erb b/app/views/dmails/_search.html.erb new file mode 100644 index 000000000..969745d0a --- /dev/null +++ b/app/views/dmails/_search.html.erb @@ -0,0 +1,8 @@ +<%= simple_form_for(:search, url: dmails_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> + <%= f.hidden_field :folder, value: params[:folder] %> + <%= f.input :title_matches, label: "Title", hint: "Use * for wildcard", input_html: { value: params[:search][:title_matches] } %> + <%= f.input :message_matches, label: "Message", hint: "Use * for wildcard", input_html: { value: params[:search][:messages_matches] } %> + <%= f.input :to_name, label: "To", input_html: { value: params[:search][:to_name] } %> + <%= f.input :from_name, label: "From", input_html: { value: params[:search][:from_name] } %> + <%= f.submit "Search" %> +<% end %> diff --git a/app/views/dmails/_secondary_links.html.erb b/app/views/dmails/_secondary_links.html.erb index 06a9d39dd..59c205b62 100644 --- a/app/views/dmails/_secondary_links.html.erb +++ b/app/views/dmails/_secondary_links.html.erb @@ -6,7 +6,6 @@
  • <%= link_to "Sent", sent_dmails_path(set_default_folder: true) %>
  • |
  • <%= link_to "New", new_dmail_path %>
  • -
  • <%= link_to "Search", search_dmails_path %>
  • <%= link_to "Mark all as read", {:controller => "dmails", :action => "mark_all_as_read"}, :method => :post, :remote => true %>
  • |
  • <%= link_to "Help", wiki_pages_path(title: "help:dmail") %>
  • diff --git a/app/views/dmails/index.html.erb b/app/views/dmails/index.html.erb index df04ee581..eaaa09cce 100644 --- a/app/views/dmails/index.html.erb +++ b/app/views/dmails/index.html.erb @@ -8,6 +8,8 @@

    Messages

    <% end %> + <%= render "search" %> + diff --git a/app/views/dmails/search.html.erb b/app/views/dmails/search.html.erb deleted file mode 100644 index 23c2700c0..000000000 --- a/app/views/dmails/search.html.erb +++ /dev/null @@ -1,20 +0,0 @@ -
    - -
    - -<%= render "secondary_links" %> - -<% content_for(:page_title) do %> - Search Messages - <%= Danbooru.config.app_name %> -<% end %> From e31f8acc34312c51285d003f42975208f115f4d4 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 29 Apr 2017 11:48:32 -0500 Subject: [PATCH 4/4] dmails: add title search test. --- test/unit/dmail_test.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/unit/dmail_test.rb b/test/unit/dmail_test.rb index 4a6d6343f..ec89c655c 100644 --- a/test/unit/dmail_test.rb +++ b/test/unit/dmail_test.rb @@ -74,9 +74,14 @@ class DmailTest < ActiveSupport::TestCase context "search" do should "return results based on title contents" do dmail = FactoryGirl.create(:dmail, :title => "xxx", :owner => @user) - matches = Dmail.search_message("xxx") - assert(matches.any?) - matches = Dmail.search_message("aaa") + + matches = Dmail.search(title_matches: "x") + assert_equal([dmail.id], matches.map(&:id)) + + matches = Dmail.search(message_matches: "xxx") + assert_equal([dmail.id], matches.map(&:id)) + + matches = Dmail.search(message_matches: "aaa") assert(matches.empty?) end