Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lakehouseru opened this issue Apr 9, 2017 · 1 comment
Open

Comments

@lakehouseru
Copy link

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
Copy link
Owner

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants