diff --git a/lib/jsonapi/basic_resource.rb b/lib/jsonapi/basic_resource.rb index da9042658..ea8b19ea7 100644 --- a/lib/jsonapi/basic_resource.rb +++ b/lib/jsonapi/basic_resource.rb @@ -453,6 +453,9 @@ def inherited(subclass) subclass._routed = false subclass._warned_missing_route = false + + subclass._clear_cached_attribute_options + subclass._clear_fields_cache end def rebuild_relationships(relationships) @@ -527,6 +530,9 @@ def attributes(*attrs) end def attribute(attribute_name, options = {}) + _clear_cached_attribute_options + _clear_fields_cache + attr = attribute_name.to_sym check_reserved_attribute_name(attr) @@ -693,7 +699,7 @@ def sortable_field?(key, context = nil) end def fields - _relationships.keys | _attributes.keys + @_fields_cache ||= _relationships.keys | _attributes.keys end def resources_for(records, context) @@ -826,7 +832,7 @@ def verify_relationship_filter(filter, raw, _context = nil) # quasi private class methods def _attribute_options(attr) - default_attribute_options.merge(@_attributes[attr]) + @_cached_attribute_options[attr] ||= default_attribute_options.merge(@_attributes[attr]) end def _attribute_delegated_name(attr) @@ -1063,6 +1069,8 @@ def construct_order_options(sort_params) end def _add_relationship(klass, *attrs) + _clear_fields_cache + options = attrs.extract_options! options[:parent_resource] = self @@ -1107,6 +1115,14 @@ def register_relationship(name, relationship_object) @_relationships[name] = relationship_object end + def _clear_cached_attribute_options + @_cached_attribute_options = {} + end + + def _clear_fields_cache + @_fields_cache = nil + end + private def check_reserved_resource_name(type, name) diff --git a/lib/jsonapi/link_builder.rb b/lib/jsonapi/link_builder.rb index 99cd6223c..6ede8a022 100644 --- a/lib/jsonapi/link_builder.rb +++ b/lib/jsonapi/link_builder.rb @@ -123,16 +123,16 @@ def module_scopes_from_class(klass) end def resources_path(source_klass) - formatted_module_path_from_class(source_klass) + format_route(source_klass._type.to_s) + @_resources_path ||= {} + @_resources_path[source_klass] ||= formatted_module_path_from_class(source_klass) + format_route(source_klass._type.to_s) end def resource_path(source) - url = "#{resources_path(source.class)}" - - unless source.class.singleton? - url = "#{url}/#{source.id}" + if source.class.singleton? + resources_path(source.class) + else + "#{resources_path(source.class)}/#{source.id}" end - url end def resource_url(source) diff --git a/lib/jsonapi/resource_serializer.rb b/lib/jsonapi/resource_serializer.rb index e5749a98e..5751adb67 100644 --- a/lib/jsonapi/resource_serializer.rb +++ b/lib/jsonapi/resource_serializer.rb @@ -230,7 +230,7 @@ def attributes_hash(source, fetchable_fields) end def custom_generation_options - { + @_custom_generation_options ||= { serializer: self, serialization_options: @serialization_options }