Sha256: b4f6ec794a1fec71b3553ca84d76d816b73584da13e875e9873ab2fdc895fc30

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

module ActiveAdminAssociations
  module AssociationActions
    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aa_associations-0.1.2 lib/active_admin_associations/association_actions.rb
aa_associations-0.1.1 lib/active_admin_associations/association_actions.rb
aa_associations-0.1.0 lib/active_admin_associations/association_actions.rb