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
1 change: 1 addition & 0 deletions jsonapi-resources.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'pry'
spec.add_development_dependency 'concurrent-ruby-ext'
spec.add_development_dependency 'database_cleaner'
spec.add_development_dependency 'memory_profiler'
Copy link
Copy Markdown
Contributor

@hesalx hesalx Nov 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be included in the gem itself.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hesalx It seems like keeping the memory_profiler included as a development dependency should be safe and useful. What's your reasoning for not including it?

spec.add_dependency 'activerecord', '>= 4.1'
spec.add_dependency 'railties', '>= 4.1'
spec.add_dependency 'concurrent-ruby'
Expand Down
23 changes: 13 additions & 10 deletions lib/jsonapi/link_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,19 @@ def format_route(route)
end

def formatted_module_path_from_class(klass)
scopes = if @engine
module_scopes_from_class(klass)[1..-1]
else
module_scopes_from_class(klass)
end

unless scopes.empty?
"/#{ scopes.map {|scope| format_route(scope.to_s.underscore)}.compact.join('/') }/"
else
"/"
@_module_path_cache ||= {}
@_module_path_cache[klass] ||= begin
scopes = if @engine
module_scopes_from_class(klass)[1..-1]
else
module_scopes_from_class(klass)
end

unless scopes.empty?
"/#{ scopes.map {|scope| format_route(scope.to_s.underscore)}.compact.join('/') }/"
else
"/"
end
end
end

Expand Down
7 changes: 5 additions & 2 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module JSONAPI
class Resource
include Callbacks

DEFAULT_ATTRIBUTE_OPTIONS = { format: :default }.freeze
MODULE_PATH_REGEXP = /::[^:]+\Z/.freeze

attr_reader :context

define_jsonapi_resources_callbacks :create,
Expand Down Expand Up @@ -555,7 +558,7 @@ def attribute(attribute_name, options = {})
end

def default_attribute_options
{ format: :default }
DEFAULT_ATTRIBUTE_OPTIONS
end

def relationship(*attrs)
Expand Down Expand Up @@ -1133,7 +1136,7 @@ def module_path
if name == 'JSONAPI::Resource'
''
else
name =~ /::[^:]+\Z/ ? ($`.freeze.gsub('::', '/') + '/').underscore : ''
name =~ MODULE_PATH_REGEXP ? ($`.freeze.gsub('::', '/') + '/').underscore : ''
end
end

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 @@ -287,7 +287,6 @@ def relationships_hash(source, fetchable_fields, include_directives = {})
include_linkage = ia && ia[:include]
include_linked_children = ia && !ia[:include_related].empty?

options = { filters: ia && ia[:include_filters] || {} }
if field_set.include?(name)
ro = relationship_object(source, relationship, include_linkage)
hash[format_key(name)] = ro unless ro.blank?
Expand All @@ -300,6 +299,7 @@ def relationships_hash(source, fetchable_fields, include_directives = {})
resources = if source.preloaded_fragments.has_key?(format_key(name))
source.preloaded_fragments[format_key(name)].values
else
options = { filters: ia && ia[:include_filters] || {} }
[source.public_send(name, options)].flatten(1).compact
end
resources.each do |resource|
Expand Down
4 changes: 2 additions & 2 deletions test/benchmark/request_benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ def setup
end

def bench_large_index_request_uncached
10.times do
100.times do
assert_jsonapi_get '/api/v2/books?include=bookComments,bookComments.author'
end
end

def bench_large_index_request_caching
cache = ActiveSupport::Cache::MemoryStore.new
with_resource_caching(cache) do
10.times do
100.times do
assert_jsonapi_get '/api/v2/books?include=bookComments,bookComments.author'
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require 'minitest/mock'
require 'jsonapi-resources'
require 'pry'
require 'memory_profiler'

require File.expand_path('../helpers/value_matchers', __FILE__)
require File.expand_path('../helpers/assertions', __FILE__)
Expand Down Expand Up @@ -639,6 +640,9 @@ def self.run_one_method(klass, method_name, reporter)
end
end
puts
if ENV["MEMORY_PROFILER"]
MemoryProfiler.report(allow_files: 'lib/jsonapi') { super(klass, method_name, reporter) }.pretty_print
end
end
end

Expand Down