Skip to content

Commit 92c5fe5

Browse files
committed
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
1 parent 5868a37 commit 92c5fe5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+5685
-3056
lines changed

.github/workflows/ruby.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,21 @@ jobs:
4747
- '7.0'
4848
- '6.1'
4949
- '6.0'
50-
- '5.2'
51-
- '5.1'
5250
database_url:
5351
- sqlite3:test_db
5452
- postgresql://postgres:password@localhost:5432/test
5553
- mysql2://root:root@127.0.0.1:3306/test
5654
exclude:
5755
- ruby: '3.2'
5856
rails: '6.0'
59-
- ruby: '3.2'
60-
rails: '5.2'
61-
- ruby: '3.2'
62-
rails: '5.1'
6357
- ruby: '3.1'
6458
rails: '6.0'
65-
- ruby: '3.1'
66-
rails: '5.2'
6759
- ruby: '3.1'
6860
rails: '5.1'
6961
- ruby: '3.0'
7062
rails: '6.0'
71-
- ruby: '3.0'
72-
rails: '5.2'
73-
- ruby: '3.0'
74-
rails: '5.1'
7563
- ruby: '2.6'
7664
rails: '7.0'
77-
- database_url: postgresql://postgres:password@localhost:5432/test
78-
rails: '5.1'
7965
env:
8066
RAILS_VERSION: ${{ matrix.rails }}
8167
DATABASE_URL: ${{ matrix.database_url }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ test_db
2323
test_db-journal
2424
.idea
2525
*.iml
26+
*.override.yml

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2014-2021 Cerebris Corporation
1+
Copyright (c) 2014-2023 Cerebris Corporation
22

33
MIT License
44

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,4 @@ and **paste the content into the issue description or attach as a file**:
110110

111111
## License
112112

113-
Copyright 2014-2021 Cerebris Corporation. MIT License (see LICENSE for details).
113+
Copyright 2014-2023 Cerebris Corporation. MIT License (see LICENSE for details).

jsonapi-resources.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ Gem::Specification.new do |spec|
2727
spec.add_development_dependency 'pry'
2828
spec.add_development_dependency 'concurrent-ruby-ext'
2929
spec.add_development_dependency 'database_cleaner'
30+
spec.add_development_dependency 'hashie'
3031
spec.add_dependency 'activerecord', '>= 5.1'
3132
spec.add_dependency 'railties', '>= 5.1'
3233
spec.add_dependency 'concurrent-ruby'
34+
spec.add_dependency 'sorted_set'
3335
end

lib/jsonapi-resources.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
require 'jsonapi/resources/railtie'
44
require 'jsonapi/naive_cache'
55
require 'jsonapi/compiled_json'
6-
require 'jsonapi/basic_resource'
7-
require 'jsonapi/active_relation_resource'
6+
require 'jsonapi/active_relation_retrieval'
7+
require 'jsonapi/active_relation_retrieval_v09'
8+
require 'jsonapi/active_relation_retrieval_v10'
9+
require 'jsonapi/resource_common'
810
require 'jsonapi/resource'
11+
require 'jsonapi/simple_resource'
912
require 'jsonapi/cached_response_fragment'
1013
require 'jsonapi/response_document'
1114
require 'jsonapi/acts_as_resource_controller'
@@ -37,6 +40,7 @@
3740
require 'jsonapi/link_builder'
3841
require 'jsonapi/active_relation/adapters/join_left_active_record_adapter'
3942
require 'jsonapi/active_relation/join_manager'
43+
require 'jsonapi/active_relation/join_manager_v10'
4044
require 'jsonapi/resource_identity'
4145
require 'jsonapi/resource_fragment'
4246
require 'jsonapi/resource_tree'

lib/jsonapi/active_relation/join_manager.rb

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@ class JoinManager
77
attr_reader :resource_klass,
88
:source_relationship,
99
:resource_join_tree,
10-
:join_details
10+
:join_details,
11+
:through_source
1112

1213
def initialize(resource_klass:,
1314
source_relationship: nil,
15+
source_resource_klass: nil,
16+
through_source: false,
1417
relationships: nil,
1518
filters: nil,
1619
sort_criteria: nil)
1720

1821
@resource_klass = resource_klass
22+
@source_resource_klass = source_resource_klass
1923
@join_details = nil
2024
@collected_aliases = Set.new
25+
@through_source = through_source
2126

2227
@resource_join_tree = {
2328
root: {
@@ -45,7 +50,7 @@ def join(records, options)
4550
# this method gets the join details whether they are on a relationship or are just pseudo details for the base
4651
# resource. Specify the resource type for polymorphic relationships
4752
#
48-
def source_join_details(type=nil)
53+
def source_join_details(type = nil)
4954
if source_relationship
5055
related_resource_klass = type ? resource_klass.resource_klass_for(type) : source_relationship.resource_klass
5156
segment = PathSegment::Relationship.new(relationship: source_relationship, resource_klass: related_resource_klass)
@@ -108,14 +113,20 @@ def self.get_join_arel_node(records, relationship, join_type, options = {})
108113
end
109114

110115
def self.alias_from_arel_node(node)
111-
case node.left
116+
# case node.left
117+
case node&.left
112118
when Arel::Table
113119
node.left.name
114120
when Arel::Nodes::TableAlias
115121
node.left.right
116122
when Arel::Nodes::StringJoin
117123
# :nocov:
118-
warn "alias_from_arel_node: Unsupported join type - use custom filtering and sorting"
124+
warn "alias_from_arel_node: Unsupported join type `Arel::Nodes::StringJoin` - use custom filtering and sorting"
125+
nil
126+
# :nocov:
127+
else
128+
# :nocov:
129+
warn "alias_from_arel_node: Unsupported join type `#{node&.left.to_s}`"
119130
nil
120131
# :nocov:
121132
end
@@ -181,7 +192,8 @@ def perform_joins(records, options)
181192
options: options)
182193
}
183194

184-
details = {alias: self.class.alias_from_arel_node(join_node), join_type: join_type}
195+
join_alias = self.class.alias_from_arel_node(join_node)
196+
details = {alias: join_alias, join_type: join_type}
185197

186198
if relationship == source_relationship
187199
if relationship.polymorphic? && relationship.belongs_to?
@@ -193,15 +205,19 @@ def perform_joins(records, options)
193205

194206
# We're adding the source alias with two keys. We only want the check for duplicate aliases once.
195207
# See the note in `add_join_details`.
196-
check_for_duplicate_alias = !(relationship == source_relationship)
197-
add_join_details(PathSegment::Relationship.new(relationship: relationship, resource_klass: related_resource_klass), details, check_for_duplicate_alias)
208+
check_for_duplicate_alias = relationship != source_relationship
209+
path_segment = PathSegment::Relationship.new(relationship: relationship,
210+
resource_klass: related_resource_klass)
211+
212+
add_join_details(path_segment, details, check_for_duplicate_alias)
198213
end
199214
end
200215
records
201216
end
202217

203218
def add_join(path, default_type = :inner, default_polymorphic_join_type = :left)
204-
if source_relationship
219+
# puts "add_join #{path} default_type=#{default_type} default_polymorphic_join_type=#{default_polymorphic_join_type}"
220+
if source_relationship && through_source
205221
if source_relationship.polymorphic?
206222
# Polymorphic paths will come it with the resource_type as the first segment (for example `#documents.comments`)
207223
# We just need to prepend the relationship portion the
@@ -213,9 +229,9 @@ def add_join(path, default_type = :inner, default_polymorphic_join_type = :left)
213229
sourced_path = path
214230
end
215231

216-
join_manager, _field = parse_path_to_tree(sourced_path, resource_klass, default_type, default_polymorphic_join_type)
232+
join_tree, _field = parse_path_to_tree(sourced_path, resource_klass, default_type, default_polymorphic_join_type)
217233

218-
@resource_join_tree[:root].deep_merge!(join_manager) { |key, val, other_val|
234+
@resource_join_tree[:root].deep_merge!(join_tree) { |key, val, other_val|
219235
if key == :join_type
220236
if val == other_val
221237
val

0 commit comments

Comments
 (0)