jsonb_accessor 1.3.4 → 1.3.6

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
  SHA256:
3
- metadata.gz: b3b40b7c775b9275c1fe7b6f3f3a8c0b7988dcd995da1b5bd594d0c3469ca372
4
- data.tar.gz: 68dcf83030c07a537adee9030fd0f209f276e1e80f118c2d3ecc982694be466b
3
+ metadata.gz: b9c291219f0d8fa746d82718410e4eb6743a2e53b0c7f55b2d4770b0ab40b4de
4
+ data.tar.gz: 92cb5a1e680f3bcab2aff0ad1c459e1f67714083337a1cf0675837aba3f01a65
5
5
  SHA512:
6
- metadata.gz: 9a136d5a27d25f8ffb29e2f2f06ffb43417b91544836dda70dd40583786512d29d302251c39eb877772e382da5ef4af28afc970a31ae295b5d01933d5f5e33bd
7
- data.tar.gz: 411147569acaf3f9052e4ccae17063b3b0eaeb00c2738933a520a635861b054889b31b534b8959ae5d38bc3f5709dc0c3ccc9b20f45cf0f1f066b0aa60469e77
6
+ metadata.gz: cf6c29dba63cdc774387bc4e8146b7884188ef6219e29a901367cc380da7257501401a4788534016ea6c8c95a1a2cfb526e8657ae6f8606caa4e91748c4358dd
7
+ data.tar.gz: f8805f9278ca28e29b2521b3283e132e2264687a5dfaee01c18de82ede3de38465228092a96571bbb66d24a24cd3a01b5d61732ef2cbacca75fc02679860eb39
@@ -11,12 +11,13 @@ jobs:
11
11
  runs-on: ubuntu-latest
12
12
  steps:
13
13
  - uses: actions/checkout@v2
14
-
14
+
15
15
  - name: Set up Ruby
16
16
  uses: ruby/setup-ruby@v1
17
17
  with:
18
18
  ruby-version: 2.7.2
19
19
  bundler-cache: true
20
+
20
21
  - name: Rubocop
21
22
  run: bundle exec rubocop
22
23
 
data/CHANGELOG.md CHANGED
@@ -1,6 +1,16 @@
1
1
  # Changelog
2
2
  ## [Unreleased]
3
3
 
