active_record_bulk_insert 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjEyNGY3MzlkZTc3ZDkyOTRkYjdkMGFiZjUwYzVjNTFmN2M0NDVmYQ==
4
+ YTY2YmY2OTZkYWEzODRlOWFmZGFlZDkxZjZmYWFmNDk1Yjk3YTZiZg==
5
5
  data.tar.gz: !binary |-
6
- NGFmMWVkN2U1ZWFmNTZmMDJiOTgxYzRjM2M4OWI1M2RkOThiYTNhMQ==
6
+ NzJjZDAyYmYwYTNlMmUyYTAzMTkwYjRjNWU2ZThkMmE3ZGE2NjBhZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YWQ5MDA0ZmY0ZjllNDc0ZGI4ZjY5OTFjYmYxNjFhNzdhZWRiZGExYTEzMTdm
10
- Mzk4Zjc3MzBjZDhjZWY5ZDZjMjlmMWVlYzUzOWY3ZTFhN2FmZTg2ZjgzMDFk
11
- ODU0YTNiZWM1MGQ3ZTFkZTZlNzdlOGZjN2JkZDIwMmVkNTU2OGM=
9
+ ZTQ5ZDdkNzI5ZTJiMmJjNWMzZDMzMDU5YjY5MGJlM2Q3MDBlOGRlMzcwYjE3
10
+ YWM5NzBiY2Y0ZDA0NWNmNDE5MWFkZTc5Y2RlNjhhOTA2MWUxYTliY2U4YmQw
11
+ YTI0ZTQxNTlkNGMxYmE2NzI2NTQ1YTUzMWNkMTdkYjkwYTM3NWI=
12
12
  data.tar.gz: !binary |-
13
- MmU5MmRjMWUzMDQ0MzRmMmE3ZjJjNGJmZjQ0OTI4NDRmOTUyMGY2ODg2MDA4
14
- NWNlMThjZGUwNzVlMGI2Y2ZiNmFiZGFmNTZjNTM2NjVkOTc0N2RhMDY4NmFl
15
- YWJiOTU2MDg4ZDBmMDIwMDZhMTliNjQwNTgzN2EyYWQ0NDJkY2U=
13
+ ZTJhZDQ3M2U0ZWNhNmYwZmQyNWQzNmE0Y2Q1MzJjNTA5MDY4NTRmNjJmMTAw
14
+ YzM3NWEzNDY5NjQ1NDQ4NmFmMjAwNDMwYjllYmQ4NTA5ZTMwMzFjMzg5NTY0
15
+ YjU4MGI4NGQyYzExMGJjNDQ4OTgxODQzNWZmMTVmMDdmNzAxZmI=
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/active_record_bulk_insert.svg)](http://badge.fury.io/rb/active_record_bulk_insert)
2
+ [![Gem](https://img.shields.io/gem/dt/active_record_bulk_insert.svg)](https://rubygems.org/gems/active_record_bulk_insert)
2
3
  [![Build Status](https://api.travis-ci.org/bjhaid/active_record_bulk_insert.png)](https://travis-ci.org/bjhaid/active_record_bulk_insert)
3
4
 
4
5
  # BULK INSERT
@@ -50,6 +51,17 @@ User.bulk_insert(users, :validate => true)
50
51
  ```
51
52
  *The return value is a list of invalid records*
52
53
 
54
+ ### Validate with a custom validator
55
+
56
+ ```ruby
57
+ users = [{:username => "foo", :firstname => "Foo", :lastname => "Bar", :age => 31},
58
+ {:username => "j.doe", :firstname => "John", :lastname => "Doe", :age => 57},
59
+ {:firstname => "John", :lastname => "Doe", :age => 57}]
60
+ User.bulk_insert(users, :validate_with => Proc.new { |user| !user[:username].blank? })
61
+ # => [{:firstname => "John", :lastname => "Doe", :age => 57}]
62
+ ```
63
+ *The return value is a list of invalid records*
64
+
53
65
  ### Provide your own primary keys
54
66
 
55
67
  ```ruby
@@ -18,8 +18,9 @@ ActiveRecord::Base.class_eval do
18
18
  attributes = _resolve_record(attrs.first, options).keys.join(", ")
19
19
 
20
20
  invalid = []
21
- if options.fetch(:validate, false)
22
- attrs, invalid = attrs.partition { |record| _validate(record) }
21
+ if options.fetch(:validate, false) || options.fetch(:validate_with, false)
22
+ _validator = options.fetch(:validate_with, self.method(:_validate))
23
+ attrs, invalid = attrs.partition { |record| _validator.call(record) }
23
24
  end
24
25
 
25
26
  values_sql = attrs.map do |record|
@@ -89,6 +89,25 @@ describe SampleRecord do
89
89
  invalid_records = SampleRecord.bulk_insert(records, :validate => true, :disable_timestamps => true)
90
90
  invalid_records.should == [{:age => 30, :name => ""}]
91
91
  end
92
+
93
+ it "validates with a custom validator if ':validate_with' is specified" do
94
+ records = [{:age => 30, :name => "Bar"}, {:age => 29, :name => "Foo"}]
95
+ validator = Proc.new { |record| record[:name] != "Foo" }
96
+
97
+ invalid_records = SampleRecord.bulk_insert(records, :validate_with => validator)
98
+
99
+ invalid_records.should == [{:age => 29, :name => "Foo"}]
100
+ end
101
+
102
+ it "validates with the custom validator when both ':validate' and ':validate_with' are specified" do
103
+ SampleRecord.send(:validates, :name, :presence => true)
104
+ records = [{:age => 30, :name => ""}, {:age => 29, :name => "Foo"}]
105
+ validator = Proc.new { |record| record[:name] != "Foo" }
106
+
107
+ invalid_records = SampleRecord.bulk_insert(records, :validate => true, :validate_with => validator)
108
+
109
+ invalid_records.should == [{:age => 29, :name => "Foo"}]
110
+ end
92
111
  end
93
112
 
94
113
  context "timestamps" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_bulk_insert
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abejide Ayodele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-21 00:00:00.000000000 Z
11
+ date: 2017-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord