Skip to content

Feature Request: Selectively do not update the position field in "sort" action #96

@awh-tokyo

Description

@awh-tokyo

My use case is that I would like a tree structure, but to have each level always sorted by name. I'd like to have the tree editor still work for easily setting parent/child relationships.

Currently I have to override the "sort" controller action so that it does not update the "position" field (which I have set to 'name'), or else all my objects get their name changed to numbers:

  # Override the one provided by ActiveAdminSortable, since we want it to ignore updates to the sort order
  collection_action :sort, :method => :post do
    resource_name = ActiveAdmin::SortableTree::Compatibility.normalized_resource_name(active_admin_config.resource_name)

    records = []
    params[resource_name].each_pair do |resource, parent_resource|
      parent_resource = resource_class.find(parent_resource) rescue nil
      records << [resource_class.find(resource), parent_resource]
    end

    errors = []
    ActiveRecord::Base.transaction do
      records.each_with_index do |(record, parent_record), position|
        # record.send "#{options[:sorting_attribute]}=", position
        if options[:tree]
          record.send "parent=", parent_record
        end
        errors << {record.id => record.errors} if !record.save
      end
    end
    if errors.empty?
      head 200
    else
      render json: errors, status: 422
    end
  end

Could we have a config option that allows us to disable the record.send "#{options[:sorting_attribute]}=", position ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions