Browse files

Allow policy.rb to be parsed without documents table.

Before this commit, using rake to drop and create databases, and to
migrate those databases would fail (`rake db:drop:all db:create:all
db:migrate`). This is because we now have the
SupportingPageSearchIndexObserver which observes :policy, causing
policy.rb to be evaluated at Rails app initialisation time. There were
two problems with evaluating policy.rb when the documents table didn't

* The `scope :stub` line caused active record to inspect the documents
table schema, which didn't exist. Interestingly, `scope` only exhibits
this behaviour when the model is derived from another model. If we
change Policy so that it derives from ActiveRecord::Base then `scope`
doesn't cause active record to inspect the database schema.

* The `define_attribute_methods` line caused active record to inspect
the documents table schema in order to generate the model accessor
methods. This was only required so that we could then call
`alias_method_chain :title, :stub`. By introducing explicit
`#title_without_stub` and `#title` methods we can avoid the use of both
`alias_method_chain` and `define_attribute_methods`.

This commit makes it possible to evaluate policy.rb even when the
documents table is missing, which in turn allows us to schema:load and
migrate an empty database.
  • Loading branch information...
1 parent e293b5e commit 7b4a62a85802a4e9a6da0bf2f75861aabbcbdd1d @chrisroos chrisroos committed Jan 31, 2012
Showing with 8 additions and 6 deletions.
  1. +8 −6 app/models/policy.rb
@@ -20,15 +20,17 @@ def process_associations_after_save(document)
after_destroy :remove_document_relations
- scope :stub, where(stub: true)
+ def self.stub
+ where(stub: true)
+ end
- define_attribute_methods
+ def title_without_stub
+ read_attribute(:title)
+ end
- def title_with_stub
- original_title = title_without_stub
- stub? ? "[Sample] #{original_title}" : original_title
+ def title
+ stub? ? "[Sample] #{title_without_stub}" : title_without_stub
- alias_method_chain :title, :stub
def sluggable_title

0 comments on commit 7b4a62a

Please sign in to comment.