Skip to content

Commit defc701

Browse files
authored
Merge pull request #1269 from cerebris/performance
Performance
2 parents cf210f3 + 388824b commit defc701

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

lib/jsonapi/basic_resource.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ def inherited(subclass)
453453

454454
subclass._routed = false
455455
subclass._warned_missing_route = false
456+
457+
subclass._clear_cached_attribute_options
458+
subclass._clear_fields_cache
456459
end
457460

458461
def rebuild_relationships(relationships)
@@ -527,6 +530,9 @@ def attributes(*attrs)
527530
end
528531

529532
def attribute(attribute_name, options = {})
533+
_clear_cached_attribute_options
534+
_clear_fields_cache
535+
530536
attr = attribute_name.to_sym
531537

532538
check_reserved_attribute_name(attr)
@@ -693,7 +699,7 @@ def sortable_field?(key, context = nil)
693699
end
694700

695701
def fields
696-
_relationships.keys | _attributes.keys
702+
@_fields_cache ||= _relationships.keys | _attributes.keys
697703
end
698704

699705
def resources_for(records, context)
@@ -826,7 +832,7 @@ def verify_relationship_filter(filter, raw, _context = nil)
826832

827833
# quasi private class methods
828834
def _attribute_options(attr)
829-
default_attribute_options.merge(@_attributes[attr])
835+
@_cached_attribute_options[attr] ||= default_attribute_options.merge(@_attributes[attr])
830836
end
831837

832838
def _attribute_delegated_name(attr)
@@ -1063,6 +1069,8 @@ def construct_order_options(sort_params)
10631069
end
10641070

10651071
def _add_relationship(klass, *attrs)
1072+
_clear_fields_cache
1073+
10661074
options = attrs.extract_options!
10671075
options[:parent_resource] = self
10681076

@@ -1107,6 +1115,14 @@ def register_relationship(name, relationship_object)
11071115
@_relationships[name] = relationship_object
11081116
end
11091117

1118+
def _clear_cached_attribute_options
1119+
@_cached_attribute_options = {}
1120+
end
1121+
1122+
def _clear_fields_cache
1123+
@_fields_cache = nil
1124+
end
1125+
11101126
private
11111127

11121128
def check_reserved_resource_name(type, name)

lib/jsonapi/link_builder.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ def module_scopes_from_class(klass)
123123
end
124124

125125
def resources_path(source_klass)
126-
formatted_module_path_from_class(source_klass) + format_route(source_klass._type.to_s)
126+
@_resources_path ||= {}
127+
@_resources_path[source_klass] ||= formatted_module_path_from_class(source_klass) + format_route(source_klass._type.to_s)
127128
end
128129

129130
def resource_path(source)
130-
url = "#{resources_path(source.class)}"
131-
132-
unless source.class.singleton?
133-
url = "#{url}/#{source.id}"
131+
if source.class.singleton?
132+
resources_path(source.class)
133+
else
134+
"#{resources_path(source.class)}/#{source.id}"
134135
end
135-
url
136136
end
137137

138138
def resource_url(source)

lib/jsonapi/resource_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def attributes_hash(source, fetchable_fields)
230230
end
231231

232232
def custom_generation_options
233-
{
233+
@_custom_generation_options ||= {
234234
serializer: self,
235235
serialization_options: @serialization_options
236236
}

0 commit comments

Comments
 (0)