Skip to content

Commit 7d753ec

Browse files
committed
Cache fields
Reset the cache if the relationships or the attributes change
1 parent ca1baa0 commit 7d753ec

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/jsonapi/basic_resource.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ def inherited(subclass)
455455
subclass._warned_missing_route = false
456456

457457
subclass._clear_cached_attribute_options
458+
subclass._clear_fields_cache
458459
end
459460

460461
def rebuild_relationships(relationships)
@@ -530,6 +531,7 @@ def attributes(*attrs)
530531

531532
def attribute(attribute_name, options = {})
532533
_clear_cached_attribute_options
534+
_clear_fields_cache
533535

534536
attr = attribute_name.to_sym
535537

@@ -697,7 +699,7 @@ def sortable_field?(key, context = nil)
697699
end
698700

699701
def fields
700-
_relationships.keys | _attributes.keys
702+
@_fields_cache ||= _relationships.keys | _attributes.keys
701703
end
702704

703705
def resources_for(records, context)
@@ -1067,6 +1069,8 @@ def construct_order_options(sort_params)
10671069
end
10681070

10691071
def _add_relationship(klass, *attrs)
1072+
_clear_fields_cache
1073+
10701074
options = attrs.extract_options!
10711075
options[:parent_resource] = self
10721076

@@ -1115,6 +1119,10 @@ def _clear_cached_attribute_options
11151119
@_cached_attribute_options = {}
11161120
end
11171121

1122+
def _clear_fields_cache
1123+
@_fields_cache = nil
1124+
end
1125+
11181126
private
11191127

11201128
def check_reserved_resource_name(type, name)

0 commit comments

Comments
 (0)