UDV Worksheet - Plugin Tradecraft

The UDV Worksheet plugin allows administrators to create questions that can be filled out by consultants. The answers are inserted into the report as User Defined Variables. It is really handy because it can centralize questions that are typically added in an assessment while still allowing them to be used throughout a report template.

It also illustrates the use of multiple databases in a plugin.

Multiple Databases

One of the cool things about plugins is you can have multiple database connections. Two things are required, a connection to the database 1

DataMapper.setup(:udv, "sqlite://#{Dir.pwd}/plugins/UDV_Worksheet/udv.db")

A call to the secondary database is done using the Datamapper repository method (https://github.com/SerpicoProject/SerpicoPlugins/blob/master/UDV_Worksheet/routes.rb#L23).

 DataMapper.repository(:udv) {
      @questions = Questions.all(:report_id => 0)

Note, in this case we use a SQLite database. But with the addition of a different data mapper gem (e.g. dm-mysql-adapter), you could make a connection to another database.