...
2. Building your first Bolt Widget
The dashboard service in Impac! Angular fetches the widgets from the Impac! Finance Bolt with a request to /api/v2/maestrano/finance/widgets/.
In the Bolt, your first step is to add the name of your new widget to the WIDGETS_LIST so that it may be included in the response for this endpoint.
Code Block | ||
---|---|---|
| ||
class Api::V1::WidgetsController < ApplicationController
...
WIDGETS_LIST = %w(cash_balance cash_projection your_new_widget).freeze
...
end |
The next step is to create the model for your new widget and let it inherit the base widget class. Create this file in the /app/models/widgets/ directory.
Code Block | ||
---|---|---|
| ||
class Widgets::YourNewWidget < Widgets::Base
end |
Before you start with the calculations, think about what you want your widget to display. Will your widget hold a chart? A grouped table? Both? Your new widget’s model will use data from the Bolt’s database to manipulate and run the calculations necessary for it’s requested report. The report output, however, is based on the design of your widget.
These displays (chart, table, grouped_table, etc) have reporting layouts, which are unique data structures that determine how a widget will return the result of its calculation. Layouts are predefined, and will maintain the same data structure regardless of any requested report.
The data structures have been designed with the intentions of being directly usable by a reporting front-end or charting library with little transformation.
Every report will be output to one or several layouts. You must define a SUPPORTED_LAYOUTS constant for each individual widget (an array of the layouts for your report). The supported layouts will be listed for each widget at the public endpoint (/api/v2/maestrano/finance/widgets/).
Code Block | ||||
---|---|---|---|---|
| ||||
class Widgets::YourNewWidget < Widgets::Base
...
SUPPORTED_LAYOUTS = %w(chart grouped_table).freeze
...
end |
A compute method must be defined for each widget, and it must return self. It fetches the report and calculates the widget.
Code Block | ||||
---|---|---|---|---|
| ||||
def compute
...
return self
end |
The compute method will fetch the calculated data you need for your report, but you still have to fill your layout objects for rendering. Each layout has a class defining its structure, and can be filled with your report data by defining a fill method ("fill_#{layout.name}") that follows the layout's cl.
Code Block | ||||
---|---|---|---|---|
| ||||
def fill_grouped_table
my_items = {
"Type1" => [ item1, item2],
"Type2" => [ item3, item4]
}
my_items.each do |header, group|
layout.headers << header
layout.groups << group
end
end |
WIP!