Every repository with this icon (
Every repository with this icon (
ContentMixin
A Mixin
Gluttonberg already allows us to easily create content classes using a mixin — see Gluttonberg::Content::Block. We could use this block mixin some additional logic for attaching assets to a content class.
Implementation
The mixin would add a class method for declaring the type of attachments, much like the #is method in DataMapper. It would then be used to declare an asset attachment and the the various constraints on it. For example it may allow images only, or multiple assets, including max/min options.
has_assets 1, :profile_picture, :categories => [:images], :types => [:png, :jpeg, :gif]
Count
The first argument in the method determines how many assets can/should be attached to this content class. It has exactly the same semantics as the #has method in DM. This means you can specify an exact count, infinite or a range.
# Between 0 and 2 assets
has_assets 0..2, :things
# Must have at least two and maximum of 8 assets
has_assets 2..8, :things
Constraints
The options will allow for a series of constraints to be set on the declaration. Some of these options will only apply to particular categories of assets i.e. resizing is for images only.
- Size
- Categories – documents, images etc.
- Type – the exact type, perhaps based one extension
- Filesize – particularly relevant with images, you don’t want users upping massive images
Associations
When these declarations are used, Gluttonberg will create all the associations in the background. It will also cache the names of those associations and their options, since it will need them to do various processing. For example the library needs to know the various image sizes being used for an asset so it can process and cache them on disk — particularly important if the actual image file is re-uploaded/replaced.






