alphagov/govuk_content_models — pull request #72 by JordanHatch (2013-05-03 11:24:29+00:00)
Title: Add a trait for attaching files from the asset manager api

This is mainly extracted from the asset manager api code in Travel Advice Publisher.

- A new `Attachable` trait is added. This makes a class method available called `attaches` which can be used in a model to define a list of attachment types. This automatically defines a `_id` field of type string using the Mongoid helpers.

- When a file is attached, and the model saved, the `before_save` callback attempts to create a new asset in the asset manager for the file. If successful, the `_id` field is populated with the ID of the newly-created asset. If not successful, the model continues to be saved, but an error is added to warn that the asset was not uploaded.

- When the asset is requested, the stored ID is used to retrieve the asset details from the asset manager. This is cached in memory so that multiple references to the asset will only result in a single request to the API.

- An asset can be removed by setting the `remove_` field to true. This fits well when used with a form builder, as it plays nice with checkboxes using the same name.

An application wishing to use the API must assign an instance of `GdsApi::AssetManager` to `Attachable.asset_api_client`. If an api client is not present when asset details are requested, or if a new asset is uploaded, an exception is raised.

I consider this a **breaking change** to content models, due to travel advice edition changing to use the new implementation here.