active_record_to_hash 1.1.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,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6fd8387c13ea938de4bd07f3136535a99c11f294e06f895565da55970e916a6
|
4
|
+
data.tar.gz: 1ed0ac8096bc6ff084516be1fb130e43a34d7578044a043da73543bf640ada14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df5703d7c50fa512ec6d4294a07a967a81c4faa1fb827fefc3e0cec25c3dca038bd7a2c557353fe00d235e6caa4414d8aa01a394d48f10ff1a19bcf934029e97
|
7
|
+
data.tar.gz: fe940a5caee54389c0641b12c3e64057686b6acc7e7232b42c6e16c45cebb6dbe710be35a8d1a184305101f151c7e2e4c2e6ff386d115f683baf278d61af5a62
|
data/README.md
CHANGED
@@ -169,6 +169,10 @@ p shop.to_hash(only: [:id, :name], with_areas: :exists)
|
|
169
169
|
# {:id=>1, :name=>"Shop No1", :areas=>true}
|
170
170
|
# You can use this option only with ActiveRecord::Relation
|
171
171
|
|
172
|
+
|
173
|
+
p shop.to_hash(ignore_nil: true)
|
174
|
+
# When the value is nil, it skips that key.
|
175
|
+
|
172
176
|
```
|
173
177
|
|
174
178
|
### Configuration
|
@@ -15,6 +15,7 @@ module ActiveRecordToHash
|
|
15
15
|
key = k.to_sym
|
16
16
|
next if ActiveRecordToHash.to_a(options[:except]).include?(key)
|
17
17
|
next if options[:only] && !ActiveRecordToHash.to_a(options[:only]).include?(key)
|
18
|
+
next if options[:ignore_nil] && v.nil?
|
18
19
|
|
19
20
|
memo[key] = v
|
20
21
|
end
|
@@ -23,6 +24,9 @@ module ActiveRecordToHash
|
|
23
24
|
|
24
25
|
ActiveRecordToHash.handle_with_options(options) do |hash_key, attr_name, child_options|
|
25
26
|
child = ActiveRecordToHash.retrieve_child_attribute(self, attr_name, child_options, __callee__)
|
27
|
+
next if child_options[:optional] == true && child.blank?
|
28
|
+
next if options[:ignore_nil] && child.nil?
|
29
|
+
|
26
30
|
result[hash_key] = ActiveRecordToHash.handle_alter(child, child_options)
|
27
31
|
end
|
28
32
|
|
@@ -20,7 +20,7 @@ module ActiveRecordToHash
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def retrieve_child_attribute(record, attr_name, options, callee) # rubocop:disable Metrics/AbcSize
|
23
|
-
if options
|
23
|
+
if options.key?(:value)
|
24
24
|
return options[:value].call(record) if options[:value].is_a? Proc
|
25
25
|
|
26
26
|
return options[:value]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_to_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masamoto Miyata
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-07
|
11
|
+
date: 2022-09-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|