activerecord_mass_insert 0.1.0 → 0.2.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: fa01ff53a01bb14c0926a26f816905205ffeffc2
4
- data.tar.gz: bc10dde065d7f8782dd3b7e04bfe72c67a619d24
3
+ metadata.gz: dc7e1b88f199bb60fa96b966661a69e2fed064ff
4
+ data.tar.gz: a43ed568b5b504dfa47c82ae23a0ba64d4da8287
5
5
  SHA512:
6
- metadata.gz: 7464ccd801d7c378e710c2fa3cfb6a92074512e9c0268c4af828e3e93b6601222000e4dc6479aa89da39a47f3a1a240b383a7592f0f66e1504bc1c57c4d1815d
7
- data.tar.gz: aa77057469cfc75cd67370239134422b568ff0176782e10d7f9f92d53690ee04044ff95484917cc4e882e71f1d7314da6fcf76c8b8f0cf981c45573b4fbb4fda
6
+ metadata.gz: d1f4c728c521a1c5932f794e5c87a29068175a0268dccf473801364391cbc2cf3ebc7f41c9d6d56e7498852fed3bbd8de2b7692bb71370eac9e820bbd52eba08
7
+ data.tar.gz: 01d447056198709f89b102c2957f8a41887337c12b828188f8c08f90ebcac9631e2ebea6b688cb8fb86b9c1edd746a102fb0ad541d6e838bde52fa890e8a167a
data/README.md CHANGED
@@ -31,12 +31,14 @@ end
31
31
 
32
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}}]'
33
33
 
34
- dog_ids = Dog.mass_insert(payload, :name, created_at: 'NOW()', updated_at: 'NOW()')
34
+ dog_ids = Dog.mass_insert(payload)
35
35
 
36
36
  dogs = Dog.find(dog_ids)
37
37
 
38
38
  puts dogs.count # => 6
39
39
  puts dogs.first.name # => Madison
40
+ puts dogs.first.breed # => Golden
41
+ puts dogs.first.meta # => { "rescue" => false, "age" => null }
40
42
  ```
41
43
 
42
44
  For a payload you can pass a JSON objects by itself, in a JSON array, or a ruby array. You can also
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
+ spec.required_ruby_version = '>= 2.0.0'
26
+
25
27
  spec.add_runtime_dependency 'activerecord', '>= 4.2.0'
26
28
  spec.add_runtime_dependency 'pg', '>= 0.20.0'
27
29
 
@@ -8,8 +8,6 @@ module ActiveRecord
8
8
  class Inserter < Statement
9
9
  extend Forwardable
10
10
 
11
- def_delegators :model, :primary_key, :arel_table, :connection
12
-
13
11
  def self.insert(*args)
14
12
  new(*args).insert
15
13
  end
@@ -18,6 +16,10 @@ module ActiveRecord
18
16
  connection.execute(sql)
19
17
  end
20
18
 
19
+ private
20
+
21
+ def_delegators :model, :primary_key, :arel_table, :connection
22
+
21
23
  def sql
22
24
  [
23
25
  model.send(:sanitize_sql_array, [statement.to_sql, payload]),
@@ -9,8 +9,6 @@ module ActiveRecord
9
9
  class Projection < Statement
10
10
  extend Forwardable
11
11
 
12
- def_delegators :model, :column_for_attribute
13
-
14
12
  def statement
15
13
  init_args = []
16
14
  init_args << arel_table.engine if arel_table.respond_to?(:engine)
@@ -21,6 +19,10 @@ module ActiveRecord
21
19
  end
22
20
  end
23
21
 
22
+ private
23
+
24
+ def_delegators :model, :column_for_attribute
25
+
24
26
  def select
25
27
  [
26
28
  *matching_columns.map { |column| arel_table[column] },
@@ -4,17 +4,36 @@ module ActiveRecord
4
4
  module MassInsert
5
5
  # Common structure for all SQL statement builders
6
6
  class Statement
7
- attr_reader :model, :payload, :matching_columns, :mapped_columns
8
-
9
7
  def initialize(model, payload, *matching_columns, **mapped_columns)
10
8
  @model = model
11
9
  @payload = payload_to_json_array(payload)
12
- @matching_columns = matching_columns
13
- @mapped_columns = mapped_columns
10
+ if matching_columns.empty? && mapped_columns.empty?
11
+ @matching_columns = fallback_matching_columns(model)
12
+ @mapped_columns = fallback_mapped_columns(model)
13
+ else
14
+ @matching_columns = matching_columns
15
+ @mapped_columns = mapped_columns
16
+ end
14
17
  end
15
18
 
16
19
  private
17
20
 
21
+ attr_reader :model, :payload, :matching_columns, :mapped_columns
22
+
23
+ def fallback_matching_columns(new_model)
24
+ new_model.columns.map(&:name) - [new_model.primary_key, 'created_at', 'updated_at']
25
+ end
26
+
27
+ def fallback_mapped_columns(new_model)
28
+ {}.tap do |result|
29
+ %w[created_at updated_at].each do |column|
30
+ if new_model.columns_hash.keys.include?(column)
31
+ result[column.to_sym] = Arel::Nodes::NamedFunction.new('NOW', [])
32
+ end
33
+ end
34
+ end
35
+ end
36
+
18
37
  def payload_to_json_array(new_payload)
19
38
  if new_payload.nil?
20
39
  new_payload = new_payload.to_s
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module MassInsert
5
- VERSION = [0, 1, 0].freeze
5
+ VERSION = [0, 2, 0].freeze
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_mass_insert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Rove
@@ -184,7 +184,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
184
184
  requirements:
185
185
  - - ">="
186
186
  - !ruby/object:Gem::Version
187
- version: '0'
187
+ version: 2.0.0
188
188
  required_rubygems_version: !ruby/object:Gem::Requirement
189
189
  requirements:
190
190
  - - ">="