0

Firstly, my apologies if this question has already been answered elsewhere, but I could not find a single post that is related to this. Please redirect me to the answer if one already exists.

Now, I'm pretty new to Ruby/Rails web development and what I'm attempting to do is usually really straight forward in a JS Framework like React/Vue etc.
What I want to do is to extend an existing Administrate table/collection view of Product items with the ability to have expandable rows which represent Product groups.

The data change would be like this:

From: [Product, Product, Product]
To: 
[{group=> ProductGroup, products=>[Product, Product]}, 
 {group=> ProductGroup, products=>[Product, Product]}, 
 Product, 
 Product]

The current administrate collection view is fine for a list of items that are of the same model. However, I've no clue on even how to introduce a list of items with varied types ({ProductGroup,Product} Hash-items as well as Product-items).

So my first question starts there; how do I customise the attributes of the table to allow for this, as currently it would error as some attributes expected in a Product-item are not defined in a {ProductGroup, Product} Hash-item.

The second question is how would I even start building the expansion functionality for a row, but that can be out of scope and answers would be granted additional 'you're very knowledgable' points.

1 Answers1

0

It turns out that the answer to this specific question lies in a different approach to how the data should be structured for the expandable item.

What I did in the end is add an attr_accessor labelled: :product_variants in the Product model and then, in code, instantiate a new Product which holds the product group-name and no other attributes except product_variants.

The pseudocode is as follows:

group = SomeProductGroup
decorated_group = Product.new(name: group.name)
decorated_group.product_variants = group.products

Which would then give you this data structure (instead of the initial one in the OP):

[Product (with product_variants), Product, Product]

By doing this, I still have all the same attributes in the Administrate dashboard by 'duck-typing' the ProductGroup into a Product