activerecord_mass_insert 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa01ff53a01bb14c0926a26f816905205ffeffc2
|
4
|
+
data.tar.gz: bc10dde065d7f8782dd3b7e04bfe72c67a619d24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7464ccd801d7c378e710c2fa3cfb6a92074512e9c0268c4af828e3e93b6601222000e4dc6479aa89da39a47f3a1a240b383a7592f0f66e1504bc1c57c4d1815d
|
7
|
+
data.tar.gz: aa77057469cfc75cd67370239134422b568ff0176782e10d7f9f92d53690ee04044ff95484917cc4e882e71f1d7314da6fcf76c8b8f0cf981c45573b4fbb4fda
|
data/README.md
CHANGED
@@ -19,7 +19,16 @@ Or install it yourself as:
|
|
19
19
|
$ gem install activerecord_mass_insert
|
20
20
|
|
21
21
|
## Usage
|
22
|
+
|
22
23
|
```ruby
|
24
|
+
class ApplicationRecord < ActiveRecord::Base
|
25
|
+
self.abstract_class = true
|
26
|
+
extend ActiveRecord::MassInsert::Helper
|
27
|
+
end
|
28
|
+
|
29
|
+
class Dog < ApplicationRecord
|
30
|
+
end
|
31
|
+
|
23
32
|
payload = '[{"name":"Madison","breed":"Golden","meta":{"rescue":false,"age":null}},{"name":"Daisy","meta":{"rescue":true,"age":18}},{"name":"Gracey","meta":{"rescue":false,"nickname":"Scoogie","age":11}},{"name":"Sadie","meta":{"rescue":true,"dingo_blood":true,"age":11}},{"name":"Raymond","meta":{"rescue":null,"nickname":"Radar","tail":false,"age":11}},{"name":"Nemo","meta":{"rescue":true,"number_of_ears":1,"age":2}}]'
|
24
33
|
|
25
34
|
dog_ids = Dog.mass_insert(payload, :name, created_at: 'NOW()', updated_at: 'NOW()')
|
@@ -31,8 +40,8 @@ puts dogs.first.name # => Madison
|
|
31
40
|
```
|
32
41
|
|
33
42
|
For a payload you can pass a JSON objects by itself, in a JSON array, or a ruby array. You can also
|
34
|
-
pass a ruby object that responds to to_json by itself or in an array.
|
35
|
-
This done to ensure no time is wasted parsing json twice.
|
43
|
+
pass a ruby object that responds to to_json by itself or in an array. nil payload is treated as an empty
|
44
|
+
json array. JSON will never be parsed to ruby. This done to ensure no time is wasted parsing json twice.
|
36
45
|
|
37
46
|
## Development
|
38
47
|
|
@@ -46,7 +55,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
46
55
|
|
47
56
|
Bug reports and pull requests are welcome on GitHub at https://github.com/rovermicrover/activerecord_mass_insert.
|
48
57
|
|
49
|
-
|
50
58
|
## License
|
51
59
|
|
52
60
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -16,7 +16,9 @@ module ActiveRecord
|
|
16
16
|
private
|
17
17
|
|
18
18
|
def payload_to_json_array(new_payload)
|
19
|
-
if new_payload.
|
19
|
+
if new_payload.nil?
|
20
|
+
new_payload = new_payload.to_s
|
21
|
+
elsif new_payload.is_a?(Array) && new_payload.all? { |p| p.is_a?(String) }
|
20
22
|
# If payload is an array of strings, assume its an array
|
21
23
|
# of json objects. contact them togther to form the
|
22
24
|
# inner part of a json array.
|