[英]Rails ActiveAdmin modify resource object
我目前有一個用戶對象,但為了避免冗余,我想把它包裝到一個名為MerchantUser / ProviderUser的presenter對象中。 但是,使用ActiveAdmin,我對如何執行此操作感到有些困惑。 我已經嘗試使用before_create將用戶更改為相應的演示者,但是在索引...中,我仍然看到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
你確定你想要/需要一個演示者嗎? 您可以使用不同的名稱和自定義(過濾器,索引頁,表單等)多次注冊相同的Rails模型作為ActiveAdmin資源。 您也可以使用Rails STI或僅使用Rails模型子類,可能使用不同的Rails default_scope,然后注冊子類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.