map_fields 2.0.0.beta → 2.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/map_fields/controller.rb +2 -2
- data/lib/map_fields/mapper.rb +37 -11
- data/lib/map_fields/mapping.rb +11 -2
- data/spec/spec_helper.rb +4 -0
- data/views/map_fields/_mapping.erb +31 -0
- metadata +3 -1
@@ -3,11 +3,11 @@ require 'map_fields/mapper'
|
|
3
3
|
module MapFields
|
4
4
|
module Controller
|
5
5
|
def map_fields(fields, file, &block)
|
6
|
+
append_view_path File.expand_path('../../../views/', __FILE__)
|
6
7
|
@mapper = Mapper.new(self, fields, file)
|
7
8
|
if @mapper.mapped?
|
8
|
-
block.call
|
9
|
+
block.call(@mapper)
|
9
10
|
else
|
10
|
-
append_view_path File.expand_path('../../../views/', __FILE__)
|
11
11
|
render
|
12
12
|
end
|
13
13
|
end
|
data/lib/map_fields/mapper.rb
CHANGED
@@ -8,6 +8,7 @@ module MapFields
|
|
8
8
|
|
9
9
|
class Mapper
|
10
10
|
def initialize(controller, fields, file)
|
11
|
+
@controller = controller
|
11
12
|
params = controller.params
|
12
13
|
@fields = get_fields(controller, fields)
|
13
14
|
@params = ParamsParser.parse(params)
|
@@ -18,24 +19,47 @@ module MapFields
|
|
18
19
|
else
|
19
20
|
raise MissingFileError unless controller.session[:map_fields_file] && File.exist?(controller.session[:map_fields_file])
|
20
21
|
@mapped = true
|
21
|
-
@rows = map_fields(controller, params.delete(:mapped_fields), fields)
|
22
|
+
@rows = map_fields(controller, params.delete(:mapped_fields), @fields)
|
22
23
|
end
|
23
24
|
end
|
24
|
-
attr_reader :rows, :fields, :params
|
25
|
+
attr_reader :rows, :fields, :params, :mapping, :ignore_first_row
|
26
|
+
|
27
|
+
def error!
|
28
|
+
@mapped = false
|
29
|
+
@rows = parse_first_few_lines @controller.session[:map_fields_file]
|
30
|
+
end
|
25
31
|
|
26
32
|
def mapped?
|
27
33
|
@mapped
|
28
34
|
end
|
29
35
|
|
30
|
-
|
31
|
-
|
32
|
-
|
36
|
+
def selected_mapping(column)
|
37
|
+
@mapping ? @mapping.selected_mapping(column) : nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def is_mapped?(key)
|
41
|
+
@mapping ? @mapping.is_mapped?(key) : false
|
42
|
+
end
|
43
|
+
|
44
|
+
def fields_for_select
|
45
|
+
result = []
|
46
|
+
fields.each_with_index { |i,e| result << [i, e] }
|
47
|
+
result
|
33
48
|
end
|
34
49
|
|
50
|
+
def each(&block)
|
51
|
+
@rows.each &block
|
52
|
+
end
|
53
|
+
|
54
|
+
def file
|
55
|
+
@controller.session[:map_fields_file]
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
35
59
|
def map_fields(controller, mapped_fields, fields)
|
36
|
-
ignore_first_row = mapped_fields.delete(:ignore_first_row)
|
37
|
-
mapping = Mapping.new(mapped_fields, fields)
|
38
|
-
CSVReader.new(controller.session[:map_fields_file], mapping, ignore_first_row)
|
60
|
+
@ignore_first_row = !!mapped_fields.delete(:ignore_first_row)
|
61
|
+
@mapping = Mapping.new(mapped_fields, fields)
|
62
|
+
CSVReader.new(controller.session[:map_fields_file], @mapping, @ignore_first_row)
|
39
63
|
end
|
40
64
|
|
41
65
|
def get_fields(controller, fields)
|
@@ -49,10 +73,12 @@ module MapFields
|
|
49
73
|
end
|
50
74
|
|
51
75
|
def save_file(controller, file)
|
52
|
-
Tempfile.
|
76
|
+
# path = Tempfile.new(['map_fields', '.csv']).path
|
77
|
+
path = File.join(Dir::tmpdir, "map_fields_#{Time.now.to_i}_#{$$}")
|
78
|
+
File.open(path, 'wb') do |tmpfile|
|
53
79
|
tmpfile.write file.read
|
54
|
-
controller.session[:map_fields_file] =
|
55
|
-
|
80
|
+
controller.session[:map_fields_file] = path
|
81
|
+
path
|
56
82
|
end
|
57
83
|
end
|
58
84
|
|
data/lib/map_fields/mapping.rb
CHANGED
@@ -5,8 +5,9 @@ module MapFields
|
|
5
5
|
@fields = fields
|
6
6
|
|
7
7
|
@mapping = mapping.each_with_object({}){ |arr, hash|
|
8
|
-
key = arr[
|
9
|
-
|
8
|
+
key = arr[1].blank? ? nil : arr[1].to_i
|
9
|
+
next if key.nil?
|
10
|
+
value = arr[0].blank? ? nil : arr[0].to_i
|
10
11
|
|
11
12
|
hash[key] = value
|
12
13
|
hash[@fields[key]] = value
|
@@ -18,6 +19,14 @@ module MapFields
|
|
18
19
|
@mapping[index]
|
19
20
|
end
|
20
21
|
|
22
|
+
def is_mapped?(key)
|
23
|
+
@mapping.has_key?(key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def selected_mapping(column)
|
27
|
+
@mapping.find{|k, v| return k if v == column && k.is_a?(Numeric) }
|
28
|
+
end
|
29
|
+
|
21
30
|
private
|
22
31
|
def field_to_symbol(field)
|
23
32
|
field.to_s.downcase.gsub(/[^a-z0-9]+/, '_').to_sym
|
data/spec/spec_helper.rb
CHANGED
@@ -6,6 +6,10 @@ require 'bundler'
|
|
6
6
|
require File.expand_path('../../rails_app/config/environment.rb', __FILE__)
|
7
7
|
require 'rails/test_help'
|
8
8
|
require 'rspec/rails'
|
9
|
+
|
10
|
+
require 'simplecov'
|
11
|
+
SimpleCov.start
|
12
|
+
|
9
13
|
require 'map_fields'
|
10
14
|
|
11
15
|
RailsApp::Application.routes.draw do
|
@@ -0,0 +1,31 @@
|
|
1
|
+
<%= form_tag nil, :id => 'map_fields_form', :method => :post do -%>
|
2
|
+
<% @mapper.params.each do |arr| -%>
|
3
|
+
<%= hidden_field_tag arr[0], arr[1] %>
|
4
|
+
<% end -%>
|
5
|
+
<div class="map_fields">
|
6
|
+
<table cellspacing="0">
|
7
|
+
<thead>
|
8
|
+
<tr>
|
9
|
+
<% (0..@mapper.rows[0].size-1).each do |c| -%>
|
10
|
+
<th><%= select_tag "mapped_fields[#{c}]", options_for_select(@mapper.fields_for_select, @mapper.selected_mapping(c)), :include_blank => true, :class => 'field_options' %></th>
|
11
|
+
<% end -%>
|
12
|
+
</tr>
|
13
|
+
</thead>
|
14
|
+
<tbody>
|
15
|
+
<% @mapper.rows.each do |row| -%>
|
16
|
+
<tr>
|
17
|
+
<% row.each do |column| -%>
|
18
|
+
<td><%= h(column) %></td>
|
19
|
+
<% end -%>
|
20
|
+
</tr>
|
21
|
+
<% end -%>
|
22
|
+
</tbody>
|
23
|
+
</table>
|
24
|
+
</div>
|
25
|
+
<div class="option">
|
26
|
+
<%= 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>
|
27
|
+
</div>
|
28
|
+
<div class="action">
|
29
|
+
<%= submit_tag 'Import' %>
|
30
|
+
</div>
|
31
|
+
<% end -%>
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: map_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0.
|
4
|
+
version: 2.0.0.beta2
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -25,6 +25,7 @@ files:
|
|
25
25
|
- lib/map_fields/params_parser.rb
|
26
26
|
- lib/map_fields/railtie.rb
|
27
27
|
- lib/map_fields.rb
|
28
|
+
- views/map_fields/_mapping.erb
|
28
29
|
- spec/spec_helper.rb
|
29
30
|
homepage: http://github.com/internuity/map-fields
|
30
31
|
licenses: []
|
@@ -53,3 +54,4 @@ summary: Rails gem to allow a user to map the fields of a CSV to an expected lis
|
|
53
54
|
of fields
|
54
55
|
test_files:
|
55
56
|
- spec/spec_helper.rb
|
57
|
+
has_rdoc:
|