From 1a03f49adc8e193e9e64f6a4b099e5a11acba5b4 Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 8 Mar 2013 15:56:24 -0500 Subject: [PATCH] fixes #775 --- app/models/tag.rb | 16 ++++++++-------- app/models/tag_subscription.rb | 2 +- ...130308204213_add_lower_name_index_to_pools.rb | 9 +++++++++ db/structure.sql | 11 ++++++++++- test/unit/post_test.rb | 7 +++++++ test/unit/tag_test.rb | 4 ++-- 6 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20130308204213_add_lower_name_index_to_pools.rb diff --git a/app/models/tag.rb b/app/models/tag.rb index 85b117fcc..d3e1e170e 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -144,7 +144,7 @@ class Tag < ActiveRecord::Base module ParseMethods def normalize(query) - query.to_s.downcase.strip + query.to_s.strip end def scan_query(query) @@ -219,18 +219,18 @@ class Tag < ActiveRecord::Base def parse_tag(tag, output) if tag[0] == "-" && tag.size > 1 - output[:exclude] << tag[1..-1] + output[:exclude] << tag[1..-1].downcase elsif tag[0] == "~" && tag.size > 1 - output[:include] << tag[1..-1] + output[:include] << tag[1..-1].downcase elsif tag =~ /\*/ - matches = Tag.name_matches(tag).all(:select => "name", :limit => Danbooru.config.tag_query_limit, :order => "post_count DESC").map(&:name) + matches = Tag.name_matches(tag.downcase).all(:select => "name", :limit => Danbooru.config.tag_query_limit, :order => "post_count DESC").map(&:name) matches = ["~no_matches~"] if matches.empty? output[:include] += matches else - output[:related] << tag + output[:related] << tag.downcase end end @@ -283,7 +283,7 @@ class Tag < ActiveRecord::Base q[:subscriptions] << $2 when "md5" - q[:md5] = $2.split(/,/) + q[:md5] = $2.downcase.split(/,/) when "-rating" q[:rating_negated] = $2 @@ -334,10 +334,10 @@ class Tag < ActiveRecord::Base q[:parent_id] = $2.to_i when "order" - q[:order] = $2 + q[:order] = $2.downcase when "status" - q[:status] = $2 + q[:status] = $2.downcase when "pixiv" q[:pixiv] = parse_helper($2) diff --git a/app/models/tag_subscription.rb b/app/models/tag_subscription.rb index 2435da33d..7acae058c 100644 --- a/app/models/tag_subscription.rb +++ b/app/models/tag_subscription.rb @@ -111,7 +111,7 @@ class TagSubscription < ActiveRecord::Base relation = where("creator_id = ?", user_id) if name - relation = relation.where("name ILIKE ? ESCAPE E'\\\\'", name.to_escaped_for_sql_like) + relation = relation.where("lower(name) LIKE ? ESCAPE E'\\\\'", name.downcase.to_escaped_for_sql_like) end relation.each do |tag_sub| diff --git a/db/migrate/20130308204213_add_lower_name_index_to_pools.rb b/db/migrate/20130308204213_add_lower_name_index_to_pools.rb new file mode 100644 index 000000000..6deddeee1 --- /dev/null +++ b/db/migrate/20130308204213_add_lower_name_index_to_pools.rb @@ -0,0 +1,9 @@ +class AddLowerNameIndexToPools < ActiveRecord::Migration + def self.up + execute "create index index_pools_on_lower_name on pools (lower(name))" + end + + def self.down + execute "drop index index_pools_on_lower_name" + end +end diff --git a/db/structure.sql b/db/structure.sql index 21aaebbe6..39c2b1380 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -5679,6 +5679,13 @@ CREATE INDEX index_pool_versions_on_updater_ip_addr ON pool_versions USING btree CREATE INDEX index_pools_on_creator_id ON pools USING btree (creator_id); +-- +-- Name: index_pools_on_lower_name; Type: INDEX; Schema: public; Owner: -; Tablespace: +-- + +CREATE INDEX index_pools_on_lower_name ON pools USING btree (lower((name)::text)); + + -- -- Name: index_pools_on_name; Type: INDEX; Schema: public; Owner: -; Tablespace: -- @@ -6231,4 +6238,6 @@ INSERT INTO schema_migrations (version) VALUES ('20130302214500'); INSERT INTO schema_migrations (version) VALUES ('20130305005138'); -INSERT INTO schema_migrations (version) VALUES ('20130307225324'); \ No newline at end of file +INSERT INTO schema_migrations (version) VALUES ('20130307225324'); + +INSERT INTO schema_migrations (version) VALUES ('20130308204213'); \ No newline at end of file diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 29a6120f8..bd5758f2b 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -909,6 +909,13 @@ class PostTest < ActiveSupport::TestCase assert_equal(post2.id, relation.first.id) end + should "return posts for a case sensitive source search" do + post1 = FactoryGirl.create(:post, :source => "ABCD") + post2 = FactoryGirl.create(:post, :source => "1234") + relation = Post.tag_match("source:ABCD") + assert_equal(1, relation.count) + end + should "return posts for a pixiv source search" do post = FactoryGirl.create(:post, :source => "http://i1.pixiv.net/img123/img/artist-name/789.png") assert_equal(1, Post.tag_match("source:pixiv/artist-name/*").count) diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index bd5a69e4d..c3e83407b 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -101,12 +101,12 @@ class TagTest < ActiveSupport::TestCase context "A tag parser" do should "scan a query" do assert_equal(%w(aaa bbb), Tag.scan_query("aaa bbb")) - assert_equal(%w(~aaa -bbb*), Tag.scan_query("~AAa -BBB* -bbb*")) + assert_equal(%w(~AAa -BBB* -bbb*), Tag.scan_query("~AAa -BBB* -bbb*")) end should "strip out invalid characters when scanning" do assert_equal(%w(aaa bbb), Tag.scan_tags("aaa bbb")) - assert_equal(%w(-bb;b* -b_b_b_), Tag.scan_tags("-B,B;B* -b_b_b_")) + assert_equal(%w(-BB;B* -b_b_b_), Tag.scan_tags("-B,B;B* -b_b_b_")) end should "cast values" do