added initial changes
This commit is contained in:
parent
7db80f673d
commit
dc874a0034
@ -50,7 +50,7 @@ module FastJsonapi
|
||||
|
||||
return serializable_hash unless @resource
|
||||
|
||||
serializable_hash[:data] = self.class.record_hash(@resource, @fieldsets[self.class.record_type.to_sym], @includes, @params)
|
||||
serializable_hash[:data] = self.class.record_hash(@resource, @fieldsets[self.class.record_type.to_sym], @includes, @params, @optional)
|
||||
serializable_hash[:included] = self.class.get_included_records(@resource, @includes, @known_included_objects, @fieldsets, @params) if @includes.present?
|
||||
serializable_hash
|
||||
end
|
||||
@ -62,7 +62,7 @@ module FastJsonapi
|
||||
included = []
|
||||
fieldset = @fieldsets[self.class.record_type.to_sym]
|
||||
@resource.each do |record|
|
||||
data << self.class.record_hash(record, fieldset, @includes, @params)
|
||||
data << self.class.record_hash(record, fieldset, @includes, @params, @optional)
|
||||
included.concat self.class.get_included_records(record, @includes, @known_included_objects, @fieldsets, @params) if @includes.present?
|
||||
end
|
||||
|
||||
@ -92,6 +92,11 @@ module FastJsonapi
|
||||
@includes = options[:include].reject(&:blank?).map(&:to_sym)
|
||||
self.class.validate_includes!(@includes)
|
||||
end
|
||||
|
||||
@optional = options[:optional] || []
|
||||
raise ArgumentError, '`optional` option passed to serializer must be an array' unless @optional.is_a?(Array)
|
||||
|
||||
@optional.map! { |x| x.to_s.to_sym }
|
||||
end
|
||||
|
||||
def deep_symbolize(collection)
|
||||
@ -216,6 +221,13 @@ module FastJsonapi
|
||||
options: options
|
||||
)
|
||||
end
|
||||
|
||||
key = run_key_transform(attributes_list[0])
|
||||
attributes_to_serialize[key] = Attribute.new(
|
||||
key: key,
|
||||
method: block || attributes_list[0],
|
||||
options: options
|
||||
)
|
||||
end
|
||||
|
||||
alias_method :attribute, :attributes
|
||||
|
@ -1,15 +1,16 @@
|
||||
module FastJsonapi
|
||||
class Scalar
|
||||
attr_reader :key, :method, :conditional_proc
|
||||
attr_reader :key, :method, :conditional_proc, :optional
|
||||
|
||||
def initialize(key:, method:, options: {})
|
||||
@key = key
|
||||
@method = method
|
||||
@conditional_proc = options[:if]
|
||||
@optional = options[:optional] || false
|
||||
end
|
||||
|
||||
def serialize(record, serialization_params, output_hash)
|
||||
if conditionally_allowed?(record, serialization_params)
|
||||
def serialize(record, serialization_params, output_hash, allowed_optionals)
|
||||
if conditionally_allowed?(record, serialization_params, allowed_optionals)
|
||||
if method.is_a?(Proc)
|
||||
output_hash[key] = FastJsonapi.call_proc(method, record, serialization_params)
|
||||
else
|
||||
@ -18,7 +19,9 @@ module FastJsonapi
|
||||
end
|
||||
end
|
||||
|
||||
def conditionally_allowed?(record, serialization_params)
|
||||
def conditionally_allowed?(record, serialization_params, allowed_optionals)
|
||||
return false if optional.present? && !allowed_optionals.include?(@key)
|
||||
|
||||
if conditional_proc.present?
|
||||
FastJsonapi.call_proc(conditional_proc, record, serialization_params)
|
||||
else
|
||||
|
@ -41,13 +41,13 @@ module FastJsonapi
|
||||
end
|
||||
end
|
||||
|
||||
def attributes_hash(record, fieldset = nil, params = {})
|
||||
def attributes_hash(record, fieldset = nil, params = {}, optional= [])
|
||||
attributes = attributes_to_serialize
|
||||
attributes = attributes.slice(*fieldset) if fieldset.present?
|
||||
attributes = {} if fieldset == []
|
||||
|
||||
attributes.each_with_object({}) do |(_k, attribute), hash|
|
||||
attribute.serialize(record, params, hash)
|
||||
attribute.serialize(record, params, hash, optional)
|
||||
end
|
||||
end
|
||||
|
||||
@ -66,12 +66,12 @@ module FastJsonapi
|
||||
FastJsonapi.call_proc(meta_to_serialize, record, params)
|
||||
end
|
||||
|
||||
def record_hash(record, fieldset, includes_list, params = {})
|
||||
def record_hash(record, fieldset, includes_list, params = {}, optional = [])
|
||||
if cache_store_instance
|
||||
cache_opts = record_cache_options(cache_store_options, fieldset, includes_list, params)
|
||||
record_hash = cache_store_instance.fetch(record, **cache_opts) do
|
||||
temp_hash = id_hash(id_from_record(record, params), record_type, true)
|
||||
temp_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present?
|
||||
temp_hash[:attributes] = attributes_hash(record, fieldset, params, optional) if attributes_to_serialize.present?
|
||||
temp_hash[:relationships] = relationships_hash(record, cachable_relationships_to_serialize, fieldset, includes_list, params) if cachable_relationships_to_serialize.present?
|
||||
temp_hash[:links] = links_hash(record, params) if data_links.present?
|
||||
temp_hash
|
||||
@ -79,7 +79,7 @@ module FastJsonapi
|
||||
record_hash[:relationships] = (record_hash[:relationships] || {}).merge(relationships_hash(record, uncachable_relationships_to_serialize, fieldset, includes_list, params)) if uncachable_relationships_to_serialize.present?
|
||||
else
|
||||
record_hash = id_hash(id_from_record(record, params), record_type, true)
|
||||
record_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present?
|
||||
record_hash[:attributes] = attributes_hash(record, fieldset, params, optional) if attributes_to_serialize.present?
|
||||
record_hash[:relationships] = relationships_hash(record, nil, fieldset, includes_list, params) if relationships_to_serialize.present?
|
||||
record_hash[:links] = links_hash(record, params) if data_links.present?
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user