From 2241a760c2f9fa52411c93038ebedea6d4a95f98 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Tue, 17 Feb 2015 23:33:39 +1100 Subject: [PATCH 1/8] exclude unapproved crops from search --- app/models/crop.rb | 6 +++++- spec/models/crop_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/crop.rb b/app/models/crop.rb index 0571feecd..89471f90f 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -76,6 +76,7 @@ class Crop < ActiveRecord::Base mappings dynamic: 'false' do indexes :id, type: 'long' indexes :name, type: 'string', analyzer: 'gs_edgeNGram_analyzer' + indexes :approval_status, type: 'string' indexes :scientific_names do indexes :scientific_name, type: 'string', @@ -92,7 +93,7 @@ class Crop < ActiveRecord::Base def as_indexed_json(options={}) self.as_json( - only: [:id, :name], + only: [:id, :name, :approval_status], include: { scientific_names: { only: :scientific_name }, alternate_names: { only: :name } @@ -311,6 +312,9 @@ class Crop < ActiveRecord::Base fields: ["name", "scientific_names.scientific_name", "alternate_names.name"] } }, + filter: { + term: {approval_status: "approved"} + }, size: 50 } ) diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 9cda818e8..02f3149ae 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -342,6 +342,16 @@ describe Crop do it "searches case insensitively" do Crop.search('mUsH').should include @mushroom end + it "doesn't find 'rejected' crop" do + @mushroom.approval_status = "rejected" + sync_elasticsearch([@mushroom]) + Crop.search('mushroom').should_not include @mushroom + end + it "doesn't find 'pending' crop" do + @mushroom.approval_status = "pending" + sync_elasticsearch([@mushroom]) + Crop.search('mushroom').should_not include @mushroom + end end context "csv loading" do From 023333b15f096353203a12cdf3431ff0330cd7f1 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Tue, 17 Feb 2015 23:33:39 +1100 Subject: [PATCH 2/8] exclude unapproved crops from search --- app/models/crop.rb | 6 +++++- spec/models/crop_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/crop.rb b/app/models/crop.rb index 0571feecd..89471f90f 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -76,6 +76,7 @@ class Crop < ActiveRecord::Base mappings dynamic: 'false' do indexes :id, type: 'long' indexes :name, type: 'string', analyzer: 'gs_edgeNGram_analyzer' + indexes :approval_status, type: 'string' indexes :scientific_names do indexes :scientific_name, type: 'string', @@ -92,7 +93,7 @@ class Crop < ActiveRecord::Base def as_indexed_json(options={}) self.as_json( - only: [:id, :name], + only: [:id, :name, :approval_status], include: { scientific_names: { only: :scientific_name }, alternate_names: { only: :name } @@ -311,6 +312,9 @@ class Crop < ActiveRecord::Base fields: ["name", "scientific_names.scientific_name", "alternate_names.name"] } }, + filter: { + term: {approval_status: "approved"} + }, size: 50 } ) diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 9cda818e8..9332d9c0f 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -342,6 +342,16 @@ describe Crop do it "searches case insensitively" do Crop.search('mUsH').should include @mushroom end + it "doesn't find 'rejected' crop" do + @mushroom.update_attributes(approval_status: "rejected") + sync_elasticsearch([@mushroom]) + Crop.search('mushroom').should_not include @mushroom + end + it "doesn't find 'pending' crop" do + @mushroom.update_attributes(approval_status: "pending") + sync_elasticsearch([@mushroom]) + Crop.search('mushroom').should_not include @mushroom + end end context "csv loading" do From ef2d1eb68358dd16d481829b0fdd0587094af7d1 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Wed, 18 Feb 2015 00:17:53 +1100 Subject: [PATCH 3/8] fixing Travis CI error --- spec/models/crop_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 9332d9c0f..f4e12d6db 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -343,12 +343,12 @@ describe Crop do Crop.search('mUsH').should include @mushroom end it "doesn't find 'rejected' crop" do - @mushroom.update_attributes(approval_status: "rejected") + @mushroom.assign_attributes(approval_status: "rejected") sync_elasticsearch([@mushroom]) Crop.search('mushroom').should_not include @mushroom end it "doesn't find 'pending' crop" do - @mushroom.update_attributes(approval_status: "pending") + @mushroom.assign_attributes(approval_status: "pending") sync_elasticsearch([@mushroom]) Crop.search('mushroom').should_not include @mushroom end From eb5e9e926bf42d28080849fdb9fb32fd03eaad57 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Wed, 18 Feb 2015 00:17:53 +1100 Subject: [PATCH 4/8] fixing Travis CI error --- spec/models/crop_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 9332d9c0f..96ed1ea0b 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -343,14 +343,14 @@ describe Crop do Crop.search('mUsH').should include @mushroom end it "doesn't find 'rejected' crop" do - @mushroom.update_attributes(approval_status: "rejected") - sync_elasticsearch([@mushroom]) - Crop.search('mushroom').should_not include @mushroom + @rejected_crop = FactoryGirl.create(:rejected_crop, :name => 'mushroom') + sync_elasticsearch([@rejected_crop]) + Crop.search('mushroom').should_not include @rejected_crop end it "doesn't find 'pending' crop" do - @mushroom.update_attributes(approval_status: "pending") - sync_elasticsearch([@mushroom]) - Crop.search('mushroom').should_not include @mushroom + @crop_request = FactoryGirl.create(:crop_request, :name => 'mushroom') + sync_elasticsearch([@crop_request]) + Crop.search('mushroom').should_not include @crop_request end end From 8ab3167a2851cc8d5995150c7e0ca5d73a973009 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Wed, 18 Feb 2015 11:15:15 +1100 Subject: [PATCH 5/8] update sql search to excludes unapproved crops --- app/models/crop.rb | 2 ++ spec/models/crop_spec.rb | 1 + 2 files changed, 3 insertions(+) diff --git a/app/models/crop.rb b/app/models/crop.rb index 7d880a3b5..d321353a7 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -334,6 +334,8 @@ class Crop < ActiveRecord::Base matches.delete(exact_match) matches.unshift(exact_match) end + + matches = matches.select {|c| c.approval_status == 'approved'} return matches end end diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index d58c46ee3..ee773e7cb 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -1,4 +1,5 @@ require 'rails_helper' +require 'pry' describe Crop do context 'all fields present' do From 506e3e550cd1a529de8786d50cfd3037cd973a12 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Wed, 18 Feb 2015 11:15:15 +1100 Subject: [PATCH 6/8] update sql search to excludes unapproved crops --- app/models/crop.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/crop.rb b/app/models/crop.rb index 7d880a3b5..d321353a7 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -334,6 +334,8 @@ class Crop < ActiveRecord::Base matches.delete(exact_match) matches.unshift(exact_match) end + + matches = matches.select {|c| c.approval_status == 'approved'} return matches end end From 35f8c126b6426ded0f2dda30e920cd151938f386 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Wed, 18 Feb 2015 15:38:28 +1100 Subject: [PATCH 7/8] using scope for searching only approved records --- app/models/crop.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/crop.rb b/app/models/crop.rb index d321353a7..4d898c73c 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -27,6 +27,7 @@ class Crop < ActiveRecord::Base scope :popular, -> { where(:approval_status => "approved").reorder("plantings_count desc, lower(name) asc") } scope :randomized, -> { where(:approval_status => "approved").reorder('random()') } # ok on sqlite and psql, but not on mysql scope :pending_approval, -> { where(:approval_status => "pending") } + scope :approved, -> { where(:approval_status => "approved") } scope :rejected, -> { where(:approval_status => "rejected") } ## Wikipedia urls are only necessary when approving a crop @@ -325,17 +326,16 @@ class Crop < ActiveRecord::Base # collection, so it matches what we get from elasticsearch and we can # manipulate it in the same ways (eg. deleting elements without deleting # the whole record from the db) - matches = where("name ILIKE ?", "%#{query}%").to_a + matches = Crop.approved.where("name ILIKE ?", "%#{query}%").to_a # we want to make sure that exact matches come first, even if not # using elasticsearch (eg. in development) - exact_match = Crop.find_by_name(query) + exact_match = Crop.approved.find_by_name(query) if exact_match matches.delete(exact_match) matches.unshift(exact_match) end - matches = matches.select {|c| c.approval_status == 'approved'} return matches end end From d338bab82bf3255efefa63c55ab2ee39a52420a6 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Wed, 18 Feb 2015 16:18:24 +1100 Subject: [PATCH 8/8] travis to test both with and without elasticsearch --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index dd6853a02..8b8d9a6b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,8 @@ language: ruby cache: bundler env: matrix: - - GROWSTUFF_SITE_NAME="Growstuff (travis)" RAILS_SECRET_TOKEN='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' + - GROWSTUFF_SITE_NAME="Growstuff (travis)" RAILS_SECRET_TOKEN='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' GROWSTUFF_ELASTICSEARCH='true' + - GROWSTUFF_SITE_NAME="Growstuff (travis)" RAILS_SECRET_TOKEN='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' GROWSTUFF_ELASTICSEARCH='false' global: secure: "Z5TpM2jEX4UCvNePnk/LwltQX48U2u9BRc+Iypr1x9QW2o228QJhPIOH39a8RMUrepGnkQIq9q3ZRUn98RfrJz1yThtlNFL3NmzdQ57gKgjGwfpa0e4Dwj/ZJqV2D84tDGjvdVYLP7zzaYZxQcwk/cgNpzKf/jq97HLNP7CYuf4=" bundler_args: "--without development production staging"