Module: ActiveAdminAssociations::AssociationActions

Defined in:
lib/active_admin_associations/association_actions.rb

Instance Method Summary collapse

Instance Method Details

#association_actionsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_admin_associations/association_actions.rb', line 3

def association_actions
  member_action :unrelate, :method => :put do
    reflection = resource_class.reflect_on_association(params[:relationship_name].to_sym)
    if reflection.collection?
      related_record = reflection.klass.find(params[:related_id])
      resource.send(params[:relationship_name]).delete(related_record)
    else
      resource.update_attribute("#{params[:relationship_name]}_id", nil)
    end
    flash[:notice] = "The recored has been unrelated."
    redirect_to request.headers["Referer"].presence || admin_dashboard_url
  end

  member_action :relate, :method => :put do
    reflection = resource_class.reflect_on_association(params[:relationship_name].to_sym)
    if reflection.collection?
      record_to_relate = reflection.klass.find(params[:related_id])
      resource.send(params[:relationship_name]) << record_to_relate
    else
      resource.update_attribute("#{params[:relationship_name]}_id", record_to_relate)
    end
    flash[:notice] = "The recored has been related."
    redirect_to request.headers["Referer"].presence || admin_dashboard_url
  end

  member_action :page_related, :method => :get do
    relationship_name = params[:relationship_name].to_sym
    association_config = active_admin_config.form_associations[relationship_name]
    relationship_class = resource_class.reflect_on_association(relationship_name).klass
    association_columns = association_config.fields.presence || relationship_class.content_columns
    render :partial => 'admin/shared/collection_table', :locals => {
      :object             => resource,
      :collection         => resource.send(relationship_name).page(params[:page]),
      :relationship       => relationship_name,
      :columns            => association_columns,
      :relationship_class => relationship_class
    }
  end
end