Permalink
Browse files

Add supporting pages to search

  • Loading branch information...
2 parents dabeb4b + 9faa1cb commit 4fef4b1d685528030722d0e469d0fbfd00c329c7 @tomstuart tomstuart committed Jan 31, 2012
View
7 app/models/document/workflow.rb
@@ -12,6 +12,13 @@ module Document::Workflow
scope :published, where(state: "published")
define_model_callbacks :publish, :archive, only: :after
+ set_callback :publish, :after do
+ notify_observers :after_publish
+ end
+ set_callback :archive, :after do
+ notify_observers :after_archive
+ end
+
after_publish :archive_previous_documents
state_machine do
View
4 app/models/searchable.rb
@@ -26,8 +26,8 @@ def searchable(options)
end
end
- set_callback searchable_options[:index_after], :after, :update_in_search_index
- set_callback searchable_options[:unindex_after], :after, :remove_from_search_index
+ set_callback searchable_options[:index_after], :after, :update_in_search_index if searchable_options[:index_after]
+ set_callback searchable_options[:unindex_after], :after, :remove_from_search_index if searchable_options[:unindex_after]
end
end
View
20 app/models/supporting_page.rb
@@ -1,4 +1,8 @@
class SupportingPage < ActiveRecord::Base
+ include Searchable
+ include Rails.application.routes.url_helpers
+ include PublicDocumentRoutesHelper
+
belongs_to :document
has_many :supporting_page_attachments
@@ -8,6 +12,11 @@ class SupportingPage < ActiveRecord::Base
validates :title, :body, :document, presence: true
+ scope :published, joins(:document).merge(Document.published)
+
+ searchable title: :title, link: :search_link, content: :body_without_markup, format: -> p { p.document.type.underscore },
+ only: :published, index_after: false, unindex_after: false
+
extend FriendlyId
friendly_id :title, use: :slugged
@@ -33,6 +42,17 @@ def destroyable?
!document.published?
end
+ def search_link
+ # This should be public_supporting_page_path(document, self), but we can't use that because friendly_id's
+ # #to_param returns the old value of the slug (e.g. nil for a new record) if the record is dirty, and
+ # apparently the record is still marked as dirty during after_save callbacks.
+ public_supporting_page_path(document, slug)
+ end
+
+ def body_without_markup
+ Govspeak::Document.new(body).to_text
+ end
+
private
def prevent_destruction_on_published_documents
View
11 app/models/supporting_page_search_index_observer.rb
@@ -0,0 +1,11 @@
+class SupportingPageSearchIndexObserver < ActiveRecord::Observer
+ observe :policy # observe :document doesn't work
+
+ def after_publish(document)
+ document.supporting_pages.each(&:update_in_search_index)
+ end
+
+ def after_archive(document)
+ document.supporting_pages.each(&:remove_from_search_index)
+ end
+end
View
2 config/application.rb
@@ -25,7 +25,7 @@ class Application < Rails::Application
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
- config.active_record.observers = :ministerial_role_search_index_observer
+ config.active_record.observers = :ministerial_role_search_index_observer, :supporting_page_search_index_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
View
2 lib/whitehall.rb
@@ -41,7 +41,7 @@ def use_s3?
end
def search_index
- [Document, MinisterialRole, Organisation].map(&:search_index).sum([])
+ [Document, MinisterialRole, Organisation, SupportingPage].map(&:search_index).sum([])
end
private
View
5 test/integration/search_index_test.rb
@@ -15,4 +15,9 @@ class SearchIndexTest < ActiveSupport::TestCase
MinisterialRole.stubs(:search_index).returns([:ministerial_roles])
assert Whitehall.search_index.include?(:ministerial_roles)
end
+
+ test "Whitehall.search_index includes supporting pages" do
+ MinisterialRole.stubs(:search_index).returns([:supporting_pages])
+ assert Whitehall.search_index.include?(:supporting_pages)
+ end
end
View
29 test/unit/supporting_page_search_index_observer_test.rb
@@ -0,0 +1,29 @@
+require 'test_helper'
+
+class SupportingPageSearchIndexObserverTest < ActiveSupport::TestCase
+ test 'should add supporting page to search index when its document is published' do
+ policy = create(:submitted_policy)
+ supporting_page = create(:supporting_page, document: policy)
+
+ search_index_data = stub('search index data')
+ policy.stubs(:supporting_pages).returns([supporting_page])
+ supporting_page.stubs(:search_index).returns(search_index_data)
+ Rummageable.stubs(:index) # ignore the update to the document index
+ Rummageable.expects(:index).with(search_index_data)
+
+ policy.publish_as(create(:departmental_editor))
+ end
+
+ test 'should remove supporting page from search index when its document is archived' do
+ policy = create(:published_policy)
+ supporting_page = create(:supporting_page, document: policy)
+ policy_slug = policy.document_identity.slug
+
+ Rummageable.stubs(:delete) # ignore the delete from the document index
+ Rummageable.expects(:delete).with("/government/policies/#{policy_slug}/supporting-pages/#{supporting_page.slug}")
+
+ new_edition = policy.create_draft(create(:policy_writer))
+ new_edition.reload # because each supporting page touches the new document as it's copied over
+ new_edition.publish_as(create(:departmental_editor), force: true)
+ end
+end
View
60 test/unit/supporting_page_test.rb
@@ -69,4 +69,64 @@ class SupportingPageTest < ActiveSupport::TestCase
assert supporting_page.destroy
assert_raises(ActiveRecord::RecordNotFound) { supporting_page.reload }
end
+
+ test 'should return search index data suitable for Rummageable' do
+ policy = create(:published_policy)
+ policy_slug = policy.document_identity.slug
+ supporting_page = create(:supporting_page, title: 'Love all the people', document: policy)
+
+ assert_equal 'Love all the people', supporting_page.search_index["title"]
+ assert_equal "/government/policies/#{policy_slug}/supporting-pages/#{supporting_page.slug}", supporting_page.search_index['link']
+ assert_equal supporting_page.body, supporting_page.search_index['indexable_content']
+ assert_equal 'policy', supporting_page.search_index['format']
+ end
+
+ test 'should not add supporting page to search index on creating' do
+ supporting_page = build(:supporting_page)
+
+ search_index_data = stub('search index data')
+ supporting_page.stubs(:search_index).returns(search_index_data)
+ Rummageable.expects(:index).with(search_index_data).never
+
+ supporting_page.save
+ end
+
+ test 'should not add supporting page to search index on updating' do
+ supporting_page = create(:supporting_page)
+
+ search_index_data = stub('search index data')
+ supporting_page.stubs(:search_index).returns(search_index_data)
+ Rummageable.expects(:index).with(search_index_data).never
+
+ supporting_page.title = 'Love all the people'
+ supporting_page.save
+ end
+
+ test 'should not remove supporting page from search index on destroying' do
+ policy = create(:published_policy)
+ supporting_page = create(:supporting_page, document: policy)
+ policy_slug = policy.document_identity.slug
+
+ Rummageable.expects(:delete).with("/government/policies/#{policy_slug}/supporting-pages/#{supporting_page.slug}").never
+ supporting_page.destroy
+ end
+
+ test 'should return search index data for all supporting pages on published documents' do
+ policy = create(:published_policy)
+ draft_policy = create(:draft_policy)
+ policy_slug = policy.document_identity.slug
+ create(:supporting_page, document: policy, title: 'Love all the people', body: 'Thoughts on love and smoking.')
+ create(:supporting_page, document: policy, title: 'Dangerous', body: 'I love my job.')
+ create(:supporting_page, document: policy, title: 'Relentless', body: 'Rockers against drugs suck.')
+ create(:supporting_page, document: policy, title: 'Arizona Bay', body: 'Marketing and advertising.')
+ create(:supporting_page, document: draft_policy, title: 'Rant in E-Minor', body: 'I\'m talking to the women here.')
+
+ results = SupportingPage.search_index
+
+ assert_equal 4, results.length
+ assert_equal({ 'title' => 'Love all the people', 'link' => "/government/policies/#{policy_slug}/supporting-pages/love-all-the-people", 'indexable_content' => 'Thoughts on love and smoking.', 'format' => 'policy' }, results[0])
+ assert_equal({ 'title' => 'Dangerous', 'link' => "/government/policies/#{policy_slug}/supporting-pages/dangerous", 'indexable_content' => 'I love my job.', 'format' => 'policy' }, results[1])
+ assert_equal({ 'title' => 'Relentless', 'link' => "/government/policies/#{policy_slug}/supporting-pages/relentless", 'indexable_content' => 'Rockers against drugs suck.', 'format' => 'policy' }, results[2])
+ assert_equal({ 'title' => 'Arizona Bay', 'link' => "/government/policies/#{policy_slug}/supporting-pages/arizona-bay", 'indexable_content' => 'Marketing and advertising.', 'format' => 'policy' }, results[3])
+ end
end

0 comments on commit 4fef4b1

Please sign in to comment.