Importing and converting CSV data into a Rails model with a slick Active Admin interface

Avatar-eric-london
Created by Eric.London on 2012-06-27
Tags:
New Comment
 
Please note: the content on this page orginates from ericlondon.com.
I recently exported a few data sources to CSV and thought it would interesting to migrate the data into a Ruby on Rails model and use Active Admin to create a slick admin interface. Here is the skeletal code I used.

Part 1, Rails setup


# create directory for Rails project
$ mkdir csvimport

# create new rvm gemset
$ echo "rvm use --create ruby-1.9.2@csvimport" > csvimport/.rvmrc

# enter directory
$ cd csvimport

# add rails gem
$ gem install rails

# create new Rails app
$ rails new .

# git integration
# I generally commit after every action, but I'll leave that out per sake of clutter
$ git init
$ git add .
$ git commit -am "new rails project"

# setup database
$ rake db:migrate


Part 2, Model creation


# add acts as taggable on gem, for tagging (clearly optional)
# edit file: Gemfile
+gem 'acts-as-taggable-on'

# execute bundle to install gems
$ bundle

# create Post model
$ rails generate model Post title:string content:text

# add taggable property to Post model & make mass-assignable
# file: app/models/post.rb
 class Post < ActiveRecord::Base
-  attr_accessible :content, :title
+  attr_accessible :content, :title, :tag_list
+  acts_as_taggable_on :tags
 end

# run acts as taggable migration
$ rails generate acts_as_taggable_on:migration

# setup database
$ rake db:migrate


Part 3, Active Admin integration


# add active admin gem
# edit file: Gemfile
+# active admin
+gem 'activeadmin'
+gem 'sass-rails', '~> 3.2.3'
+gem 'meta_search'
+
 # Gems used only for assets and not required
 # in production environments by default.
 group :assets do
-  gem 'sass-rails',   '~> 3.2.3'
+  #gem 'sass-rails',   '~> 3.2.3'

# execute bundle to install gems
$ bundle

# run active admin generator, and setup database
$ rails generate active_admin:install
$ rake db:migrate

# register Post model with active admin
$ rails generate active_admin:resource Post


Part 4, Active Admin + acts-as-taggable-on integration

In this section, I (optionally) show how you might alter the active admin post file to integrate with acts-as-taggable-on for tagging.


# edit file: app/admin/post.rb:
ActiveAdmin.register Post do

  # filters
  filter :title
  filter :content
  filter :created_at
  filter :updated_at
  filter :taggings_tag_name, :as => :check_boxes, :collection => proc { Post.tag_counts.map{|t| t.name} }

  # column list
  index do
    column :id
    column :title
    column :content
    column :tag_list
    column :created_at
    column :updated_at
    default_actions
  end

  # form
  form do |f|
    f.inputs "Post" do
      f.input :title
      f.input :content
      f.input :tag_list
    end
    f.buttons
  end

end


Next, I executed some code to set the root (home) route to the active admin dashboard, removed the default public index.html file, and updated the admin user.


# set root route to active admin dashboard
# file: config/routes.rb
+  root :to => 'admin/dashboard#index'

# clean up, remove public facing index.html file
$ rm public/index.html

# update default/admin user, or create a new user via console
$ rails c
> u = AdminUser.find(1)
> u.email = 'youremailaddress'
> u.password = 'yourpassword'
> u.save
> exit


Part 5, Rake task to import CSV data


# create a rake task to import the CSV data
# new file: lib/tasks/import_csv_data.rake

namespace :csvimport do

  desc "Import CSV Data."
  task :import_csv_data => :environment do

    require 'csv'

    csv_file_path = '/path/to/your/file.csv'

    CSV.foreach(csv_file_path) do |row|

      p = Post.create!({
          :title => row[0],
          :content => row[1],
          :tag_list => row[2].split('|'),
        }
      )

    end

  end

end


Lastly, I ran the rake task to import the CSV data into the Rails model. And then started the WEBrick server.


# run import task
rake csvimport:import_csv_data

# start server
rails s


Here is a screenshot of the CSV data file I imported, and below, the active admin interface that was created:

Test-data-csv

Active admin csv