我目前有一个用户对象,但为了避免冗余,我想将其包装到名为MerchantUser / ProviderUser的演示者对象中。但是,使用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