-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V0.11 refactor resource classes to modules (#1406)
* Restore previous include directives behavior * Default sort use _primary_key * Remove support for pluck attributes * Pass relationship instead of relationship name * Update copyright date * Ignore docker-compose override files * add _relation_name method * Rework resource class to support using modules for retrieving resources by way of a `resource_retrieval_strategy` Removes BasicResource class and replaces ActiveRelationResource with a module * Use `_relationship` helper method * Add ActiveRelationRetrieval Allows retrieval of resources by querying the primary table and joining the source table - the opposite of the v10 version * Skip extra pluck queries when not caching a resource * Test Cleanup * Adjust tested query counts based on default_resource_retrieval_strategy * create_implicit_polymorphic_type_relationships * Add ActiveRelationRetrievalV09 * Move resource down in the load order * Use underscore instead of downcase * Refactor Resource to load retrieval strategy as class loads * Simplify loading resource retrieval strategy modules Add SimpleResource that does not load a resource retrieval strategy module * Remove no longer need deferred_relationship code * Add warning about potentially unused `records_for_populate` * Rework loading the resource_retrieval_strategy to fix issue in real projects * Use SortedSets for resource_identities * Add sorted_set gem * Remove rails 5 support
Showing
42 changed files
with
5,690 additions
and
3,056 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,35 +47,21 @@ jobs: | |
- '7.0' | ||
- '6.1' | ||
- '6.0' | ||
- '5.2' | ||
- '5.1' | ||
database_url: | ||
- sqlite3:test_db | ||
- postgresql://postgres:password@localhost:5432/test | ||
- mysql2://root:[email protected]:3306/test | ||
exclude: | ||
- ruby: '3.2' | ||
rails: '6.0' | ||
- ruby: '3.2' | ||
rails: '5.2' | ||
- ruby: '3.2' | ||
rails: '5.1' | ||
- ruby: '3.1' | ||
rails: '6.0' | ||
- ruby: '3.1' | ||
rails: '5.2' | ||
- ruby: '3.1' | ||
rails: '5.1' | ||
- ruby: '3.0' | ||
rails: '6.0' | ||
- ruby: '3.0' | ||
rails: '5.2' | ||
- ruby: '3.0' | ||
rails: '5.1' | ||
- ruby: '2.6' | ||
rails: '7.0' | ||
- database_url: postgresql://postgres:password@localhost:5432/test | ||
rails: '5.1' | ||
env: | ||
RAILS_VERSION: ${{ matrix.rails }} | ||
DATABASE_URL: ${{ matrix.database_url }} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ test_db | |
test_db-journal | ||
.idea | ||
*.iml | ||
*.override.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Copyright (c) 2014-2021 Cerebris Corporation | ||
Copyright (c) 2014-2023 Cerebris Corporation | ||
|
||
MIT License | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,297 @@ | ||
module JSONAPI | ||
module ActiveRelation | ||
|
||
# Stores relationship paths starting from the resource_klass, consolidating duplicate paths from | ||
# relationships, filters and sorts. When joins are made the table aliases are tracked in join_details | ||
class JoinManagerV10 | ||
attr_reader :resource_klass, | ||
:source_relationship, | ||
:resource_join_tree, | ||
:join_details | ||
|
||
def initialize(resource_klass:, | ||
source_relationship: nil, | ||
relationships: nil, | ||
filters: nil, | ||
sort_criteria: nil) | ||
|
||
@resource_klass = resource_klass | ||
@join_details = nil | ||
@collected_aliases = Set.new | ||
|
||
@resource_join_tree = { | ||
root: { | ||
join_type: :root, | ||
resource_klasses: { | ||
resource_klass => { | ||
relationships: {} | ||
} | ||
} | ||
} | ||
} | ||
add_source_relationship(source_relationship) | ||
add_sort_criteria(sort_criteria) | ||
add_filters(filters) | ||
add_relationships(relationships) | ||
end | ||
|
||
def join(records, options) | ||
fail "can't be joined again" if @join_details | ||
@join_details = {} | ||
perform_joins(records, options) | ||
end | ||
|
||
# source details will only be on a relationship if the source_relationship is set | ||
# this method gets the join details whether they are on a relationship or are just pseudo details for the base | ||
# resource. Specify the resource type for polymorphic relationships | ||
# | ||
def source_join_details(type=nil) | ||
if source_relationship | ||
related_resource_klass = type ? resource_klass.resource_klass_for(type) : source_relationship.resource_klass | ||
segment = PathSegment::Relationship.new(relationship: source_relationship, resource_klass: related_resource_klass) | ||
details = @join_details[segment] | ||
else | ||
if type | ||
details = @join_details["##{type}"] | ||
else | ||
details = @join_details[''] | ||
end | ||
end | ||
details | ||
end | ||
|
||
def join_details_by_polymorphic_relationship(relationship, type) | ||
segment = PathSegment::Relationship.new(relationship: relationship, resource_klass: resource_klass.resource_klass_for(type)) | ||
@join_details[segment] | ||
end | ||
|
||
def join_details_by_relationship(relationship) | ||
segment = PathSegment::Relationship.new(relationship: relationship, resource_klass: relationship.resource_klass) | ||
@join_details[segment] | ||
end | ||
|
||
def self.get_join_arel_node(records, options = {}) | ||
init_join_sources = records.arel.join_sources | ||
init_join_sources_length = init_join_sources.length | ||
|
||
records = yield(records, options) | ||
|
||
join_sources = records.arel.join_sources | ||
if join_sources.length > init_join_sources_length | ||
last_join = (join_sources - init_join_sources).last | ||
else | ||
# :nocov: | ||
warn "get_join_arel_node: No join added" | ||
last_join = nil | ||
# :nocov: | ||
end | ||
|
||
return records, last_join | ||
end | ||
|
||
def self.alias_from_arel_node(node) | ||
case node.left | ||
when Arel::Table | ||
node.left.name | ||
when Arel::Nodes::TableAlias | ||
node.left.right | ||
when Arel::Nodes::StringJoin | ||
# :nocov: | ||
warn "alias_from_arel_node: Unsupported join type - use custom filtering and sorting" | ||
nil | ||
# :nocov: | ||
end | ||
end | ||
|
||
private | ||
|
||
def flatten_join_tree_by_depth(join_array = [], node = @resource_join_tree, level = 0) | ||
join_array[level] = [] unless join_array[level] | ||
|
||
node.each do |relationship, relationship_details| | ||
relationship_details[:resource_klasses].each do |related_resource_klass, resource_details| | ||
join_array[level] << { relationship: relationship, | ||
relationship_details: relationship_details, | ||
related_resource_klass: related_resource_klass} | ||
flatten_join_tree_by_depth(join_array, resource_details[:relationships], level+1) | ||
end | ||
end | ||
join_array | ||
end | ||
|
||
def add_join_details(join_key, details, check_for_duplicate_alias = true) | ||
fail "details already set" if @join_details.has_key?(join_key) | ||
@join_details[join_key] = details | ||
|
||
# Joins are being tracked as they are added to the built up relation. If the same table is added to a | ||
# relation more than once subsequent versions will be assigned an alias. Depending on the order the joins | ||
# are made the computed aliases may change. The order this library performs the joins was chosen | ||
# to prevent this. However if the relation is reordered it should result in reusing on of the earlier | ||
# aliases (in this case a plain table name). The following check will catch this an raise an exception. | ||
# An exception is appropriate because not using the correct alias could leak data due to filters and | ||
# applied permissions being performed on the wrong data. | ||
if check_for_duplicate_alias && @collected_aliases.include?(details[:alias]) | ||
fail "alias '#{details[:alias]}' has already been added. Possible relation reordering" | ||
end | ||
|
||
@collected_aliases << details[:alias] | ||
end | ||
|
||
def perform_joins(records, options) | ||
join_array = flatten_join_tree_by_depth | ||
|
||
join_array.each do |level_joins| | ||
level_joins.each do |join_details| | ||
relationship = join_details[:relationship] | ||
relationship_details = join_details[:relationship_details] | ||
related_resource_klass = join_details[:related_resource_klass] | ||
join_type = relationship_details[:join_type] | ||
|
||
if relationship == :root | ||
unless source_relationship | ||
add_join_details('', {alias: resource_klass._table_name, join_type: :root}) | ||
end | ||
next | ||
end | ||
|
||
records, join_node = self.class.get_join_arel_node(records, options) {|records, options| | ||
related_resource_klass.join_relationship( | ||
records: records, | ||
resource_type: related_resource_klass._type, | ||
join_type: join_type, | ||
relationship: relationship, | ||
options: options) | ||
} | ||
|
||
details = {alias: self.class.alias_from_arel_node(join_node), join_type: join_type} | ||
|
||
if relationship == source_relationship | ||
if relationship.polymorphic? && relationship.belongs_to? | ||
add_join_details("##{related_resource_klass._type}", details) | ||
else | ||
add_join_details('', details) | ||
end | ||
end | ||
|
||
# We're adding the source alias with two keys. We only want the check for duplicate aliases once. | ||
# See the note in `add_join_details`. | ||
check_for_duplicate_alias = !(relationship == source_relationship) | ||
add_join_details(PathSegment::Relationship.new(relationship: relationship, resource_klass: related_resource_klass), details, check_for_duplicate_alias) | ||
end | ||
end | ||
records | ||
end | ||
|
||
def add_join(path, default_type = :inner, default_polymorphic_join_type = :left) | ||
if source_relationship | ||
if source_relationship.polymorphic? | ||
# Polymorphic paths will come it with the resource_type as the first segment (for example `#documents.comments`) | ||
# We just need to prepend the relationship portion the | ||
sourced_path = "#{source_relationship.name}#{path}" | ||
else | ||
sourced_path = "#{source_relationship.name}.#{path}" | ||
end | ||
else | ||
sourced_path = path | ||
end | ||
|
||
join_manager, _field = parse_path_to_tree(sourced_path, resource_klass, default_type, default_polymorphic_join_type) | ||
|
||
@resource_join_tree[:root].deep_merge!(join_manager) { |key, val, other_val| | ||
if key == :join_type | ||
if val == other_val | ||
val | ||
else | ||
:inner | ||
end | ||
end | ||
} | ||
end | ||
|
||
def process_path_to_tree(path_segments, resource_klass, default_join_type, default_polymorphic_join_type) | ||
node = { | ||
resource_klasses: { | ||
resource_klass => { | ||
relationships: {} | ||
} | ||
} | ||
} | ||
|
||
segment = path_segments.shift | ||
|
||
if segment.is_a?(PathSegment::Relationship) | ||
node[:resource_klasses][resource_klass][:relationships][segment.relationship] ||= {} | ||
|
||
# join polymorphic as left joins | ||
node[:resource_klasses][resource_klass][:relationships][segment.relationship][:join_type] ||= | ||
segment.relationship.polymorphic? ? default_polymorphic_join_type : default_join_type | ||
|
||
segment.relationship.resource_types.each do |related_resource_type| | ||
related_resource_klass = resource_klass.resource_klass_for(related_resource_type) | ||
|
||
# If the resource type was specified in the path segment we want to only process the next segments for | ||
# that resource type, otherwise process for all | ||
process_all_types = !segment.path_specified_resource_klass? | ||
|
||
if process_all_types || related_resource_klass == segment.resource_klass | ||
related_resource_tree = process_path_to_tree(path_segments.dup, related_resource_klass, default_join_type, default_polymorphic_join_type) | ||
node[:resource_klasses][resource_klass][:relationships][segment.relationship].deep_merge!(related_resource_tree) | ||
end | ||
end | ||
end | ||
node | ||
end | ||
|
||
def parse_path_to_tree(path_string, resource_klass, default_join_type = :inner, default_polymorphic_join_type = :left) | ||
path = JSONAPI::Path.new(resource_klass: resource_klass, path_string: path_string) | ||
|
||
field = path.segments[-1] | ||
return process_path_to_tree(path.segments, resource_klass, default_join_type, default_polymorphic_join_type), field | ||
end | ||
|
||
def add_source_relationship(source_relationship) | ||
@source_relationship = source_relationship | ||
|
||
if @source_relationship | ||
resource_klasses = {} | ||
source_relationship.resource_types.each do |related_resource_type| | ||
related_resource_klass = resource_klass.resource_klass_for(related_resource_type) | ||
resource_klasses[related_resource_klass] = {relationships: {}} | ||
end | ||
|
||
join_type = source_relationship.polymorphic? ? :left : :inner | ||
|
||
@resource_join_tree[:root][:resource_klasses][resource_klass][:relationships][@source_relationship] = { | ||
source: true, resource_klasses: resource_klasses, join_type: join_type | ||
} | ||
end | ||
end | ||
|
||
def add_filters(filters) | ||
return if filters.blank? | ||
filters.each_key do |filter| | ||
# Do not add joins for filters with an apply callable. This can be overridden by setting perform_joins to true | ||
next if resource_klass._allowed_filters[filter].try(:[], :apply) && | ||
!resource_klass._allowed_filters[filter].try(:[], :perform_joins) | ||
|
||
add_join(filter, :left) | ||
end | ||
end | ||
|
||
def add_sort_criteria(sort_criteria) | ||
return if sort_criteria.blank? | ||
|
||
sort_criteria.each do |sort| | ||
add_join(sort[:field], :left) | ||
end | ||
end | ||
|
||
def add_relationships(relationships) | ||
return if relationships.blank? | ||
relationships.each do |relationship| | ||
add_join(relationship, :left) | ||
end | ||
end | ||
end | ||
end | ||
end |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
168 changes: 53 additions & 115 deletions
168
lib/jsonapi/active_relation_resource.rb → lib/jsonapi/active_relation_retrieval_v10.rb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
require 'jsonapi/callbacks' | ||
require 'jsonapi/configuration' | ||
|
||
module JSONAPI | ||
class Resource < ActiveRelationResource | ||
class Resource | ||
include ResourceCommon | ||
root_resource | ||
abstract | ||
immutable | ||
end | ||
end | ||
end |
247 changes: 172 additions & 75 deletions
247
lib/jsonapi/basic_resource.rb → lib/jsonapi/resource_common.rb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'jsonapi/callbacks' | ||
require 'jsonapi/configuration' | ||
|
||
module JSONAPI | ||
class SimpleResource | ||
include ResourceCommon | ||
root_resource | ||
abstract | ||
immutable | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 222 additions & 0 deletions
222
test/unit/active_relation_resource_finder/join_manager_v10_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
require File.expand_path('../../../test_helper', __FILE__) | ||
require 'jsonapi-resources' | ||
|
||
class JoinManagerV10Test < ActiveSupport::TestCase | ||
def test_no_added_joins | ||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PostResource) | ||
|
||
records = PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
assert_equal 'SELECT "posts".* FROM "posts"', sql_for_compare(records.to_sql) | ||
|
||
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details) | ||
end | ||
|
||
def test_add_single_join | ||
filters = {'tags' => ['1']} | ||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PostResource, filters: filters) | ||
records = PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"', sql_for_compare(records.to_sql) | ||
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags))) | ||
end | ||
|
||
def test_add_single_sort_join | ||
sort_criteria = [{field: 'tags.name', direction: :desc}] | ||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PostResource, sort_criteria: sort_criteria) | ||
records = PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"', sql_for_compare(records.to_sql) | ||
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags))) | ||
end | ||
|
||
def test_add_single_sort_and_filter_join | ||
filters = {'tags' => ['1']} | ||
sort_criteria = [{field: 'tags.name', direction: :desc}] | ||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PostResource, sort_criteria: sort_criteria, filters: filters) | ||
records = PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"', sql_for_compare(records.to_sql) | ||
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags))) | ||
end | ||
|
||
def test_add_sibling_joins | ||
filters = { | ||
'tags' => ['1'], | ||
'author' => ['1'] | ||
} | ||
|
||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PostResource, filters: filters) | ||
records = PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id"', sql_for_compare(records.to_sql) | ||
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags))) | ||
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:author))) | ||
end | ||
|
||
|
||
def test_add_joins_source_relationship | ||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PostResource, | ||
source_relationship: PostResource._relationship(:comments)) | ||
records = PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"', sql_for_compare(records.to_sql) | ||
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details) | ||
end | ||
|
||
|
||
def test_add_joins_source_relationship_with_custom_apply | ||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: Api::V10::PostResource, | ||
source_relationship: Api::V10::PostResource._relationship(:comments)) | ||
records = Api::V10::PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
sql = 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE "comments"."approved" = ' + db_true | ||
|
||
assert_equal sql, sql_for_compare(records.to_sql) | ||
|
||
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details) | ||
end | ||
|
||
def test_add_nested_scoped_joins | ||
filters = { | ||
'comments.author' => ['1'], | ||
'comments.tags' => ['1'], | ||
'author' => ['1'] | ||
} | ||
|
||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: Api::V10::PostResource, filters: filters) | ||
records = Api::V10::PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments))) | ||
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author))) | ||
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags))) | ||
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:author))) | ||
|
||
# Now test with different order for the filters | ||
filters = { | ||
'author' => ['1'], | ||
'comments.author' => ['1'], | ||
'comments.tags' => ['1'] | ||
} | ||
|
||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: Api::V10::PostResource, filters: filters) | ||
records = Api::V10::PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments))) | ||
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author))) | ||
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags))) | ||
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:author))) | ||
end | ||
|
||
def test_add_nested_joins_with_fields | ||
filters = { | ||
'comments.author.name' => ['1'], | ||
'comments.tags.id' => ['1'], | ||
'author.foo' => ['1'] | ||
} | ||
|
||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: Api::V10::PostResource, filters: filters) | ||
records = Api::V10::PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments))) | ||
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author))) | ||
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags))) | ||
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:author))) | ||
end | ||
|
||
def test_add_joins_with_sub_relationship | ||
relationships = %w(author author.comments tags) | ||
|
||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: Api::V10::PostResource, relationships: relationships, | ||
source_relationship: Api::V10::PostResource._relationship(:comments)) | ||
records = Api::V10::PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments))) | ||
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags))) | ||
assert_hash_equals({alias: 'comments_people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PersonResource._relationship(:comments))) | ||
end | ||
|
||
def test_add_joins_with_sub_relationship_and_filters | ||
filters = { | ||
'author.name' => ['1'], | ||
'author.comments.name' => ['Foo'] | ||
} | ||
|
||
relationships = %w(author author.comments tags) | ||
|
||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PostResource, | ||
filters: filters, | ||
relationships: relationships, | ||
source_relationship: PostResource._relationship(:comments)) | ||
records = PostResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.join_details_by_relationship(PostResource._relationship(:comments))) | ||
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(CommentResource._relationship(:author))) | ||
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(CommentResource._relationship(:tags))) | ||
assert_hash_equals({alias: 'comments_people', join_type: :left}, join_manager.join_details_by_relationship(PersonResource._relationship(:comments))) | ||
end | ||
|
||
def test_polymorphic_join_belongs_to_just_source | ||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PictureResource, | ||
source_relationship: PictureResource._relationship(:imageable)) | ||
|
||
records = PictureResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
# assert_equal 'SELECT "pictures".* FROM "pictures" LEFT OUTER JOIN "products" ON "products"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Product\' LEFT OUTER JOIN "documents" ON "documents"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Document\'', sql_for_compare(records.to_sql) | ||
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.source_join_details('products')) | ||
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.source_join_details('documents')) | ||
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'products')) | ||
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'documents')) | ||
end | ||
|
||
def test_polymorphic_join_belongs_to_filter | ||
filters = {'imageable' => ['Foo']} | ||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PictureResource, filters: filters) | ||
|
||
records = PictureResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
# assert_equal 'SELECT "pictures".* FROM "pictures" LEFT OUTER JOIN "products" ON "products"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Product\' LEFT OUTER JOIN "documents" ON "documents"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Document\'', sql_for_compare(records.to_sql) | ||
assert_hash_equals({alias: 'pictures', join_type: :root}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'products')) | ||
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'documents')) | ||
end | ||
|
||
def test_polymorphic_join_belongs_to_filter_on_resource | ||
filters = { | ||
'imageable#documents.name' => ['foo'] | ||
} | ||
|
||
relationships = %w(imageable file_properties) | ||
join_manager = JSONAPI::ActiveRelation::JoinManagerV10.new(resource_klass: PictureResource, | ||
filters: filters, | ||
relationships: relationships) | ||
|
||
records = PictureResource.records({}) | ||
records = join_manager.join(records, {}) | ||
|
||
assert_hash_equals({alias: 'pictures', join_type: :root}, join_manager.source_join_details) | ||
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'products')) | ||
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'documents')) | ||
assert_hash_equals({alias: 'file_properties', join_type: :left}, join_manager.join_details_by_relationship(PictureResource._relationship(:file_properties))) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
236 changes: 236 additions & 0 deletions
236
test/unit/resource/active_relation_resource_v_10_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
require File.expand_path('../../../test_helper', __FILE__) | ||
|
||
module V10 | ||
class BaseResource | ||
include JSONAPI::ResourceCommon | ||
resource_retrieval_strategy 'JSONAPI::ActiveRelationRetrievalV10' | ||
abstract | ||
end | ||
|
||
class PostResource < V10::BaseResource | ||
attribute :headline, delegate: :title | ||
has_one :author | ||
has_many :tags | ||
end | ||
|
||
class AuthorResource < V10::BaseResource | ||
model_name 'Person' | ||
attributes :name | ||
|
||
has_many :posts, inverse_relationship: :author | ||
has_many :pictures | ||
end | ||
|
||
class TagResource < V10::BaseResource | ||
attributes :name | ||
|
||
has_many :posts | ||
end | ||
|
||
class PictureResource < V10::BaseResource | ||
attribute :name | ||
has_one :author | ||
|
||
has_one :imageable, polymorphic: true | ||
end | ||
|
||
class ImageableResource < V10::BaseResource | ||
polymorphic | ||
has_one :picture | ||
end | ||
|
||
class DocumentResource < V10::BaseResource | ||
attribute :name | ||
|
||
has_many :pictures | ||
|
||
has_one :author, class_name: 'Person' | ||
end | ||
|
||
class ProductResource < V10::BaseResource | ||
attribute :name | ||
has_many :pictures | ||
has_one :designer, class_name: 'Person' | ||
|
||
has_one :file_properties, :foreign_key_on => :related | ||
|
||
def picture_id | ||
_model.picture.id | ||
end | ||
end | ||
end | ||
|
||
class ActiveRelationResourceTest < ActiveSupport::TestCase | ||
def setup | ||
# skip("Skipping: Currently test is only valid for ActiveRelationRetrievalV10") | ||
end | ||
|
||
def test_find_fragments_no_attributes | ||
filters = {} | ||
posts_identities = V10::PostResource.find_fragments(filters) | ||
|
||
assert_equal 20, posts_identities.length | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::PostResource, 1), posts_identities.keys[0] | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::PostResource, 1), posts_identities.values[0].identity | ||
assert posts_identities.values[0].is_a?(JSONAPI::ResourceFragment) | ||
end | ||
|
||
def test_find_fragments_cache_field | ||
filters = {} | ||
options = { cache: true } | ||
posts_identities = V10::PostResource.find_fragments(filters, options) | ||
|
||
assert_equal 20, posts_identities.length | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::PostResource, 1), posts_identities.keys[0] | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::PostResource, 1), posts_identities.values[0].identity | ||
assert posts_identities.values[0].is_a?(JSONAPI::ResourceFragment) | ||
assert posts_identities.values[0].cache.is_a?(ActiveSupport::TimeWithZone) | ||
end | ||
|
||
def test_find_related_has_one_fragments | ||
options = {} | ||
source_rids = [JSONAPI::ResourceIdentity.new(V10::PostResource, 1), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 2), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 20)] | ||
source_fragments = source_rids.collect {|rid| JSONAPI::ResourceFragment.new(rid) } | ||
|
||
relationship = V10::PostResource._relationship('author') | ||
related_fragments = V10::PostResource.find_included_fragments(source_fragments, relationship, options) | ||
|
||
assert_equal 2, related_fragments.length | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::AuthorResource, 1001), related_fragments.keys[0] | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::AuthorResource, 1001), related_fragments.values[0].identity | ||
assert related_fragments.values[0].is_a?(JSONAPI::ResourceFragment) | ||
assert_equal 2, related_fragments.values[0].related_from.length | ||
end | ||
|
||
def test_find_related_has_one_fragments_cache_field | ||
options = { cache: true } | ||
source_rids = [JSONAPI::ResourceIdentity.new(V10::PostResource, 1), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 2), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 20)] | ||
source_fragments = source_rids.collect {|rid| JSONAPI::ResourceFragment.new(rid) } | ||
|
||
relationship = V10::PostResource._relationship('author') | ||
related_fragments = V10::PostResource.find_included_fragments(source_fragments, relationship, options) | ||
|
||
assert_equal 2, related_fragments.length | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::AuthorResource, 1001), related_fragments.keys[0] | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::AuthorResource, 1001), related_fragments.values[0].identity | ||
assert related_fragments.values[0].is_a?(JSONAPI::ResourceFragment) | ||
assert_equal 2, related_fragments.values[0].related_from.length | ||
assert related_fragments.values[0].cache.is_a?(ActiveSupport::TimeWithZone) | ||
end | ||
|
||
def test_find_related_has_many_fragments | ||
options = {} | ||
source_rids = [JSONAPI::ResourceIdentity.new(V10::PostResource, 1), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 2), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 12), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 14)] | ||
source_fragments = source_rids.collect {|rid| JSONAPI::ResourceFragment.new(rid) } | ||
|
||
relationship = V10::PostResource._relationship('tags') | ||
related_fragments = V10::PostResource.send(:find_included_fragments, source_fragments, relationship, options) | ||
|
||
assert_equal 8, related_fragments.length | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::TagResource, 501), related_fragments.keys[0] | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::TagResource, 501), related_fragments.values[0].identity | ||
assert related_fragments.values[0].is_a?(JSONAPI::ResourceFragment) | ||
assert_equal 1, related_fragments.values[0].related_from.length | ||
assert_equal 2, related_fragments[JSONAPI::ResourceIdentity.new(V10::TagResource, 502)].related_from.length | ||
end | ||
|
||
def test_find_related_has_many_fragments_pagination | ||
params = ActionController::Parameters.new(number: 2, size: 4) | ||
options = { paginator: PagedPaginator.new(params) } | ||
source_rids = [JSONAPI::ResourceIdentity.new(V10::PostResource, 15)] | ||
source_fragments = source_rids.collect {|rid| JSONAPI::ResourceFragment.new(rid) } | ||
|
||
relationship = V10::PostResource._relationship('tags') | ||
related_fragments = V10::PostResource.find_included_fragments(source_fragments, relationship, options) | ||
|
||
assert_equal 1, related_fragments.length | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::TagResource, 516), related_fragments.keys[0] | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::TagResource, 516), related_fragments.values[0].identity | ||
assert related_fragments.values[0].is_a?(JSONAPI::ResourceFragment) | ||
assert_equal 1, related_fragments.values[0].related_from.length | ||
end | ||
|
||
def test_find_related_has_many_fragments_cache_field | ||
options = { cache: true } | ||
source_rids = [JSONAPI::ResourceIdentity.new(V10::PostResource, 1), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 2), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 12), | ||
JSONAPI::ResourceIdentity.new(V10::PostResource, 14)] | ||
source_fragments = source_rids.collect {|rid| JSONAPI::ResourceFragment.new(rid) } | ||
|
||
relationship = V10::PostResource._relationship('tags') | ||
related_fragments = V10::PostResource.find_included_fragments(source_fragments, relationship, options) | ||
|
||
assert_equal 8, related_fragments.length | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::TagResource, 501), related_fragments.keys[0] | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::TagResource, 501), related_fragments.values[0].identity | ||
assert related_fragments.values[0].is_a?(JSONAPI::ResourceFragment) | ||
assert_equal 1, related_fragments.values[0].related_from.length | ||
assert_equal 2, related_fragments[JSONAPI::ResourceIdentity.new(V10::TagResource, 502)].related_from.length | ||
assert related_fragments.values[0].cache.is_a?(ActiveSupport::TimeWithZone) | ||
end | ||
|
||
def test_find_related_polymorphic_fragments | ||
options = {} | ||
source_rids = [JSONAPI::ResourceIdentity.new(V10::PictureResource, 1), | ||
JSONAPI::ResourceIdentity.new(V10::PictureResource, 2), | ||
JSONAPI::ResourceIdentity.new(V10::PictureResource, 3)] | ||
source_fragments = source_rids.collect {|rid| JSONAPI::ResourceFragment.new(rid) } | ||
|
||
relationship = V10::PictureResource._relationship('imageable') | ||
related_fragments = V10::PictureResource.find_included_fragments(source_fragments, relationship, options) | ||
|
||
assert_equal 2, related_fragments.length | ||
assert related_fragments.keys.include?(JSONAPI::ResourceIdentity.new(V10::ProductResource, 1)) | ||
assert related_fragments.keys.include?(JSONAPI::ResourceIdentity.new(V10::DocumentResource, 1)) | ||
|
||
assert related_fragments.values[0].is_a?(JSONAPI::ResourceFragment) | ||
assert_equal 1, related_fragments.values[0].related_from.length | ||
assert_equal JSONAPI::ResourceIdentity.new(V10::ProductResource, 1), related_fragments.values[0].identity | ||
end | ||
|
||
def test_find_related_polymorphic_fragments_cache_field | ||
options = { cache: true } | ||
source_rids = [JSONAPI::ResourceIdentity.new(V10::PictureResource, 1), | ||
JSONAPI::ResourceIdentity.new(V10::PictureResource, 2), | ||
JSONAPI::ResourceIdentity.new(V10::PictureResource, 3)] | ||
source_fragments = source_rids.collect {|rid| JSONAPI::ResourceFragment.new(rid) } | ||
|
||
relationship = V10::PictureResource._relationship('imageable') | ||
related_fragments = V10::PictureResource.find_included_fragments(source_fragments, relationship, options) | ||
|
||
assert_equal 2, related_fragments.length | ||
assert related_fragments.keys.include?(JSONAPI::ResourceIdentity.new(V10::ProductResource, 1)) | ||
assert related_fragments.keys.include?(JSONAPI::ResourceIdentity.new(V10::DocumentResource, 1)) | ||
|
||
assert related_fragments.values[0].is_a?(JSONAPI::ResourceFragment) | ||
assert_equal 1, related_fragments.values[0].related_from.length | ||
assert related_fragments.values[0].cache.is_a?(ActiveSupport::TimeWithZone) | ||
assert related_fragments.values[1].cache.is_a?(ActiveSupport::TimeWithZone) | ||
end | ||
|
||
def test_find_related_polymorphic_fragments_not_cached | ||
options = { cache: false } | ||
source_rids = [JSONAPI::ResourceIdentity.new(V10::PictureResource, 1), | ||
JSONAPI::ResourceIdentity.new(V10::PictureResource, 2), | ||
JSONAPI::ResourceIdentity.new(V10::PictureResource, 3)] | ||
source_fragments = source_rids.collect {|rid| JSONAPI::ResourceFragment.new(rid) } | ||
|
||
relationship = V10::PictureResource._relationship('imageable') | ||
related_fragments = V10::PictureResource.find_included_fragments(source_fragments, relationship, options) | ||
|
||
assert_equal 2, related_fragments.length | ||
assert related_fragments.keys.include?(JSONAPI::ResourceIdentity.new(V10::ProductResource, 1)) | ||
assert related_fragments.keys.include?(JSONAPI::ResourceIdentity.new(V10::DocumentResource, 1)) | ||
|
||
assert related_fragments.values[0].is_a?(JSONAPI::ResourceFragment) | ||
assert_equal 1, related_fragments.values[0].related_from.length | ||
end | ||
end |
Oops, something went wrong.