Matrix Edit in rails ActiveAdmin?

231 views Asked by At

I'm using ActiveAdmin in Rails.

My use case is similar to currency-exchange: say I have 10 currencies, and one currency can be converted into another one. In order to support editing, I need to create a matrix, where rows is CurrencyA, and columns is CurrencyB, and the value is conversion from CurrencyA to CurrencyB, something like this:

|     | SGD | USD | HKD | CNY |
|-----|-----|-----|-----|-----|
| SGD |     |     |     |     |
| USD |     |     |     |     |
| HKD |     |     |     |     |
| CNY |     |     |     |     |

Correspondingly, in my database, I have a table called currency_conversions, where it has:

from_currency | to_currency | conversion_rate

(My actual use case is not currency conversion, but this example can better show my use case).

However, I cannot find activeadmin have such functionality.. any suggestions?

1

There are 1 answers

0
songyy On BEST ANSWER

After quite some investigation, I've figured out

Here's how it looks like (the data is fake):

ActiveAdmin Matrix Editing

Here's what I did:

  1. define a custom controller: http://activeadmin.info/docs/8-custom-actions.html, handling the get & post request
  2. in the view, prepare the table, and it is a form
  3. define corresponding CSS in the app/assets/stylesheets/active_admin.css.scss

More over, since I've got an upvote today, let me share my code for the view here(it is .html.slim format); I'm reusing it several times:

/ Required params:
/   - headers       -- the headers array. each item would be passed to the header_blk
/   - left_headers  -- the array for the headers on the left side
/   - rows          -- contents for the table
/   - col_blk       -- a block to get the content needed for each column, where what passed in is the:
/                         column, row_id, col_id
/ Optional params:
/   - banner_top_right    -- the banner text you want to put at the top-right of the splitter
/   - banner_bottom_left  -- the banner text you want to put at the bottom-left of the splitter

- banner_top_right ||= ""
- banner_bottom_left ||= ""
table.admin-matrix
  thead
    tr
      td.diagonal-splitter
        svg(width='100%' height='100%')
          line(x1='0' y1='0' x2='100%' y2='100%' style='stroke:rgb(0,0,0);stroke-width=2')
        .triangle-top-right   = banner_top_right
        .triangle-bottom-left = banner_bottom_left
      - headers.each do |header|
        td.header
          = header
  tbody
    - rows.each_with_index do |row, rid|
      tr
        td.header = left_headers[rid]
        - row.each_with_index do |col, cid|
          td
            - col_blk[col, rid, cid]