Sha256: b480eff5ccfdd6a1911811c927268e9127fef0ac5033916c6c91b9ac22abdbdc
Contents?: true
Size: 1.29 KB
Versions: 1
Compression:
Stored size: 1.29 KB
Contents
require 'active_support/concern' module SortableTreeController module Sort extend ActiveSupport::Concern included do helper SortableTreeHelper end module ClassMethods def sortable_tree(class_name, options = {}) define_method("sort") do resource_class = class_name.to_s.camelize.constantize # options options[:tree] = true options[:sorting_attribute] ||= 'pos' options[:parent_method] ||= 'parent' records = params[:cat].to_unsafe_h.inject({}) do |res, (resource, parent_resource)| res[resource_class.find(resource)] = resource_class.find(parent_resource) rescue nil res 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 "#{options[:parent_method]}=", 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 end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
sortable_tree_rails-0.0.8 | app/controllers/sortable_tree_controller.rb |