junk_drawer 1.4.0 → 1.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 238eb68d411dc0c6089296530a43a2465975d417
4
- data.tar.gz: 655fec097bfbdf9aa9ad42d1b3ebb95b96f5e6de
3
+ metadata.gz: 9ccbaed0545acce12327a2220ffd61a1f2c89a88
4
+ data.tar.gz: bb229cb6b5d34628b6122a1476bb854afd474726
5
5
  SHA512:
6
- metadata.gz: 97e29c483b957b9de82874825ec57a185f4b44ec4bfb00db45cdc7666fba99bc1466a0cbec786428efcbeec02843919373e1fd5177bfb4413b9e4b8b7fb5069b
7
- data.tar.gz: 361c72c75c5263153d8ca6b0bcdd1fcc0cc2a158710492ba30de723f4ef1bfdc866a9745d7ab712f78a47b7769a5a7bc4297ed068a67213a3bc3f7a792582331
6
+ metadata.gz: dc88c0d245fe98b677b94b172968605fa4bec3070fb889e226ce2eb92b848cc24fba1c59bbe92ec99a834c66498f5642b69681a4f34ade042c0db3c6c716a287
7
+ data.tar.gz: 1743c1dc5149e743d231ff3aa0708ef1cfdc52b19b78f094ed03eaa2e5dbbb8a57cb2cf7def040918e47f766ff5d63898d60bd9103be6eb7062ca1adacec95cc
data/.rubocop.yml CHANGED
@@ -20,7 +20,7 @@ Metrics/MethodLength:
20
20
  Metrics/BlockLength:
21
21
  Exclude:
22
22
  - junk_drawer.gemspec
23
- - spec/**/*_spec.rb
23
+ - spec/**/*
24
24
 
25
25
  RSpec/ExampleLength:
26
26
  Enabled: false
data/Gemfile CHANGED
@@ -6,3 +6,5 @@ gemspec
6
6
 
7
7
  gem 'activerecord', '~> 5.1.0'
8
8
  gem 'activesupport', '~> 5.1.0'
9
+ gem 'hstore_accessor', '~> 1.1.1'
10
+ gem 'jsonb_accessor', '~> 1.0.0'
data/README.md CHANGED
@@ -220,6 +220,13 @@ release a new version, update the version number in `version.rb`, and then run
220
220
  `bundle exec rake release`, which will create a git tag for the version, push
221
221
  git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
222
222
 
223
+ In order to run tests against different Rails versions, you can use
224
+ `BUNDLE_GEMFILE`:
225
+
226
+ ```sh
227
+ $ BUNDLE_GEMFILE=gemfiles/rails_4.2.gems rake spec
228
+ ```
229
+
223
230
  ## Contributing
224
231
 
225
232
  Bug reports and pull requests are welcome on GitHub at
@@ -6,3 +6,5 @@ gemspec path: '../'
6
6
 
7
7
  gem 'activerecord', '~> 4.2.0'
8
8
  gem 'activesupport', '~> 4.2.0'
9
+ gem 'hstore_accessor', '~> 1.1.1'
10
+ gem 'jsonb_accessor', '~> 0.3.3'
@@ -6,3 +6,5 @@ gemspec path: '../'
6
6
 
7
7
  gem 'activerecord', '~> 5.0.0'
8
8
  gem 'activesupport', '~> 5.0.0'
9
+ gem 'hstore_accessor', '~> 1.1.1'
10
+ gem 'jsonb_accessor', '~> 1.0.0'
data/junk_drawer.gemspec CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency 'guard-rubocop', '~> 1.2'
28
28
  spec.add_development_dependency 'pg', '~> 0.20'
29
29
  spec.add_development_dependency 'pry', '~> 0.10'
30
+ spec.add_development_dependency 'pry-byebug', '~> 3.6.0'
30
31
  spec.add_development_dependency 'rake', '~> 12.0'
31
32
  spec.add_development_dependency 'rspec', '~> 3.0'
32
33
  spec.add_development_dependency 'rubocop', '~> 0.48.1'
@@ -19,8 +19,8 @@ module JunkDrawer
19
19
  # an instance. It also causes an error to be raised if a public instance
20
20
  # method is defined with a name other than `call`
21
21
  module ClassMethods
22
- def call(*args)
23
- new.(*args)
22
+ def call(*args, &block)
23
+ new.(*args, &block)
24
24
  end
