Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use modules to split very long code in active admin?

Tags:

activeadmin

Activeadmin registers a page on a single file, in which it has all the logic: Index, Show, Edit, etc.

I would like to split, let's say, task.rb into task_index.rb, task_show.rb, task_edit.rb, etc.

So, how should you do that?

NOTE: I know that making an ActiveAdmin.register block in each file (it appends if Task exists) will do the work, but this question aims for a general approach rather than solving this specific inquiry.

-- admin/task.rb
#encoding: utf-8

ActiveAdmin.register Task do
 [Lot's of actions]
  member_action....
  member_action....
  member_action....
  batch_action....

 [Index stuff]
  filter....
  scope....
  scope....
  scope.... 
  index do
   column...
   column...
   column...
   column...
  end

 [Edit stuff]
  form do |f|
    f.input....
    f.input....
    f.input....
    f.input....
    f.input....
  end
 [etc etc etc]
end
----------------

I'm thinking of modules, but I can't figure out how to.

like image 512
Carlos Troncoso Avatar asked Oct 05 '22 10:10

Carlos Troncoso


1 Answers

That is how I do this

module source

module ResourceDSL
  module ActsAsClone

    def acts_as_clone
      controller do
        def new
          instance_variable_name = active_admin_config.resource_class.to_s.underscore
          resource = active_admin_config.resource_class.find(params[:id]) rescue nil
          attrs = resource.nil? ? {} : resource.attributes
          resource = active_admin_config.resource_class.new(attrs)
          instance_variable_set("@#{instance_variable_name}", resource)
        end
      end

      action_item :only => [:show, :edit] do
        if can? :create, resource and (!resource.respond_to?(:live?) or resource.live?)
          link_to "Copy", :action => :new, :id => resource.id
        end
      end
    end
  end
end

including to ActiveAdmin::ResourceDSL

ActiveAdmin::ResourceDSL.send :include, ResourceDSL::ActsAsClone

And then you can

ActiveAdmin.register Account do
  menu :parent => "Billing",  :priority => 10
  acts_as_clone
end
like image 104
Fivell Avatar answered Oct 10 '22 02:10

Fivell