I've written an ActiveAdmin mixin, such that you can include it and call simple_menu_for Foo, to reduce duplicate code when working with simple models. Here is the main part, which will connect all others. It works, but code looks little bit ugly. Maybe there is an alternative to using the class_variable_set method?

In case of differences of starting time I used a class variable on the ActiveAdmin class, instead of an instance variable on the controller. Is there a better way to handle that?

module ActiveAdminSimpleMenu

def simple_menu_for(klass, options = {})
  ActiveAdmin.register klass do
    permit_params(*(klass.main_fields + (options[:permitted_params] || [])))
    controller.class_variable_set(:@@permitted_params, options[:permitted_params])
    controller.class_variable_set(:@@klass, klass)

    controller do
      def scoped_collection
        permitted_params = self.class.class_variable_get :@@permitted_params
        self.class.class_variable_get( :@@klass).includes(*(permitted_params.map(&:cut_id)))
    end if options[:permitted_params]

    index_for_main_fields klass
    filter_for_main_fields klass
    form_for_main_fields klass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.