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 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
@@ -5,3 +5,5 @@
5
5
  3. Travis CI
6
6
  4. header_converters to pick from en.yml rather than symbolize
7
7
  5. Add default columns & computed columns from given set of columns
8
+ 6. Add documentation to README.md
9
+ 7. To add method upload from file by giving file params coming from controller action
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- uploadable (0.0.1)
4
+ uploadable (0.0.2)
5
5
  fastercsv
6
6
 
7
7
  GEM
@@ -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 aviable for uploadable models") if @upload_processor.blank?
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
@@ -1,3 +1,3 @@
1
1
  module Uploadable
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,5 +2,11 @@ class Album < ActiveRecord::Base
2
2
  uploadable :mandatory_fields => [:title], :optional_fields => [:artist]
3
3
  attr_accessible :artist, :title
4
4
 
5
+ validates_presence_of :title
6
+
5
7
  has_many :tracks
8
+
9
+ def test_method
10
+ "booga"
11
+ end
6
12
  end
@@ -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.1
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-01 00:00:00.000000000 Z
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fastercsv