uploadable 0.0.1 → 0.0.2
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/.gitignore +20 -0
- data/.todo +2 -0
- data/Gemfile.lock +1 -1
- data/lib/uploadable.rb +12 -1
- data/lib/uploadable/version.rb +1 -1
- data/spec/support/sample_app/app/models/album.rb +6 -0
- data/spec/uploadable_spec.rb +26 -0
- metadata +2 -2
data/.gitignore
CHANGED
@@ -1,2 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
.idea
|
19
|
+
.idea/**
|
1
20
|
.rvmrc
|
21
|
+
.rvmrcbk
|
2
22
|
tags
|
data/.todo
CHANGED
data/Gemfile.lock
CHANGED
data/lib/uploadable.rb
CHANGED
@@ -17,10 +17,21 @@ module Uploadable
|
|
17
17
|
end
|
18
18
|
|
19
19
|
define_method :upload_from_csv do |contents|
|
20
|
-
raise NoMethodError.new("Method only
|
20
|
+
raise NoMethodError.new("Method only available for uploadable models") if @upload_processor.blank?
|
21
21
|
records = @upload_processor.transform_csv contents
|
22
22
|
create records
|
23
23
|
end
|
24
24
|
|
25
|
+
define_method :upload_from_csv! do |contents, options = {}|
|
26
|
+
raise NoMethodError.new("Method only aviable for uploadable models") if @upload_processor.blank?
|
27
|
+
objects = []
|
28
|
+
failed = false
|
29
|
+
ActiveRecord::Base.transaction do
|
30
|
+
records = @upload_processor.transform_csv contents
|
31
|
+
objects = create records
|
32
|
+
raise ActiveRecord::Rollback if objects.any? {|obj| !obj.errors.full_messages.blank?}
|
33
|
+
end
|
34
|
+
return objects
|
35
|
+
end
|
25
36
|
end
|
26
37
|
end
|
data/lib/uploadable/version.rb
CHANGED
data/spec/uploadable_spec.rb
CHANGED
@@ -32,5 +32,31 @@ describe "Uploadable" do
|
|
32
32
|
Album.where(:artist => "John Denver", :title => "All Aboard!").count.should == 1
|
33
33
|
Album.where(:artist => "Usher", :title => "Looking 4 Myself").count.should == 1
|
34
34
|
end
|
35
|
+
|
36
|
+
it "should partially upload records id error exists" do
|
37
|
+
records = Album.upload_from_csv("Artist,Title,Extra\n\"John Denver\",\"All Aboard!\",1\n\"Usher\",,2")
|
38
|
+
Album.count.should == 1
|
39
|
+
records.first.errors.full_messages.should be_empty
|
40
|
+
records.last.errors.full_messages.should_not be_empty
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "upload_from_csv!" do
|
45
|
+
it "should raise NoMethodError for non uploadable models" do
|
46
|
+
lambda { User.upload_from_csv!("") }.should raise_error(NoMethodError)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should upload to an uploadable model" do
|
50
|
+
lambda { Album.upload_from_csv!("Artist,Title,Extra\n\"John Denver\",\"All Aboard!\",1\n\"Usher\",\"Looking 4 Myself\",2")}.
|
51
|
+
should change(Album, :count).by(2)
|
52
|
+
Album.where(:artist => "John Denver", :title => "All Aboard!").count.should == 1
|
53
|
+
Album.where(:artist => "Usher", :title => "Looking 4 Myself").count.should == 1
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should rollback all upload records id error exists" do
|
57
|
+
objects = Album.upload_from_csv!("Artist,Title,Extra\n\"John Denver\",\"All Aboard!\",1\n\"Usher\",,2")
|
58
|
+
objects.collect(&:id).should == [nil, nil]
|
59
|
+
Album.count.should == 0
|
60
|
+
end
|
35
61
|
end
|
36
62
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uploadable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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: 2012-07-
|
12
|
+
date: 2012-07-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: fastercsv
|