@@ -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 )
0 commit comments