25
25
 
26
26
  def to_proc
@@ -7,18 +7,6 @@ require 'active_record/connection_adapters/postgresql_adapter'
7
7
  module JunkDrawer
8
8
  # module to allow bulk updates for `ActiveRecord` models
9
9
  module BulkUpdatable
10
- ATTRIBUTE_TYPE_TO_POSTGRES_CAST = {
11
- datetime: '::timestamp',
12
- hstore: '::hstore',
13
- boolean: '::boolean',
14
- jsonb: '::jsonb',
15
- }.freeze
16
-
17
- POSTGRES_VALUE_CASTERS = {
18
- hstore: ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Hstore.new,
19
- jsonb: ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Jsonb.new,
20
- }.freeze
21
-
22
10
  def bulk_update(objects)
23
11
  objects = objects.select(&:changed?)
24
12
  return unless objects.any?
@@ -34,7 +22,13 @@ module JunkDrawer
34
22
  now = Time.zone.now
35
23
  objects.each { |object| object.updated_at = now }
36
24
 
37
- objects.flat_map(&:changed).uniq
25
+ changed_attributes = objects.flat_map(&:changed).uniq
26
+ if ::ActiveRecord::VERSION::MAJOR >= 5
27
+ column_names & changed_attributes
28
+ else
29
+ # to remove virtual columns from jsonb_accessor 0.3.3
30
+ columns.select(&:sql_type).map(&:name) & changed_attributes
31
+ end
38
32
  end
39
33
 
40
34
  def build_query_for(objects, attributes)
@@ -47,7 +41,7 @@ module JunkDrawer
47
41
  "#{quoted_column_name} = tmp_table.#{quoted_column_name}"
48
42
  end.join(', ')
49
43
 
50
- <<-SQL
44
+ <<-SQL.squish
51
45
  UPDATE #{table_name}
52
46
  SET #{assignment_query}
53
47
  FROM (VALUES #{object_values}) AS tmp_table(id, #{attributes.join(', ')})
@@ -56,29 +50,28 @@ module JunkDrawer
56
50
  end
57
51
 
58
52
  def sanitized_values(object, attributes)
59
- postgres_types = attributes.map do |attribute|
60
- attribute_type = columns_hash[attribute.to_s].type
61
- "?#{ATTRIBUTE_TYPE_TO_POSTGRES_CAST[attribute_type]}"
62
- end
63
-
64
53
  postgres_values = attributes.map do |attribute|
65
54
  value = object[attribute]
66
- attribute_type = columns_hash[attribute.to_s].type
67
- caster = POSTGRES_VALUE_CASTERS[attribute_type]
68
55
 
69
- caster ? serialized_value(caster, value) : value
56
+ # AR internal `columns_hash`
57
+ column = columns_hash[attribute.to_s]
58
+
59
+ # AR internal `type_for_attribute`
60
+ type = type_for_attribute(column.name)
61
+ type_cast = "::#{column.sql_type}"
62
+ type_cast = "#{type_cast}[]" if column.array
63
+
64
+ "#{connection.quote(serialized_value(type, value))}#{type_cast}"
70
65
  end
71
66
 
72
- sanitize_sql_array(
73
- ["(?, #{postgres_types.join(', ')})", object.id, *postgres_values],
74
- )
67
+ "(#{[object.id, *postgres_values].join(', ')})"
75
68
  end
76
69
 
77
- def serialized_value(caster, value)
70
+ def serialized_value(type, value)
78
71
  if ::ActiveRecord::VERSION::MAJOR >= 5
79
- caster.serialize(value)
72
+ type.serialize(value)
80
73
  else
81
- caster.type_cast_for_database(value)
74
+ type.type_cast_for_database(value)
82
75
  end
83
76
  end
84
77
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JunkDrawer
4
- VERSION = '1.4.0'
4
+ VERSION = '1.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: junk_drawer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Fletcher
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-14 00:00:00.000000000 Z
11
+ date: 2018-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.6.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.6.0
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rake
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -230,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
230
244
  version: '0'
231
245
  requirements: []
232
246
  rubyforge_project:
233
- rubygems_version: 2.6.13
247
+ rubygems_version: 2.6.14
234
248
  signing_key:
235
249
  specification_version: 4
236
250
  summary: random useful Ruby utilities