Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Admin - upload a CSV to create instances of a model

I have a straight forward model in my Ruby on Rails application that is also available in my rails_admin instance. Users of rails_admin will need to come in daily and upload a CSV or XLS file and based on some logic which I'll render on the backend, that will create instances of the model based on what data is in the CSV/XLS. I don't need to persist the CSV or XLS on the filesystem, so that's not the problem. The issue is, I'm not entirely sure how to get an interface going in rails_admin where a user can upload a CSV/XLS, hit upload, and the backend should take care of the rest.

Does rails_admin have support for this? Can I create an interface through it where I can upload files for processing by one of my models?

like image 354
randombits Avatar asked Oct 29 '13 02:10

randombits


1 Answers

It looks like you might have to create a custom action and view. One way to do that would be to use this custom actions plugin. There is also a tutorial about how to build a custom action here. I also used SmarterCSV, and it works brilliantly.

To register a custom action with Rails Admin, you would do this in config/initializers/rails_admin.rb :

module RailsAdmin
  module Config
    module Actions
      class YourClass < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)

         ##code here, as explained more below

      end
    end
  end
end

In this class, you can inherit any of the base actions. So to register a custom partial, in that class you would do:

    # View partial name (called in default :controller block)
    register_instance_option :template_name do
      :your_class
    end

Your _your_class partials must be in app/views/rails_admin/main/, you can handle the form with multipart.. I'm not including the partial code, if you want me to take a swing at it let me know.

You will probably also want your action on the model scope:

    register_instance_option :collection? do
      true
    end

And put your controller code in. It would probably be best to handle the processing here, for example:

register_instance_option :controller do
      Proc.new do

        @order = Order.import(params[:file])
        f = SmarterCSV.process(file.tempfile)
              f.each do |r|

               #combine date and time fields 
               r[:date_time] = [r[:date],r[:time]].join(' ')

                Order.create("date" => r[:date_time])
        end
      end
    end

Next, your action should be registered with RailsAdmin::Config::Actions like this (This code was placed into config/initializers/rails_admin.rb):

module RailsAdmin
  module Config
    module Actions
      class ApproveReview < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)
      end
    end
  end
end

Next, the custom action needs to be listed in the actions config in config/initializers/rails_admin.rb:

RailsAdmin.config do |config|
  config.actions do
    dashboard
    index
    new

    your_class

    show
    edit
    delete
  end
end

There are more details in the tutorial, but I think that should be a pretty solid start!

like image 146
Jordan Davis Avatar answered Sep 27 '22 17:09

Jordan Davis