rails_json_serializer 3.1.0 → 3.2.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 +4 -4
- data/lib/serializer/model_serializer.rb +38 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a17108258803bac91fbf2506e02b2953fe1d49c4ae3758acea6a6b8671a5cf0
|
4
|
+
data.tar.gz: f8e2ad23101fe3ba7a9253d6dbeec9d82b1c5f9392ce2df8c47db74326921858
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f95e665cd9adfc8c9900ba981d75af1b2448cb8c23baa41454b63aaeaf49e5fc04846ab60d18bbc2991491d3a7b310666a863ba830fad71fee18225a88db0f38
|
7
|
+
data.tar.gz: 8f7b66cce88ba721b58ce24a76d7a75791f61054ab0da38de23a495aa1f1d20c1d93b344cc4b393dc2479727c9d589bdfd4de807563ad8cf3465239aa4011901
|
@@ -4,6 +4,8 @@ module ModelSerializer
|
|
4
4
|
# START CLASS EVAL
|
5
5
|
klass.class_eval do
|
6
6
|
|
7
|
+
|
8
|
+
# I don't think we need to check for classname + serialiers. We are now including via: `include ModelSerializer` in classes
|
7
9
|
# Rails 5 has autoloading issues with modules. They will show as not defined, when available.
|
8
10
|
if Rails.env.development?
|
9
11
|
begin
|
@@ -38,9 +40,16 @@ module ModelSerializer
|
|
38
40
|
end
|
39
41
|
|
40
42
|
# no need to define it if inheriting class has defined it OR has been manually overridden.
|
41
|
-
# CLASS
|
42
|
-
klass.send(:define_singleton_method, :serializer) do |opts = {}|
|
43
|
+
# CLASS METHOD - :serializer
|
44
|
+
klass.send(:define_singleton_method, :serializer) do |opts = {}|
|
45
|
+
# puts "CLASS METHOD - SERIALIZER - OPTS: #{opts.inspect}"
|
43
46
|
query = opts[:json_query_override].present? ? self.send(opts[:json_query_override], opts) : serializer_query(opts)
|
47
|
+
|
48
|
+
# if as_json arg, then add to as_json query
|
49
|
+
if opts[:disable_caching] == true
|
50
|
+
query[:disable_caching] = true
|
51
|
+
end
|
52
|
+
|
44
53
|
if Serializer.configuration.enable_includes && query[:include].present? && !opts[:skip_eager_loading]
|
45
54
|
includes(generate_includes_from_json_query(query)).as_json(query)
|
46
55
|
else
|
@@ -126,8 +135,9 @@ module ModelSerializer
|
|
126
135
|
if !Serializer.configuration.disable_model_caching && self.id && options[:cache_key].present? && !(options.key?(:cache_for) && options[:cache_for].nil?)
|
127
136
|
cache_key = Serializer.configuration.cache_key.call(self.class.name, options[:cache_key], self.id)
|
128
137
|
|
129
|
-
if Rails.cache.exist?(cache_key)
|
138
|
+
if Rails.cache.exist?(cache_key) && options[:disable_caching] != true
|
130
139
|
Rails.logger.debug "Serializer: Cache reading #{cache_key}" if Serializer.configuration.debug
|
140
|
+
# Rails.logger.debug(options.inspect) if Serializer.configuration.debug
|
131
141
|
outgoing_data = Rails.cache.read(cache_key)
|
132
142
|
if (options.key?(:compress) && options[:compress] == true) || (!options.key?(:compress) && Serializer.configuration.compress)
|
133
143
|
outgoing_data = Serializer.configuration.decompressor.call(outgoing_data)
|
@@ -137,21 +147,25 @@ module ModelSerializer
|
|
137
147
|
data = super(options)
|
138
148
|
data = self.class.as_json_associations_alias_fix(options, data)
|
139
149
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
150
|
+
if options[:disable_caching] != true
|
151
|
+
# compress data
|
152
|
+
cachable_data = data
|
153
|
+
if (options.key?(:compress) && options[:compress] == true) || (!options.key?(:compress) && Serializer.configuration.compress)
|
154
|
+
cachable_data = Serializer.configuration.compressor.call(data)
|
155
|
+
end
|
156
|
+
begin
|
157
|
+
Rails.logger.debug "Serializer: Caching #{cache_key} for #{(options[:cache_for] || Serializer.configuration.default_cache_time)} minutes." if Serializer.configuration.debug
|
158
|
+
Rails.cache.write(cache_key, cachable_data, expires_in: (options[:cache_for] || Serializer.configuration.default_cache_time).minute)
|
159
|
+
rescue Exception => e
|
160
|
+
Rails.logger.error "Serializer: Internal Server Error on #{self.class}#as_json ID: #{self.id} for cache key: #{cache_key}"
|
161
|
+
Rails.logger.error e.class
|
162
|
+
Rails.logger.error e.message
|
163
|
+
Rails.logger.error e.backtrace
|
164
|
+
end
|
165
|
+
else
|
166
|
+
Rails.logger.debug "Serializer: Cache reading/writing for #{cache_key} is disabled" if Serializer.configuration.debug
|
144
167
|
end
|
145
168
|
|
146
|
-
begin
|
147
|
-
Rails.logger.debug "Serializer: Caching #{cache_key} for #{(options[:cache_for] || Serializer.configuration.default_cache_time)} minutes." if Serializer.configuration.debug
|
148
|
-
Rails.cache.write(cache_key, cachable_data, expires_in: (options[:cache_for] || Serializer.configuration.default_cache_time).minute)
|
149
|
-
rescue Exception => e
|
150
|
-
Rails.logger.error "Serializer: Internal Server Error on #{self.class}#as_json ID: #{self.id} for cache key: #{cache_key}"
|
151
|
-
Rails.logger.error e.class
|
152
|
-
Rails.logger.error e.message
|
153
|
-
Rails.logger.error e.backtrace
|
154
|
-
end
|
155
169
|
return data
|
156
170
|
end
|
157
171
|
else
|
@@ -164,9 +178,17 @@ module ModelSerializer
|
|
164
178
|
end
|
165
179
|
end
|
166
180
|
|
181
|
+
# INSTANCE METHOD - :serializer
|
167
182
|
if !klass.method_defined?(:serializer)
|
168
183
|
klass.send(:define_method, :serializer) do |opts = {}|
|
184
|
+
# puts "INSTANCE METHOD - SERIALIZER - OPTS: #{opts.inspect}"
|
169
185
|
query = opts[:json_query_override].present? ? self.class.send(opts[:json_query_override], opts) : self.class.serializer_query(opts)
|
186
|
+
|
187
|
+
# if as_json arg, then add to as_json query
|
188
|
+
if opts[:disable_caching] == true
|
189
|
+
query[:disable_caching] = true
|
190
|
+
end
|
191
|
+
|
170
192
|
if Serializer.configuration.enable_includes && query[:include].present? && self.class.column_names.include?('id') && self.id.present? && !opts[:skip_eager_loading] && self.respond_to?(:persisted?) && self.persisted?
|
171
193
|
# It's an extra SQL call, but most likely worth it to pre-load associations
|
172
194
|
self.class.includes(self.class.generate_includes_from_json_query(query)).find(self.id).as_json(query)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_json_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- benjamin.dana.software.dev@gmail.com
|
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
150
|
- !ruby/object:Gem::Version
|
151
151
|
version: '0'
|
152
152
|
requirements: []
|
153
|
-
rubygems_version: 3.1.
|
153
|
+
rubygems_version: 3.1.4
|
154
154
|
signing_key:
|
155
155
|
specification_version: 4
|
156
156
|
summary: An ActiveRecord JSON Serializer with supported caching and eager-loading
|