ActiveAdmin: How to handle large associations

徘徊边缘 提交于 2019-12-19 03:14:36

问题


I'm building an interface for administrating organizations that can have lots of people connected. The total pool of people consists of a few thousand individuals.

To the best of my knowledge, AA doesn't really have a good system for this kind of situation.

So far I've used something like this in the form block to add/remove people from the organization:

f.has_many :person_organizations, for: [:person_organizations, f.object.person_organizations.active] do |connection_f|

  all_people = Person.select([:id, :firstname, :lastname]).order(:firstname, :lastname)

  connection_f.input :person, as: :select,
                              collection: all_people,
                              member_label: proc { |d| "#{d.firstname} #{d.lastname}"
  unless connection_f.object.nil?
    # Show the destroy checkbox only if it is an existing person
    # else, there's already dynamic JS to add / remove new dentists
    connection_f.input :_destroy, as: :boolean, label: 'Delete this connection'
  end
end

The problem with this is that after adding a few people to an organization, the time it takes to generate all the select boxes begins to be substantial as it has to do pretty much the same work for each element. See beneath ("slett denne koblingen" means "delete this connection")

Does anyone know of a way to alleviate this pain?

I've had a couple of thoughts, but I don't quite understand how I would implement them:

  • Only show a text string with the person's name after the association is set up, in stead of a select box. Need to still be able to delete the association, and create new ones, though.
  • Somehow cache the generated select box. Might give AA some problem with select-ing the correct value?

Also, I'm aware of a github issue discussing a solution to this kind of challenge, but it seems like it's still a way off, if it will ever be implemented: https://github.com/activeadmin/activeadmin/issues/2692#issuecomment-71500513


回答1:


I have encountered the same sluggishness. The challenge you are faced with is the number of populated selects that will get created. I recommend using some form of AJAX and Select2/Chosen combination to "autocomplete" a textbox input. This will greatly reduce the HTML footprint and speed load times. The user experience may be more appreciated by your admins too.

https://github.com/activeadmin/activeadmin/issues/1754

https://github.com/mfairburn/activeadmin-select2

Viget Labs has a gem that provides an alternate solution for handling the autocomplete association. https://github.com/vigetlabs/active_admin_associations




回答2:


Faced with same problem as you described I found your first assumption being the best.

Need to say, that I've tried this select2 stuff first, and it didn't helped out and even made things slower. That was because rails spending most of the time in that case not in querying database (otherwise select2 with ajax would help), but in "drawing" views. So when you ask rails to draw even more complex view it fails with a speed.

So I've done pretty simple stuff: on "has_many" part you've described I have:

f.inputs 'Personer' do
      f.has_many :person_organizations, for: [:person_organizations, f.object.person_organizations.active], new_record: false do |connection_f|
        if connection_f.object.person.present?
          li connection_f.object.person.name
          li link_to 'destroy', { controller: :person_organizations, action: :destroy, id: connection_f.object.id }, method: :delete, data: { confirm: 'Are you sure?' }
          li link_to 'edit', edit_admin_person_organization_path(connection_f.object)
        end
      end
    end

    li link_to 'Add new person-organization connection', new_admin_person_organization_path(id: f.object.id)

and in ActiveAdmin.register PersonOrganization file I have some custom redirections to redirect to "parent" model.

controller do
  def update
    update! do |format|
      format.html { redirect_to edit_admin_medical_practice_path(resource.organization) } if resource.valid?
    end
  end
end

This change mage my slow AA-page to be a bit faster.



来源:https://stackoverflow.com/questions/28195877/activeadmin-how-to-handle-large-associations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!