alphagov/trade-tariff-backend — pull request #57 by sauliusg (2013-01-22 11:17:27+00:00)
Title: Time machine and indexing fix

There are three ways how we access models and associations with validity periods:

* Usual case, when we browse the site on specific day. Queries in this case look like this:

```ruby

[14] pry(main)> TimeMachine.at('2012-01-21') { Heading.first.chapter_dataset }
=> #= '2012-01-21 00:00:00') OR (`goods_nomenclatures`.`validity_end_date` IS NULL)) AND (goods_nomenclatures.goods_nomenclature_item_id LIKE '0100000000')) ORDER BY `goods_nomenclature_item_id` ASC LIMIT 1">

```

* Relevant dates when indexing for search. We traverse say all headings and what to fetch associated chapter that actually spans the validity period of the heading. E.g. there may be two Chapters '01' that were valid during different periods. When looking them up from Heading we need to fetch the relevant one. This is only used in indexing.

```ruby
[1] pry(main)> TimeMachine.with_relevant_validity_periods { Heading.first.chapter_dataset }
=> #= NULL) OR (`goods_nomenclatures`.`validity_end_date` IS NULL)) AND (goods_nomenclatures.goods_nomenclature_item_id LIKE '0100000000')) ORDER BY `goods_nomenclature_item_id` ASC LIMIT 1">
```

* Normal console access, no time machine involved. We get:

```ruby
[2] pry(main)> Heading.first.chapter_dataset
=> #
```

No conditions for time.

This change brings back the second flow for indexing as we lost it in the previous change. Added tests for Heading->Chapter, Commodity->Heading/Chapter associations that ensure second flow works too.