lib/jsonapi/resource.rb in jsonapi-resources-0.9.10 vs lib/jsonapi/resource.rb in jsonapi-resources-0.9.11
- old
+ new
@@ -3,10 +3,13 @@
module JSONAPI
class Resource
include Callbacks
+ DEFAULT_ATTRIBUTE_OPTIONS = { format: :default }.freeze
+ MODULE_PATH_REGEXP = /::[^:]+\Z/.freeze
+
attr_reader :context
define_jsonapi_resources_callbacks :create,
:update,
:remove,
@@ -553,11 +556,11 @@
@model.public_send("#{options[:delegate] ? options[:delegate].to_sym : attr}=", value)
end unless method_defined?("#{attr}=")
end
def default_attribute_options
- { format: :default }
+ DEFAULT_ATTRIBUTE_OPTIONS
end
def relationship(*attrs)
options = attrs.extract_options!
klass = case options[:to]
@@ -1131,11 +1134,11 @@
def module_path
if name == 'JSONAPI::Resource'
''
else
- name =~ /::[^:]+\Z/ ? ($`.freeze.gsub('::', '/') + '/').underscore : ''
+ name =~ MODULE_PATH_REGEXP ? ($`.freeze.gsub('::', '/') + '/').underscore : ''
end
end
def default_sort
[{field: 'id', direction: :asc}]
@@ -1349,10 +1352,11 @@
path.each_with_index do |rel_name, index|
rel_name = serializer.key_formatter.format(rel_name)
rel_id = row[index+1]
assoc_rels = res.preloaded_fragments[rel_name]
if index == path.length - 1
- assoc_rels[rel_id] = target_resources[klass.name].fetch(rel_id)
+ association_res = target_resources[klass.name].fetch(rel_id, nil)
+ assoc_rels[rel_id] = association_res if association_res
else
res = assoc_rels[rel_id]
end
end
end