Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Latest changes (should be committing more often)

  • Loading branch information...
commit 8fb6e32d9454dbaf45cb2ff40161c91b3bac5710 1 parent 4aa9084
@andrewtimberlake andrewtimberlake authored
View
2  .gitignore
@@ -1,2 +1,4 @@
pkg/
tmp/
+coverage/
+map_fields*.gem
View
3  .rspec
@@ -1 +1,2 @@
---color
+--color
+--order rand
View
30 Gemfile.lock
@@ -23,9 +23,9 @@ GEM
erubis (2.7.0)
hike (1.2.1)
i18n (0.6.0)
- json (1.6.3)
+ json (1.6.4)
multi_json (1.0.4)
- rack (1.3.5)
+ rack (1.3.6)
rack-cache (1.1)
rack (>= 0.4)
rack-mount (0.8.3)
@@ -42,21 +42,21 @@ GEM
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2.2)
- rdoc (3.11)
+ rdoc (3.12)
json (~> 1.4)
- rspec (2.7.0)
- rspec-core (~> 2.7.0)
- rspec-expectations (~> 2.7.0)
- rspec-mocks (~> 2.7.0)
- rspec-core (2.7.1)
- rspec-expectations (2.7.0)
+ rspec (2.8.0)
+ rspec-core (~> 2.8.0)
+ rspec-expectations (~> 2.8.0)
+ rspec-mocks (~> 2.8.0)
+ rspec-core (2.8.0)
+ rspec-expectations (2.8.0)
diff-lcs (~> 1.1.2)
- rspec-mocks (2.7.0)
- rspec-rails (2.7.0)
- actionpack (~> 3.0)
- activesupport (~> 3.0)
- railties (~> 3.0)
- rspec (~> 2.7.0)
+ rspec-mocks (2.8.0)
+ rspec-rails (2.8.1)
+ actionpack (>= 3.0)
+ activesupport (>= 3.0)
+ railties (>= 3.0)
+ rspec (~> 2.8.0)
simplecov (0.5.4)
multi_json (~> 1.0.3)
simplecov-html (~> 0.5.3)
View
2  README.rdoc
@@ -15,7 +15,7 @@ A Rails gem which provides a hook to preview and map the fields of an uploaded C
== INSTALLATION:
-gem install map-fields
+gem install map_fields
== EXAMPLE:
View
4 lib/map_fields/controller.rb
@@ -3,11 +3,11 @@
module MapFields
module Controller
def map_fields(fields, file, &block)
+ append_view_path File.expand_path('../../../views/', __FILE__)
@mapper = Mapper.new(self, fields, file)
if @mapper.mapped?
- block.call
+ block.call(@mapper)
else
- append_view_path File.expand_path('../../../views/', __FILE__)
render
end
end
View
52 lib/map_fields/mapper.rb
@@ -8,6 +8,7 @@ class MissingFileError < StandardError; end
class Mapper
def initialize(controller, fields, file)
+ @controller = controller
params = controller.params
@fields = get_fields(controller, fields)
@params = ParamsParser.parse(params)
@@ -18,24 +19,51 @@ def initialize(controller, fields, file)
else
raise MissingFileError unless controller.session[:map_fields_file] && File.exist?(controller.session[:map_fields_file])
@mapped = true
- @rows = map_fields(controller, params.delete(:mapped_fields), fields)
+ @rows = map_fields(controller, params.delete(:mapped_fields), @fields)
end
end
- attr_reader :rows, :fields, :params
+ attr_reader :rows, :fields, :params, :mapping, :ignore_first_row
+
+ def error!
+ @mapped = false
+ @rows = parse_first_few_lines @controller.session[:map_fields_file]
+ end
def mapped?
@mapped
end
- private
- def parse_params(params)
- params = params.except(:controller, :action)
+ def selected_mapping(column)
+ @mapping ? @mapping.selected_mapping(column) : nil
+ end
+
+ def is_mapped?(key)
+ @mapping ? @mapping.is_mapped?(key) : false
+ end
+
+ def fields_for_select
+ result = []
+ fields.each_with_index { |i,e| result << [i, e] }
+ result
end
+ def each(&block)
+ @rows.each &block
+ end
+
+ def [](key)
+ @mapping[key]
+ end
+
+ def file
+ @controller.session[:map_fields_file]
+ end
+
+ private
def map_fields(controller, mapped_fields, fields)
- ignore_first_row = mapped_fields.delete(:ignore_first_row)
- mapping = Mapping.new(mapped_fields, fields)
- CSVReader.new(controller.session[:map_fields_file], mapping, ignore_first_row)
+ @ignore_first_row = !!mapped_fields.delete(:ignore_first_row)
+ @mapping = Mapping.new(mapped_fields, fields)
+ CSVReader.new(controller.session[:map_fields_file], @mapping, @ignore_first_row)
end
def get_fields(controller, fields)
@@ -49,10 +77,12 @@ def get_fields(controller, fields)
end
def save_file(controller, file)
- Tempfile.open(['map_fields', '.csv']) do |tmpfile|
+ # path = Tempfile.new(['map_fields', '.csv']).path
+ path = File.join(Dir::tmpdir, "map_fields_#{Time.now.to_i}_#{$$}")
+ File.open(path, 'wb') do |tmpfile|
tmpfile.write file.read
- controller.session[:map_fields_file] = tmpfile.path
- tmpfile.path
+ controller.session[:map_fields_file] = path
+ path
end
end
View
13 lib/map_fields/mapping.rb
@@ -5,8 +5,9 @@ def initialize(mapping, fields)
@fields = fields
@mapping = mapping.each_with_object({}){ |arr, hash|
- key = arr[0].to_i
- value = arr[1].to_i
+ key = arr[1].blank? ? nil : arr[1].to_i
+ next if key.nil?
+ value = arr[0].blank? ? nil : arr[0].to_i
hash[key] = value
hash[@fields[key]] = value
@@ -18,6 +19,14 @@ def [](index)
@mapping[index]
end
+ def is_mapped?(key)
+ @mapping.has_key?(key)
+ end
+
+ def selected_mapping(column)
+ @mapping.find{|k, v| return k if v == column && k.is_a?(Numeric) }
+ end
+
private
def field_to_symbol(field)
field.to_s.downcase.gsub(/[^a-z0-9]+/, '_').to_sym
View
1,340 log/test.log
0 additions, 1,340 deletions not shown
View
31 map_fields.gemspec
@@ -1,11 +1,6 @@
-# Generated by jeweler
-# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
-# -*- encoding: utf-8 -*-
-
Gem::Specification.new do |s|
s.name = %q{map_fields}
- s.version = "1.0.4"
+ s.version = "2.0.0.beta2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Andrew Timberlake"]
@@ -15,30 +10,13 @@ Gem::Specification.new do |s|
"README.rdoc"
]
s.files = [
- ".autotest",
"README.rdoc",
- "Rakefile",
- "VERSION",
- "announcement.txt",
- "init.rb",
- "lib/map_fields.rb",
- "spec/application_controller.rb",
- "spec/controllers/test_controller_spec.rb",
- "spec/lib/params_parser_spec.rb",
- "spec/spec_helper.rb",
- "spec/test-file.csv",
- "views/map_fields/_map_fields.erb"
- ]
+ ] + Dir['lib/**/*.rb'] + Dir['views/**/*.erb']
s.homepage = %q{http://github.com/internuity/map-fields}
s.require_paths = ["lib"]
s.rubygems_version = %q{1.5.2}
- s.summary = %q{Rails plugin to allow a user to map the fields of a CSV to an expected list of fields}
- s.test_files = [
- "spec/application_controller.rb",
- "spec/controllers/test_controller_spec.rb",
- "spec/lib/params_parser_spec.rb",
- "spec/spec_helper.rb"
- ]
+ s.summary = %q{Rails gem to allow a user to map the fields of a CSV to an expected list of fields}
+ s.test_files = Dir['spec/**']
if s.respond_to? :specification_version then
s.specification_version = 3
@@ -49,4 +27,3 @@ Gem::Specification.new do |s|
else
end
end
-
View
47 spec/lib/map_fields/mapper_spec.rb
@@ -1,4 +1,5 @@
require 'map_fields/mapper'
+require 'active_support/all'
module MapFields
describe Mapper do
@@ -55,11 +56,51 @@ module MapFields
end
end
+ context "#error!" do
+ it "reloads the file preview for cases when an error is detected within the controller and the mapping form should be shown again" do
+ mapper = Mapper.new(controller, [], file)
+ mapper.error!
+ mapper.should_not be_mapped
+ end
+ end
+
+ context "#is_mapped?" do
+ it "returns false if no mapping has taken place" do
+ mapper = Mapper.new(controller, [], file)
+ mapper.is_mapped?(:field_1).should be_false
+ end
+
+ it "returns false if the field has not been mapped" do
+ fields = %w(Email Lastname Title Firstname)
+ controller.stub(:session => {map_fields_file: File.expand_path('spec/files/test.csv')})
+ controller.should_receive(:params).and_return(mapped_fields: {:ignore_first_row => '1', '3' => '0', '2' => '1', '0' => '2', '1' => '3'})
+ mapper = Mapper.new(controller, fields, nil)
+ mapper.is_mapped?(:unkown).should be_false
+ end
+
+ it "returns true if the field has not been mapped" do
+ fields = %w(Email Lastname Title Firstname)
+ controller.stub(:session => {map_fields_file: File.expand_path('spec/files/test.csv')})
+ controller.should_receive(:params).and_return(mapped_fields: {:ignore_first_row => '1', '3' => '0', '2' => '1', '0' => '2', '1' => '3'})
+ mapper = Mapper.new(controller, fields, nil)
+ mapper.is_mapped?(:email).should be_true
+ end
+ end
+
+ context "#file" do
+ it "returns the file that was uploaded" do
+ file = File.expand_path('spec/files/test.csv')
+ controller.stub(:session => {map_fields_file: file})
+ mapper = Mapper.new(controller, fields, nil)
+ mapper.file.should == file
+ end
+ end
+
context "mapping fields" do
it "decorates the CSV reader to allow easy access to rows by field" do
fields = %w(Email Lastname Title Firstname)
controller.stub(:session => {map_fields_file: File.expand_path('spec/files/test.csv')})
- controller.should_receive(:params).and_return(mapped_fields: {:ignore_first_row => true, '0' => '3', '1' => '2', '2' => '0', '3' => '1'})
+ controller.should_receive(:params).and_return(mapped_fields: {:ignore_first_row => '1', '3' => '0', '2' => '1', '0' => '2', '1' => '3'})
mapper = Mapper.new(controller, fields, nil)
mapper.rows[0]['Title'].should == 'Mr'
end
@@ -67,9 +108,9 @@ module MapFields
it "supports the each syntax" do
fields = %w(Email Lastname Title Firstname)
controller.stub(:session => {map_fields_file: File.expand_path('spec/files/test.csv')})
- controller.should_receive(:params).and_return(mapped_fields: {:ignore_first_row => true, '0' => '3', '1' => '2', '2' => '0', '3' => '1'})
+ controller.should_receive(:params).and_return(mapped_fields: {:ignore_first_row => true, '3' => '0', '2' => '1', '0' => '2', '1' => '3'})
mapper = Mapper.new(controller, fields, nil)
- mapper.rows.each do |row|
+ mapper.each do |row|
row[:title].should == 'Mr'
break
end
View
15 spec/lib/map_fields/mapping_spec.rb
@@ -1,9 +1,10 @@
require 'map_fields/mapping'
+require 'active_support/all'
module MapFields
describe Mapping do
let(:fields) { ['Field 1', 'Field 2', 'Field 3'] }
- let(:mapping) { Mapping.new({'0' => '2', '1' => '0', '2' => '1'}, fields) }
+ let(:mapping) { Mapping.new({'2' => '0', '0' => '1', '1' => '2'}, fields) }
it "indexes by field name" do
mapping['Field 1'].should == 2
@@ -16,5 +17,17 @@ module MapFields
it "indexes by mapped index" do
mapping[2].should == 1
end
+
+ it "tells if a field has been mapped" do
+ mapping.is_mapped?(:field_1).should be_true
+ end
+
+ it "tells if a field has not been mapped" do
+ mapping.is_mapped?(:field_4).should be_false
+ end
+
+ it "knows the mapping for a specified column" do
+ mapping.selected_mapping(0).should == 1
+ end
end
end
View
4 spec/spec_helper.rb
@@ -6,6 +6,10 @@
require File.expand_path('../../rails_app/config/environment.rb', __FILE__)
require 'rails/test_help'
require 'rspec/rails'
+
+require 'simplecov'
+SimpleCov.start
+
require 'map_fields'
RailsApp::Application.routes.draw do
View
6 views/map_fields/_mapping.erb
@@ -6,8 +6,8 @@
<table cellspacing="0">
<thead>
<tr>
- <% (1..@mapper.rows[0].size).each do |c| -%>
- <th><%= select_tag "fields[#{c}]", options_for_select(@mapper.fields), :include_blank => true, :class => 'field_options' %></th>
+ <% (0..@mapper.rows[0].size-1).each do |c| -%>
+ <th><%= select_tag "mapped_fields[#{c}]", options_for_select(@mapper.fields_for_select, @mapper.selected_mapping(c)), :include_blank => true, :class => 'field_options' %></th>
<% end -%>
</tr>
</thead>
@@ -23,7 +23,7 @@
</table>
</div>
<div class="option">
- <%= check_box_tag 'ignore_first_row', '1', true, :id => 'ignore_first_row_option' %><label for="ignore_first_row_option">Ignore the first row (headings)</label>
+ <%= check_box_tag 'mapped_fields[ignore_first_row]', '1', true, :id => 'ignore_first_row_option' %><label for="ignore_first_row_option">Ignore the first row (headings)</label>
</div>
<div class="action">
<%= submit_tag 'Import' %>
Please sign in to comment.
Something went wrong with that request. Please try again.