Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Rails plugin to handle the mapping of an uploaded CSV file to an expected format
Ruby
tree: 14a4d7bd0b

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
rails_app
spec
views/map_fields
.autotest
.gitignore
.rspec
Gemfile
Gemfile.lock
README.rdoc
Rakefile
VERSION
announcement.txt
init.rb
map_fields.gemspec

README.rdoc

map-fields

DESCRIPTION:

A Rails gem which provides a hook to preview and map the fields of an uploaded CSV file to a pre-defined schema

FEATURES/PROBLEMS:

  • Captures the post and provides an intermediate view where a user can map their data to the expected schema

  • Provides a default mapping view that can be customised

  • Allows the import to be part of a larger form (The form fields are remembered through the mapping)

  • Can only handle a single uploaded file at a time

INSTALLATION:

gem install map_fields

EXAMPLE:

TestController

class TestController < AppliactionController

  def new
    @list = List.new
  end

  def create
    map_fields :get_fields, params[:file] do
      @list = List.create(params[:list])
      @mapping.rows.each do |row|
        @list.contact.create(:title => row[:title], 
                             :first_name => row[:first_name], 
                             :last_name => row[:last_name])
      end
      flash[:notice] = 'Contact list created'
      redirect_to :action => :index
    end
  rescue MapFields::MissingFileError
    flash[:error] = 'Please upload a file'
    redirect_to :action => :new
  end

  private
  def get_fields
    ['Title', 'First name', 'Last name']
  end	
end

New view (new.html.erb)

<h1>Import a new List</h1>
<% form_for :list, :html => {:multipart => true} do |form| %>
  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name %>
  </div>
  <div class="field">
    <label for="file">File</label>
    <%= file_field_tag 'file' %>
  </div>
  <div class="buttons">
    <%= form.submit 'Import' %>
  </div>
<% end %>

Create view (create.html.erb)

<h1>Import a new List</h1>
<p>Please map the details you're importing</p>
=render :partial => 'map_fields/mapping'

REQUIREMENTS:

  • Rails 3.1

  • Ruby 1.9.3 (Not tested on 1.9.1 or 1.9.2

LICENSE:

(The MIT License)

Copyright © 2009

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.