Skip to content
This repository has been archived by the owner on Apr 4, 2018. It is now read-only.

Commit

Permalink
Index countries.
Browse files Browse the repository at this point in the history
  • Loading branch information
threedaymonk committed Nov 25, 2011
1 parent 7b7a2f9 commit b9d05e5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/models/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ def bounding_box_array
bounding_box.try(:split, ',')
end

def indexable_content
sections = raw_travel_advice && raw_travel_advice["travel_advice_sections"]
return "" unless sections
sections.inject(missions.map(&:address)) { |acc, advice|
acc.concat([advice["title"], advice["body"]["plain"]])
}.join(" ")
end

end
8 changes: 7 additions & 1 deletion lib/tasks/rummager.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
namespace :rummager do
desc "Reindex search engine"
task :index => :environment do
documents = []
documents = Country.all.map { |country| {
"title" => country.name,
"description" => "",
"format" => "fco",
"link" => "/travel-advice/countries/#{country.slug}",
"indexable_content" => country.indexable_content
}}
Rummageable.index documents
end
end
49 changes: 49 additions & 0 deletions test/unit/country_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,53 @@ def valid_attributes
assert !Country.new(valid_attributes.except(:iso_3166_1)).valid?
end

test 'should include title of advice in indexable_content' do
country = Country.new(raw_travel_advice: {
"travel_advice_sections" => [{
"title" => "SECTION_TITLE_1",
"body" => {"plain" => "SECTION_BODY_1"}
}, {
"title" => "SECTION_TITLE_2",
"body" => {"plain" => "SECTION_BODY_2"}
}]
})

indexable_content = country.indexable_content
assert_match %r{\bSECTION_TITLE_1\b}, indexable_content
assert_match %r{\bSECTION_TITLE_2\b}, indexable_content
end

test 'should include plain text of advice in indexable_content' do
country = Country.new(raw_travel_advice: {
"travel_advice_sections" => [{
"title" => "SECTION_TITLE_1",
"body" => {"plain" => "SECTION_BODY_1"}
}, {
"title" => "SECTION_TITLE_2",
"body" => {"plain" => "SECTION_BODY_2"}
}]
})

indexable_content = country.indexable_content
assert_match %r{\bSECTION_BODY_1\b}, indexable_content
assert_match %r{\bSECTION_BODY_2\b}, indexable_content
end

test 'should include mission addresses in indexable_content' do
country = Country.new(raw_travel_advice: {"travel_advice_sections" => []})
country.missions.build(address: "ADDRESS")

indexable_content = country.indexable_content
assert_match %r{\bADDRESS\b}, indexable_content
end

test 'should return empty indexable_content if raw_travel_advice is nil' do
country = Country.new(raw_travel_advice: nil)
assert_equal "", country.indexable_content
end

test 'should return empty indexable_content if travel_advice_sections is missing' do
country = Country.new(raw_travel_advice: {})
assert_equal "", country.indexable_content
end
end

0 comments on commit b9d05e5

Please sign in to comment.