简体   繁体   English

Rails ActiveAdmin修改资源对象

[英]Rails ActiveAdmin modify resource object

I've currently got a user object but to avoid redundancy, I'd like to wrap it into a presenter object called MerchantUser/ProviderUser. 我目前有一个用户对象,但为了避免冗余,我想把它包装到一个名为MerchantUser / ProviderUser的presenter对象中。 However, with ActiveAdmin, I'm a little confused on how to do this. 但是,使用ActiveAdmin,我对如何执行此操作感到有些困惑。 I've tried using before_create to change the user into the corresponding presenters but in index...do, I'm still seeing that user.class is equal to User and not the wrapper classes that I've defined. 我已经尝试使用before_create将用户更改为相应的演示者,但是在索引...中,我仍然看到user.class等于User而不是我定义的包装类。

I've looked into scoping_collection but unfortunately that only works on collections and not individual objects? 我已经研究过scoping_collection,但不幸的是,这只适用于集合而不是单个对象?

 ActiveAdmin.register User, as: "Companies" do # rubocop:disable Metrics/BlockLength
  before_create do |user|
    if user.merchant?
      user = MerchantUser.new(user)
    else
      user = ProviderUser.new(user)
    end
  end

  actions :all, except: [:destroy]
  permit_params :name, :email, contract_attributes: [:id, :flat_rate, :percentage]

  filter :role, as: :select
  index do # rubocop:disable Metrics/BlockLength
    column :name do |user|
      user.name <---I want it so I can just do this without the if/else blocks like below.
    end
    column :role
    column :contact_phone
    column :email
    column :website do |user|
      if user.merchant?
        user.company.website
      else
        user.provider.website
      end
    end

    column :flat_rate do |user|
      money_without_cents_and_with_symbol(user.contract.flat_rate)
    end
    column :percentage do |user|
      number_to_percentage(user.contract.percentage, precision: 0)
    end
    actions
  end

Have you looked into Active Admin's support for decorators? 您是否考虑过Active Admin对装饰器的支持? This page is quite comprehensive. 这个页面非常全面。 The best way to implement them depends on how your decorator/presenter object is implemented. 实现它们的最佳方式取决于decorator / presenter对象的实现方式。

Link summary: use decorate_with or look into using this gem for PORO support 链接摘要:使用decorate_with或者使用此gem来获取PORO支持

Are you sure you want/need a presenter here? 你确定你想要/需要一个演示者吗? You can register the same Rails model multiple times as ActiveAdmin resources with different names and customizations (filters, index page, forms, etc). 您可以使用不同的名称和自定义(过滤器,索引页,表单等)多次注册相同的Rails模型作为ActiveAdmin资源。 You can also use Rails STI or just subclass Rails models, perhaps with different Rails default_scope and then register the subclasses. 您也可以使用Rails STI或仅使用Rails模型子类,可能使用不同的Rails default_scope,然后注册子类。

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

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