【问题标题】:Rails ActiveAdmin modify resource objectRails ActiveAdmin 修改资源对象
【发布时间】:2017-03-24 15:06:14
【问题描述】:

我目前有一个用户对象,但为了避免冗余,我想将它包装到一个名为 MerchantUser/ProviderUser 的演示者对象中。但是,对于 ActiveAdmin,我对如何执行此操作有点困惑。我尝试使用 before_create 将用户更改为相应的演示者,但在 index...do 中,我仍然看到 user.class 等于 User 而不是我定义的包装类。

我已经研究了 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

【问题讨论】:

    标签: ruby-on-rails activeadmin


    【解决方案1】:

    您是否查看过 Active Admin 对装饰器的支持? This page 比较全面。实现它们的最佳方式取决于您的装饰器/演示器对象是如何实现的。

    链接摘要:使用 decorate_with 或查看使用 this gem 以获得 PORO 支持

    【讨论】:

      【解决方案2】:

      您确定要/需要一位演示者吗?您可以将同一个 Rails 模型多次注册为具有不同名称和自定义(过滤器、索引页面、表单等)的 ActiveAdmin 资源。您也可以使用 Rails STI 或仅使用 Rails 模型的子类,可能使用不同的 Rails default_scope,然后注册子类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 1970-01-01
        • 2013-10-16
        • 2012-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多