4
+ ## [1.3.6] - 2022-09-23
5
+ ### Fixed
6
+
7
+ - Bug fix: Datetime values were not properly deserialized [#155](https://github.com/madeintandem/jsonb_accessor/pull/155)
8
+
9
+ ## [1.3.5] - 2022-07-23
10
+ ### Fixed
11
+
12
+ - Bug fix: Attributes defined outside of jsonb_accessor are not written [#149](https://github.com/madeintandem/jsonb_accessor/pull/149)
13
+
4
14
  ## [1.3.4] - 2022-02-02
5
15
  ### Fixed
6
16
 
data/README.md CHANGED
@@ -25,7 +25,7 @@ It also adds generic scopes for querying `jsonb` columns.
25
25
  Add this line to your application's `Gemfile`:
26
26
 
27
27
  ```ruby
28
- gem "jsonb_accessor", "~> 1"
28
+ gem "jsonb_accessor"
29
29
  ```
30
30
 
31
31
  And then execute:
@@ -11,13 +11,13 @@ module JsonbAccessor
11
11
 
12
12
  # <jsonb_attribute>_where scope
13
13
  klass.define_singleton_method "#{jsonb_attribute}_where" do |attributes|
14
- store_key_attributes = ::JsonbAccessor::Helpers.convert_keys_to_store_keys(attributes, all.model.public_send(store_key_mapping_method_name))
14
+ store_key_attributes = JsonbAccessor::Helpers.convert_keys_to_store_keys(attributes, all.model.public_send(store_key_mapping_method_name))
15
15
  jsonb_where(jsonb_attribute, store_key_attributes)
16
16
  end
17
17
 
18
18
  # <jsonb_attribute>_where_not scope
19
19
  klass.define_singleton_method "#{jsonb_attribute}_where_not" do |attributes|
20
- store_key_attributes = ::JsonbAccessor::Helpers.convert_keys_to_store_keys(attributes, all.model.public_send(store_key_mapping_method_name))
20
+ store_key_attributes = JsonbAccessor::Helpers.convert_keys_to_store_keys(attributes, all.model.public_send(store_key_mapping_method_name))
21
21
  jsonb_where_not(jsonb_attribute, store_key_attributes)
22
22
  end
23
23
 
@@ -19,5 +19,19 @@ module JsonbAccessor
19
19
  def convert_store_keys_to_keys(attributes, store_key_mapping)
20
20
  convert_keys_to_store_keys(attributes, store_key_mapping.invert)
21
21
  end
22
+
23
+ def deserialize_value(value, attribute_type)
24
+ return value if value.blank?
25
+
26
+ if attribute_type == :datetime
27
+ value = if active_record_default_timezone == :utc
28
+ Time.find_zone("UTC").parse(value).in_time_zone
29
+ else
30
+ Time.zone.parse(value)
31
+ end
32
+ end
33
+
34
+ value
35
+ end
22
36
  end
23
37
  end
@@ -38,7 +38,7 @@ module JsonbAccessor
38
38
  end
39
39
 
40
40
  # Get store keys to default values mapping
41
- store_keys_and_defaults = ::JsonbAccessor::Helpers.convert_keys_to_store_keys(names_and_defaults, public_send(store_key_mapping_method_name))
41
+ store_keys_and_defaults = JsonbAccessor::Helpers.convert_keys_to_store_keys(names_and_defaults, public_send(store_key_mapping_method_name))
42
42
 
43
43
  # Define jsonb_defaults_mapping_for_<jsonb_attribute>
44
44
  defaults_mapping_method_name = "jsonb_defaults_mapping_for_#{jsonb_attribute}"
@@ -69,7 +69,7 @@ module JsonbAccessor
69
69
  attribute_value = public_send(name)
70
70
  # Rails always saves time based on `default_timezone`. Since #as_json considers timezone, manual conversion is needed
71
71
  if attribute_value.acts_like?(:time)
72
- attribute_value = (::JsonbAccessor::Helpers.active_record_default_timezone == :utc ? attribute_value.utc : attribute_value.in_time_zone).strftime("%F %R:%S.%L")
72
+ attribute_value = (JsonbAccessor::Helpers.active_record_default_timezone == :utc ? attribute_value.utc : attribute_value.in_time_zone).strftime("%F %R:%S.%L")
73
73
  end
74
74
 
75
75
  new_values = (public_send(jsonb_attribute) || {}).merge(store_key => attribute_value)
@@ -83,17 +83,16 @@ module JsonbAccessor
83
83
  names_to_store_keys = self.class.public_send(store_key_mapping_method_name)
84
84
 
85
85
  # this is the raw hash we want to save in the jsonb_attribute
86
- value_with_store_keys = ::JsonbAccessor::Helpers.convert_keys_to_store_keys(value, names_to_store_keys)
86
+ value_with_store_keys = JsonbAccessor::Helpers.convert_keys_to_store_keys(value, names_to_store_keys)
87
87
  write_attribute(jsonb_attribute, value_with_store_keys)
88
88
 
89
89
  # this maps attributes to values
90
- value_with_named_keys = ::JsonbAccessor::Helpers.convert_store_keys_to_keys(value, names_to_store_keys)
90
+ value_with_named_keys = JsonbAccessor::Helpers.convert_store_keys_to_keys(value, names_to_store_keys)
91
91
 
92
92
  empty_named_attributes = names_to_store_keys.transform_values { nil }
93
93
  empty_named_attributes.merge(value_with_named_keys).each do |name, attribute_value|
94
- # Undefined keys: There might be things in the JSON that haven't been defined using jsonb_accessor
95
- # It should still be possible to save arbitrary data in the JSON
96
- next unless has_attribute? name
94
+ # Only proceed if this attribute has been defined using `jsonb_accessor`.
95
+ next unless names_to_store_keys.key?(name)
97
96
 
98
97
  write_attribute(name, attribute_value)
99
98
  end
@@ -110,12 +109,15 @@ module JsonbAccessor
110
109
  name = names_and_store_keys.key(store_key)
111
110
  next unless name
112
111
 
113
- write_attribute(name, value)
112
+ write_attribute(
113
+ name,
114
+ JsonbAccessor::Helpers.deserialize_value(value, self.class.type_for_attribute(name).type)
115
+ )
114
116
  clear_attribute_change(name) if persisted?
115
117
  end
116
118
  end
117
119
 
118
- ::JsonbAccessor::AttributeQueryMethods.new(self).define(store_key_mapping_method_name, jsonb_attribute)
120
+ JsonbAccessor::AttributeQueryMethods.new(self).define(store_key_mapping_method_name, jsonb_attribute)
119
121
  end
120
122
  end
121
123
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonbAccessor
4
- VERSION = "1.3.4"
4
+ VERSION = "1.3.6"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonb_accessor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.4
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Crismali
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2022-02-02 00:00:00.000000000 Z
13
+ date: 2022-09-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord