Rails ActiveAdmin modifies resource object

I currently have a custom object, but to avoid redundancy, I would like to wrap it in a presenter object called MerchantUser / ProviderUser. However, with ActiveAdmin, I am a little confused on how to do this. I tried to use before_create to change the user in the respective presenters, but in the ... do index, I can still see that user.class is User, not the wrapper classes I have defined.

I looked at scoping_collection, but unfortunately this only works with collections and not individual objects?

 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

      

+3


source to share


2 answers


Have you looked into Active Admin support for decorators? This page is fairly complete. The best way to implement them depends on how your decorator / presenter object is implemented.



Link: use decorate_with

or see this gem for PORO support

+2


source


Do you really want / need a presenter? You can register the same Rails model multiple times as ActiveAdmin resources with different names and settings (filters, index page, forms, etc.). You can also use Rails STI or just Rails subclasses, perhaps with different Rails default_scope, and then register the subclasses.



0


source







All Articles