Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/jsonapi/basic_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions lib/jsonapi/link_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/resource_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def attributes_hash(source, fetchable_fields)
end

def custom_generation_options
{
@_custom_generation_options ||= {
serializer: self,
serialization_options: @serialization_options
}
Expand Down