Merge pull request #716 from oshiho3/PT80956846_crop_search

exclude unapproved crops from search
This commit is contained in:
Mackenzie
2015-02-20 22:04:53 -05:00
3 changed files with 21 additions and 4 deletions

View File

@@ -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"

View File

@@ -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
@@ -76,6 +77,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 +94,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 +313,9 @@ class Crop < ActiveRecord::Base
fields: ["name", "scientific_names.scientific_name", "alternate_names.name"]
}
},
filter: {
term: {approval_status: "approved"}
},
size: 50
}
)
@@ -321,15 +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
return matches
end
end

View File

@@ -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
@rejected_crop = FactoryGirl.create(:rejected_crop, :name => 'tomato')
sync_elasticsearch([@rejected_crop])
Crop.search('tomato').should_not include @rejected_crop
end
it "doesn't find 'pending' crop" do
@crop_request = FactoryGirl.create(:crop_request, :name => 'tomato')
sync_elasticsearch([@crop_request])
Crop.search('tomato').should_not include @crop_request
end
end
context "csv loading" do