简体   繁体   English

如何彻底自定义 Rails Administrate gem

[英]How to drastically customise Rails Administrate gem

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.现在,我对 Ruby/Rails web 开发还很陌生,我尝试做的通常是在 React/Vue 等 JS 框架中非常直接。
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.当前的管理集合视图适用于具有相同 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).但是,我什至不知道如何引入具有不同类型的项目列表({ProductGroup,Product} Hash-items 以及 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.如何自定义表的属性以允许这样做,因为目前它会出错,因为产品项中的某些属性未在 {ProductGroup,Product} 哈希项中定义。

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.第二个问题是我什至如何开始为一行构建扩展功能,但这可能超出 scope 并且答案将获得额外的“你非常博学”的分数。

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 .我最后所做的是在产品 model 中添加一个标记为attr_accessor:product_variants ,然后在代码中实例化一个新产品,该产品包含产品组名称,除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):然后它将为您提供此数据结构(而不是 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通过这样做,我在管理仪表板中仍然拥有所有相同的属性,方法是将 ProductGroup 'duck-typing' 到 Product

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM