ActiveAdmin.register JamRuby::User, :as => 'Referrals' do

  menu :label => 'Referrals', :parent => 'Affiliates'

  config.sort_order = 'created_at DESC'
  config.batch_actions = false
  config.clear_action_items!
  config.filters = true

  filter :affiliate_referral

  index do
    column 'User' do |oo| link_to(oo.name, oo.admin_url, {:title => oo.name}) end
    column 'Email' do |oo| oo.email end
    column 'Created' do |oo| oo.created_at end
    column 'Partner' do |oo| oo.affiliate_referral.display_name end
  end

  controller do

    def scoped_collection
      rel = end_of_association_chain
        .includes([:affiliate_referral])
        .order('created_at DESC')
      if (ref_id = params[AffiliatePartner::PARAM_REFERRAL]).present?
        qq = ['affiliate_referral_id = ?', ref_id]
      else
        qq = ['affiliate_referral_id IS NOT NULL']
      end
      @users ||= rel.where(qq)
    end

  end
end
