the-teacher / the_sortable_tree

Nested Set + Drag&Drop GUI. Very fast! Best render helper! 2000 nodes/sec. Ready for rails 4
MIT License
558 stars 127 forks source link

Rebuild method can't work with namespaced controllers and models #91

Open lakehouseru opened 7 years ago

lakehouseru commented 7 years ago

If I have namespaced controller and model

module Stock
  class CategoriesController < ApplicationController
    include ::TheSortableTreeController::Rebuild

class Stock::Category < ActiveRecord::Base
  acts_as_nested_set 
  include ::TheSortableTree::Scopes

This method form gem won't work

module DefineVariablesMethod
    public
    def the_define_common_variables
      collection = self.class.to_s.split(':').last.sub(/Controller/,'').underscore.downcase                 # 'recipes'
      collection = self.respond_to?(:sortable_collection) ? self.sortable_collection : collection           # 'recipes'
      variable   = collection.singularize                                                                   # 'recipe'
      klass      = self.respond_to?(:sortable_model) ? self.sortable_model : variable.classify.constantize  #  Recipe
      ["@#{variable}", collection, klass]
    end
  end

Because this line

collection = self.class.to_s.split(':').last.sub(/Controller/,'').underscore.downcase

Will result "categories"

And here klass = self.respond_to?(:sortable_model) ? self.sortable_model : variable.classify.constantize

you will get error

*** NameError Exception: uninitialized constant Category

Because the class is not Category but Stock::Category

the-teacher commented 7 years ago

@lakehouseru Hey, Hello! Could you please check this solution? I think it will help you

https://github.com/the-teacher/the_sortable_tree/blob/master/spec/dummy_app/app/controllers/admin/pages_controller.rb#L24