Permalink
Browse files

Automatically keep ministerial role search index up-to-date

  • Loading branch information...
2 parents eb52c03 + c481f19 commit a026fae9ea8587df06d1e9e47c17bcf5927ab116 @tomstuart tomstuart committed Jan 30, 2012
@@ -0,0 +1,17 @@
+class MinisterialRoleSearchIndexObserver < ActiveRecord::Observer
+ observe :person, :organisation, :role_appointment, :organisation_role
+
+ def after_save(record)
+ reindex_ministerial_roles
+ end
+
+ def after_destroy(record)
+ reindex_ministerial_roles
+ end
+
+ private
+
+ def reindex_ministerial_roles
+ Rummageable.index(MinisterialRole.search_index)
+ end
+end
@@ -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 = :cacher, :garbage_collector, :forum_observer
+ config.active_record.observers = :ministerial_role_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
@@ -1,4 +1,6 @@
# encoding: utf-8
+ActiveRecord::Base.observers.disable :ministerial_role_search_index_observer
+
["England", "Scotland", "Wales", "Northern Ireland"].each do |nation_name|
Nation.find_or_create_by_name(nation_name)
end
@@ -472,3 +474,5 @@ def other_civil_servants(person_to_role_to_organisation)
if Rails.env.development? || Rails.env.test?
require Rails.root.join("db/random_seeds.rb")
end
+
+ActiveRecord::Base.observers.enable :ministerial_role_search_index_observer
@@ -0,0 +1,6 @@
+FactoryGirl.define do
+ factory :organisation_role do
+ organisation
+ role
+ end
+end
@@ -0,0 +1,90 @@
+require 'test_helper'
+
+class MinisterialRoleSearchIndexObserverTest < ActiveSupport::TestCase
+ test 'should reindex all ministerial roles when a person is updated' do
+ person = create(:person)
+
+ search_index_data = stub('search index data')
+ MinisterialRole.stubs(:search_index).returns(search_index_data)
+ Rummageable.expects(:index).with(search_index_data)
+
+ person.forename = 'Jim'
+ person.save
+ end
+
+ test 'should reindex all ministerial roles when an organisation is updated' do
+ organisation = create(:organisation)
+
+ search_index_data = stub('search index data')
+ MinisterialRole.stubs(:search_index).returns(search_index_data)
+ Rummageable.stubs(:index) # ignore the update to the organisation index
+ Rummageable.expects(:index).with(search_index_data)
+
+ organisation.name = 'Ministry of Funk'
+ organisation.save
+ end
+
+ test 'should reindex all ministerial roles when a role appointment is created' do
+ role_appointment = build(:ministerial_role_appointment)
+
+ search_index_data = stub('search index data')
+ MinisterialRole.stubs(:search_index).returns(search_index_data)
+ Rummageable.expects(:index).with(search_index_data)
+
+ role_appointment.save
+ end
+
+ test 'should reindex all ministerial roles when a role appointment is updated' do
+ role_appointment = create(:ministerial_role_appointment)
+ person = create(:person)
+
+ search_index_data = stub('search index data')
+ MinisterialRole.stubs(:search_index).returns(search_index_data)
+ Rummageable.expects(:index).with(search_index_data)
+
+ role_appointment.person = person
+ role_appointment.save
+ end
+
+ test 'should reindex all ministerial roles when a role appointment is destroyed' do
+ role_appointment = create(:ministerial_role_appointment)
+
+ search_index_data = stub('search index data')
+ MinisterialRole.stubs(:search_index).returns(search_index_data)
+ Rummageable.expects(:index).with(search_index_data)
+
+ role_appointment.destroy
+ end
+
+ test 'should reindex all ministerial roles when an organisation role is created' do
+ organisation_role = build(:organisation_role)
+
+ search_index_data = stub('search index data')
+ MinisterialRole.stubs(:search_index).returns(search_index_data)
+ Rummageable.expects(:index).with(search_index_data)
+
+ organisation_role.save
+ end
+
+ test 'should reindex all ministerial roles when an organisation role is updated' do
+ organisation_role = create(:organisation_role)
+ organisation = create(:organisation)
+
+ search_index_data = stub('search index data')
+ MinisterialRole.stubs(:search_index).returns(search_index_data)
+ Rummageable.expects(:index).with(search_index_data)
+
+ organisation_role.organisation = organisation
+ organisation_role.save
+ end
+
+ test 'should reindex all ministerial roles when an organisation role is destroyed' do
+ organisation_role = create(:organisation_role)
+
+ search_index_data = stub('search index data')
+ MinisterialRole.stubs(:search_index).returns(search_index_data)
+ Rummageable.expects(:index).with(search_index_data)
+
+ organisation_role.destroy
+ end
+end
@@ -144,6 +144,7 @@ class OrganisationTest < ActiveSupport::TestCase
search_index_data = stub('search index data')
organisation.stubs(:search_index).returns(search_index_data)
+ Rummageable.stubs(:index) # ignore the update to the ministerial role index
Rummageable.expects(:index).with(search_index_data)
organisation.save
@@ -154,6 +155,7 @@ class OrganisationTest < ActiveSupport::TestCase
search_index_data = stub('search index data')
organisation.stubs(:search_index).returns(search_index_data)
+ Rummageable.stubs(:index) # ignore the update to the ministerial role index
Rummageable.expects(:index).with(search_index_data)
organisation.name = 'Ministry of Junk'

0 comments on commit a026fae

Please sign in to comment.