Partials
Partials allow you to compose views from reusable components. Just like views, partials can define fields, options, views, other partials, formatters, and extensions.
class WidgetBlueprint < ApplicationBlueprint
field :name
view :foo do
use :associations
field :foo
end
view :bar do
use :associations, :description
field :bar
end
partial :associations do
object :category, CategoryBlueprint
collection :parts, PartBlueprint
end
partial :description do
field :description
end
end
There are two ways of including partials: appending with ‘use’ and inserting with ‘use!’ (see examples).
Append with ‘use’
Partials are appended to your view, giving them the opportunity to override your view’s fields, options, etc. Precedence (highest to lowest) is:
- Definitions in the partial
- Definitions in the view
- Definitions inherited from the blueprint/parent views
Insert with ‘use!’
Partials are embedded immediately, on that line, allowing subsequent lines to override the partial. Precedence (highest to lowest) is:
- Definitions in the view after
use! - Definitions in the partial
- Definitions in the view before
use! - Definitions inherited from the blueprint/parent views
Examples of ‘use’ and ‘use!’
partial :no_empty_fields do
options[:field_if] = :og_field_logic
# other stuff
end
# :foo appends the partial, so it overrides the view's field_if
view :foo do
use :no_empty_fields
options[:field_if] = :other_field_logic
end
# :bar inserts the partial, but the next line overrides the partial's field_if
view :bar do
use! :no_empty_fields
options[:field_if] = :other_field_logic
end