act_as_importable 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +32 -1
- data/lib/act_as_importable/base.rb +18 -2
- data/lib/act_as_importable/version.rb +1 -1
- data/spec/act_as_importable/act_as_importable_spec.rb +15 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ActAsImportable
|
2
2
|
|
3
|
-
Helps you easily import records from a CSV file.
|
3
|
+
Helps you easily import records from a CSV file or an array of hashes.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -22,7 +22,38 @@ Or install it yourself as:
|
|
22
22
|
class User < ActiveRecord::Base
|
23
23
|
act_as_importable
|
24
24
|
end
|
25
|
+
|
26
|
+
User.import_csv_file('/path/to/file.csv')
|
27
|
+
# or
|
28
|
+
User.import_csv_text(csv_text)
|
29
|
+
# or
|
30
|
+
User.import_data(array_of_hashes)
|
31
|
+
```
|
32
|
+
|
33
|
+
## CSV File Format
|
34
|
+
|
35
|
+
The importer will automatically map the column headers to the attributes of the model.
|
36
|
+
|
37
|
+
###Example:
|
38
|
+
```
|
39
|
+
first_name,last_name,email
|
40
|
+
John,Smith,j.smith@gmail.com
|
41
|
+
```
|
42
|
+
|
43
|
+
This will create a new User called John Smith with the email address j.smith@gmail.com.
|
44
|
+
|
45
|
+
## Updating existing records
|
46
|
+
|
47
|
+
You can specify a unique field that will be used to find existing records.
|
48
|
+
|
49
|
+
###Example:
|
25
50
|
```
|
51
|
+
User.import_csv_file('/path/to/file.csv', :uid => :email)
|
52
|
+
```
|
53
|
+
|
54
|
+
This will find an existing record with a matching email address and update their name.
|
55
|
+
If no record exists it will create a new one.
|
56
|
+
|
26
57
|
|
27
58
|
## Test
|
28
59
|
|
@@ -8,7 +8,7 @@ module ActAsImportable
|
|
8
8
|
module ClassMethods
|
9
9
|
|
10
10
|
def import_csv_file(file, options = {})
|
11
|
-
import_csv_text(
|
11
|
+
import_csv_text(read_file(file, options), options)
|
12
12
|
end
|
13
13
|
|
14
14
|
def import_csv_text(text, options = {})
|
@@ -18,6 +18,12 @@ module ActAsImportable
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
def import_data(data, options = {})
|
22
|
+
data.map do |row|
|
23
|
+
import_record(row, options)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
21
27
|
# Creates or updates a model record
|
22
28
|
# Existing records are found by the column(s) specified by the :uid option (default 'id').
|
23
29
|
# If the values for the uid columns are not provided the row will be ignored.
|
@@ -31,7 +37,9 @@ module ActAsImportable
|
|
31
37
|
record = find_or_create_by_uids(uid_values(row, options))
|
32
38
|
remove_uid_values_from_row(row, options)
|
33
39
|
record.update_attributes(row)
|
34
|
-
record.save
|
40
|
+
unless record.save
|
41
|
+
Rails.logger.error(record.errors.full_messages)
|
42
|
+
end
|
35
43
|
record
|
36
44
|
end
|
37
45
|
|
@@ -87,6 +95,14 @@ module ActAsImportable
|
|
87
95
|
end
|
88
96
|
end
|
89
97
|
|
98
|
+
def read_file(file, options = {})
|
99
|
+
if options[:encoding]
|
100
|
+
File.read(file, :encoding => options[:encoding])
|
101
|
+
else
|
102
|
+
File.read(file)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
90
106
|
end
|
91
107
|
end
|
92
108
|
end
|
@@ -74,7 +74,22 @@ describe "an act_as_importable model" do
|
|
74
74
|
it "should return an array of imported records" do
|
75
75
|
Item.import_csv_text(text).should == Item.all.to_a
|
76
76
|
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "import data" do
|
80
|
+
let(:beer) { { :name => 'Beer', :price => 2.5 } }
|
81
|
+
let(:apple) { {:name => 'Apple', :price => 0.5} }
|
82
|
+
let(:data) { [beer, apple] }
|
77
83
|
|
84
|
+
it 'should call import_record with row hashes' do
|
85
|
+
Item.should_receive(:import_record).with(beer, {}).once
|
86
|
+
Item.should_receive(:import_record).with(apple, {}).once
|
87
|
+
Item.import_data(data)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should return an array of imported records" do
|
91
|
+
Item.import_data(data).should == Item.all.to_a
|
92
|
+
end
|
78
93
|
end
|
79
94
|
|
80
95
|
describe "import record" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: act_as_importable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|