Question

I have an hstore column that I'm using to build a table in Prawn (pdf builder). The data will consist of records for a given month. Since it is hstore, the keys used will likely change from day to day so this needs to be dynamic.

I need to determine:

1 What unique keys are used that month

I created a helper to find the unique keys that were used in the month. These will be used as column headers.

 keys(@users_logs)
 # this returns an array like - ["XC", "PIC", "Mountain"]

The table will display a users dutylog data for the month. For testing...If I explicitly call known hstore keys...the data displays correctly. But, since its hstore...I wont know what the table column will be in production.

For testing, I call known hstore keys...this creates the prawn table row data per duty log.

@users_logs.map do |dutylog|
        [ dutylog.properties["XC"], 
          dutylog.properties["PIC"], 
          dutylog.properties["Mountain"]
            ]
end

But, since this is hstore...I wont know what keys to call in production. So, I need to make the above iteration dynamic.

I tried, without success, to iterate over each dutylog entry, then iterate over each unique key and output one "dutylog.properties[x]" call for each key value...but, this just outputs the array of key values. I tried using send() in the block, but that didnt help.

@users_logs.map do |dutylog|
   [ keys(@users_logs).each { |k| dutylog.properties[k] }.join(",") ]
end

Any ideas on how I could make the "dutylog.properties[k]" dynamic?

Was it helpful?

Solution

Took some head scratching...but turning out to be quit easy

This will build the rows for the Prawn table

def hstore_duty_log_rows
    [keys(@users_logs)] +

    @users_logs.map do |dutylog|
        keys(@users_logs).map { |key| dutylog.properties.keys.include?(key) ? "#{dutylog.properties[key]}" : "0" }
    end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top