views:

34

answers:

0

Hi!

I'm trying to create a dynamic interface. Where my model classes exist and my controllers are dynamically created when launching the application.

Everything happens in my routes file where the resources are created!

ActionController::Routing::Routes.draw do |map|
  map.namespace :admin do |admin|
    TestAdmin.models.each do |m|
      admin.resources m.to_s.tableize.to_sym
    end
  end
end

And then there is my BeAdmin class, this does the following:

module TestAdmin
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def beadmin(options = {})
      namespace_name = "Admin"
      class_name = "#{self.to_s.pluralize.capitalize}Controller"
      klass = namespace_name.constantize.const_set(class_name, Class.new(ApplicationController))
      klass.module_eval do

        def index
          render :text => "test"
        end
      end
    end
  end

  def self.models
    all_models = []
    Dir.chdir(File.join(Rails.root, "app/models")) do
      Dir["**/*.rb"].each do |m|
        class_name = m.sub(/\.rb$/,"").camelize
        klass = class_name.split("::").inject(Object){ |klass,part| klass.const_get(part) }
        all_models << "#{class_name}" if klass < ActiveRecord::Base && !klass.abstract_class?
      end
    end
    all_models
  end
end

And when you now browse to the /admin/users (from the User model) then you get to see "test". so it works great!

But then I just do a simple refresh of the browser and The controller that is called becomes UsersController#index instead of Admin::UsersController#index... He loses it's namespace for some reason...

Maybe another important aspect here is that I added all this as a plugin and user Rails Engines so I can make a plug-able interface...

But so far no luck because my routes seem to get lost somewhere!

Thanks in advance for your help!

Jelle