Skip to content

Commit a841aa9

Browse files
committed
add comments
1 parent 6581201 commit a841aa9

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

lib/rails_jsonapi.rb

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module RailsJSONAPI
1212

1313
@class_to_serializer_proc = ->(klass){ "#{klass.name}Serializer".constantize }
1414

15+
# deserializer must return [Hash{Symbol => *}]
1516
@type_to_deserializer_proc = ->(type){ "#{type.underscore.classify}Deserializer".constantize }
1617

1718
class << self

lib/rails_jsonapi/controller.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def json_api_request?
5454
#
5555
# @example `GET /resource?fields[relationship]=id,created_at`
5656
#
57-
# @return [Hash]
57+
# @return [Hash|ActiveSupport::HashWithIndifferentAccess]
5858
def jsonapi_fields_param
5959
return {} unless params[:fields].respond_to?(:each_pair)
6060

@@ -95,6 +95,10 @@ def self.included(base)
9595
end
9696

9797
module ClassMethods
98+
# @param resource [ActionController::Parameters]
99+
# @param lid_key [String]
100+
# @param lid_regex [Regexp]
101+
# @return [Hash{Symbol => *}]
98102
def deep_deserialize_jsonapi(resource, lid_key, lid_regex)
99103
resource = resource.as_json if resource.is_a?(ActionController::Parameters)
100104
RailsJSONAPI::DeepDeserializer.new(resource, lid_key, lid_regex)

lib/rails_jsonapi/deep_deserializer.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module RailsJSONAPI
22
class DeepDeserializer
33

4-
# @param [Hash] resource jsonapi payload
4+
# @param [Hash{String => *}] resource jsonapi payload
55
# @param [String] lid_key
66
# @param [String] lid_regex
77
def initialize(resource, lid_key, lid_regex)
@@ -10,12 +10,16 @@ def initialize(resource, lid_key, lid_regex)
1010
@lid_regex = lid_regex
1111
end
1212

13+
# @param [Hash{String => *}] resource jsonapi payload
14+
# @return [Hash{Symbol => *}]
1315
def deep_deserialize(resource = @resource)
1416
data = resource.key?('data') ? resource['data'] : resource
1517
included = resource['included']
1618

1719
normalize_resource(data)
1820
klass_deserializer = RailsJSONAPI.type_to_deserializer_proc.call(data['type'])
21+
22+
# Hash{Symbol => *}
1923
deserialized = klass_deserializer.call(data)
2024

2125
# remove id if local
@@ -43,11 +47,11 @@ def deep_deserialize(resource = @resource)
4347

4448
private
4549

46-
# Transforms *attributes* and *relationships* with underscore and sets the local-id into attributes
50+
# Transforms *attributes* and *relationships* with underscore and sets the local id *lid* into attributes
4751
#
4852
# @todo is underscoring necessary? can this be handled by jsonapi-deserializable?
4953
#
50-
# @param [Hash] data
54+
# @param [Hash{String => *}] data
5155
# @return [void]
5256
def normalize_resource(data)
5357
data['attributes'] = data['attributes'].transform_keys{|k| k.underscore } if data['attributes'].present?

lib/rails_jsonapi/error_serializer/active_model.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module ErrorSerializer
88
# [...]
99
# ]
1010
#
11+
# @note requires record to respond to +lid_id+
12+
#
1113
class ActiveModel < Base
1214
set_id :object_id
1315
set_type :error
@@ -40,7 +42,7 @@ class ActiveModel < Base
4042
attribute = path.pop
4143
record = get_record_from_errors_path(params[:record], path)
4244
if record
43-
"(#{I18n.t("activerecord.models.#{record.class.name.underscore}.one")} #{(record.id || record.lid_id)}) " + record.errors.full_message(attribute, error_msg)
45+
"(#{I18n.t("activerecord.models.#{record.class.name.underscore}.one")} #{record.id || record.try(:lid_id)}) " + record.errors.full_message(attribute, error_msg)
4446
else
4547
errors_object.full_message(error_key, error_msg)
4648
end

0 commit comments

Comments
 (0)