act_as_importable 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  act_as_importable.db
19
+ .idea
data/README.md CHANGED
@@ -56,6 +56,17 @@ User.import_csv_file('/path/to/file.csv', :uid => :email)
56
56
  This will find an existing record with a matching email address and update their name.
57
57
  If no record exists it will create a new one.
58
58
 
59
+ ## Other options
60
+
61
+ * uid (default: 'uid')
62
+ * except (default: nil)
63
+ * only (default: nil)
64
+ * default_values (default: nil)
65
+ * delete_missing_records (default: false)
66
+ * existing_record_scope (default: Model.all)
67
+
68
+
69
+
59
70
 
60
71
  ## Test
61
72
 
@@ -15,6 +15,24 @@ module ActAsImportable
15
15
  data.map do |row|
16
16
  import_record(row)
17
17
  end
18
+ delete_missing_records if options[:delete_missing_records]
19
+ end
20
+
21
+ def existing_record_scope
22
+ options[:existing_record_scope] || model_class.all
23
+ end
24
+
25
+ def existing_record_ids
26
+ existing_record_scope.map(&:id)
27
+ end
28
+
29
+ def record_ids_to_delete
30
+ imported_ids = successful_imports.map(&:id)
31
+ existing_record_ids.reject { |id| imported_ids.include? id }
32
+ end
33
+
34
+ def delete_missing_records
35
+ model_class.delete(record_ids_to_delete)
18
36
  end
19
37
 
20
38
  def import_record(row)
@@ -1,3 +1,3 @@
1
1
  module ActAsImportable
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -1,3 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
1
3
  describe ActAsImportable::Importer do
2
4
 
3
5
  before(:each) do
@@ -35,4 +37,65 @@ describe ActAsImportable::Importer do
35
37
  end
36
38
  end
37
39
 
40
+ describe "delete_missing_records option" do
41
+ let(:importer) { ActAsImportable::Importer.new(:uid => :name, :model_class => Item, :delete_missing_records => true) }
42
+ let(:beer_category) { Category.create!(:name => 'Beer') }
43
+ let(:wine_category) { Category.create!(:name => 'Wine') }
44
+
45
+ before :each do
46
+ @beer1 = Item.create!(:name => 'Beer 1', :price => 3.0, :category => beer_category)
47
+ @beer2 = Item.create!(:name => 'Beer 2', :price => 3.5, :category => beer_category)
48
+ @wine1 = Item.create!(:name => 'Wine 1', :price => 16.5, :category => wine_category)
49
+ end
50
+
51
+ it "should call delete_missing_records when the option is true" do
52
+ importer.options[:delete_missing_records] = true
53
+ importer.should_receive(:delete_missing_records).once
54
+ importer.import_data([{:name => 'Beer 1', :price => 3.0}])
55
+ end
56
+
57
+ it "should not call delete_missing_records when the option is false" do
58
+ importer.options[:delete_missing_records] = false
59
+ importer.should_not_receive(:delete_missing_records)
60
+ importer.import_data([{:name => 'Beer 1', :price => 3.0}])
61
+ end
62
+
63
+ it "should default the delete_missing_records option to false" do
64
+ importer.options.delete(:delete_missing_records)
65
+ importer.should_not_receive(:delete_missing_records)
66
+ importer.import_data([{:name => 'Beer 1', :price => 3.0}])
67
+ end
68
+
69
+ context "when no existing_record_scope option is provided" do
70
+
71
+ it "should delete all existing records that were not included in the import" do
72
+ expect {
73
+ importer.import_data([{:name => 'Beer 1', :price => 3.0}])
74
+ }.to change { Item.count }.from(3).to(1)
75
+ end
76
+
77
+ end
78
+
79
+ context "when an existing_record_scope option is provided" do
80
+
81
+ before :each do
82
+ importer.options[:existing_record_scope] = Item.for_category(beer_category)
83
+ end
84
+
85
+ it "should not delete any records when all objects in the existing_records_scope are updated" do
86
+ expect {
87
+ importer.import_data([{:name => 'Beer 1', :price => 3.0}, {:name => 'Beer 2', :price => 3.0}])
88
+ }.to change { Item.count }.by(0)
89
+ end
90
+
91
+ it "should delete existing records, in the scope provided, that were not included in the import" do
92
+ expect {
93
+ importer.import_data([{:name => 'Beer 1', :price => 3.0}])
94
+ }.to change { Item.count }.from(3).to(2)
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
38
101
  end
data/spec/spec_helper.rb CHANGED
@@ -44,4 +44,8 @@ class Item < ActiveRecord::Base
44
44
  act_as_importable :uid => 'name'
45
45
 
46
46
  belongs_to :category
47
+
48
+ def self.for_category(category)
49
+ where(:category_id => category.id)
50
+ end
47
51
  end
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.8
4
+ version: 0.0.10
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-04-07 00:00:00.000000000 Z
12
+ date: 2013-04-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  version: '0'
123
123
  requirements: []
124
124
  rubyforge_project:
125
- rubygems_version: 1.8.24
125
+ rubygems_version: 1.8.25
126
126
  signing_key:
127
127
  specification_version: 3
128
128
  summary: Helps import records from CSV files.