sanger-jsonapi-resources 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +35 -12
  4. data/lib/bug_report_templates/rails_5_latest.rb +125 -0
  5. data/lib/bug_report_templates/rails_5_master.rb +140 -0
  6. data/lib/jsonapi/active_relation/adapters/join_left_active_record_adapter.rb +26 -0
  7. data/lib/jsonapi/active_relation/join_manager.rb +297 -0
  8. data/lib/jsonapi/active_relation_resource.rb +898 -0
  9. data/lib/jsonapi/acts_as_resource_controller.rb +130 -113
  10. data/lib/jsonapi/basic_resource.rb +1164 -0
  11. data/lib/jsonapi/cached_response_fragment.rb +129 -0
  12. data/lib/jsonapi/callbacks.rb +2 -0
  13. data/lib/jsonapi/compatibility_helper.rb +29 -0
  14. data/lib/jsonapi/compiled_json.rb +13 -1
  15. data/lib/jsonapi/configuration.rb +88 -21
  16. data/lib/jsonapi/error.rb +29 -0
  17. data/lib/jsonapi/error_codes.rb +4 -0
  18. data/lib/jsonapi/exceptions.rb +82 -50
  19. data/lib/jsonapi/formatter.rb +5 -3
  20. data/lib/jsonapi/include_directives.rb +22 -67
  21. data/lib/jsonapi/link_builder.rb +76 -80
  22. data/lib/jsonapi/mime_types.rb +6 -10
  23. data/lib/jsonapi/naive_cache.rb +2 -0
  24. data/lib/jsonapi/operation.rb +18 -5
  25. data/lib/jsonapi/operation_result.rb +76 -16
  26. data/lib/jsonapi/paginator.rb +2 -0
  27. data/lib/jsonapi/path.rb +45 -0
  28. data/lib/jsonapi/path_segment.rb +78 -0
  29. data/lib/jsonapi/processor.rb +193 -115
  30. data/lib/jsonapi/relationship.rb +145 -14
  31. data/lib/jsonapi/request.rb +734 -0
  32. data/lib/jsonapi/resource.rb +3 -1251
  33. data/lib/jsonapi/resource_controller.rb +2 -0
  34. data/lib/jsonapi/resource_controller_metal.rb +7 -1
  35. data/lib/jsonapi/resource_fragment.rb +56 -0
  36. data/lib/jsonapi/resource_identity.rb +44 -0
  37. data/lib/jsonapi/resource_serializer.rb +158 -284
  38. data/lib/jsonapi/resource_set.rb +196 -0
  39. data/lib/jsonapi/resource_tree.rb +236 -0
  40. data/lib/jsonapi/resources/railtie.rb +9 -0
  41. data/lib/jsonapi/resources/version.rb +1 -1
  42. data/lib/jsonapi/response_document.rb +107 -83
  43. data/lib/jsonapi/routing_ext.rb +50 -26
  44. data/lib/jsonapi-resources.rb +23 -5
  45. data/lib/tasks/check_upgrade.rake +52 -0
  46. metadata +43 -31
  47. data/lib/jsonapi/cached_resource_fragment.rb +0 -127
  48. data/lib/jsonapi/operation_dispatcher.rb +0 -88
  49. data/lib/jsonapi/operation_results.rb +0 -35
  50. data/lib/jsonapi/relationship_builder.rb +0 -167
  51. data/lib/jsonapi/request_parser.rb +0 -678
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JSONAPI
2
4
  class ResourceController < ActionController::Base
3
5
  include JSONAPI::ActsAsResourceController
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JSONAPI
2
4
  class ResourceControllerMetal < ActionController::Metal
3
5
  MODULES = [
@@ -5,10 +7,14 @@ module JSONAPI
5
7
  ActionController::Rendering,
6
8
  ActionController::Renderers::All,
7
9
  ActionController::StrongParameters,
10
+ Gem::Requirement.new('< 6.1').satisfied_by?(ActionPack.gem_version) ? ActionController::ForceSSL : nil,
8
11
  ActionController::Instrumentation,
9
12
  JSONAPI::ActsAsResourceController
10
- ].freeze
13
+ ].compact.freeze
11
14
 
15
+ # Note, the url_helpers are not loaded. This will prevent links from being generated for resources, and warnings
16
+ # will be emitted. Link support can be added by including `Rails.application.routes.url_helpers`, and links
17
+ # can be disabled, and warning suppressed, for a resource with `exclude_links :default`
12
18
  MODULES.each do |mod|
13
19
  include mod
14
20
  end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONAPI
4
+
5
+ # A ResourceFragment holds a ResourceIdentity and associated partial resource data.
6
+ #
7
+ # The following partial resource data may be stored
8
+ # cache - the value of the cache field for the resource instance
9
+ # related - a hash of arrays of related resource identities, grouped by relationship name
10
+ # related_from - a set of related resource identities that loaded the fragment
11
+ # resource - a resource instance
12
+ #
13
+ # Todo: optionally use these for faster responses by bypassing model instantiation)
14
+ # attributes - resource attributes
15
+
16
+ class ResourceFragment
17
+ attr_reader :identity, :attributes, :related_from, :related, :resource
18
+
19
+ attr_accessor :primary, :cache
20
+
21
+ alias :cache_field :cache #ToDo: Rename one or the other
22
+
23
+ def initialize(identity, resource: nil, cache: nil, primary: false)
24
+ @identity = identity
25
+ @cache = cache
26
+ @resource = resource
27
+ @primary = primary
28
+
29
+ @attributes = {}
30
+ @related = {}
31
+ @related_from = Set.new
32
+ end
33
+
34
+ def initialize_related(relationship_name)
35
+ @related[relationship_name.to_sym] ||= Set.new
36
+ end
37
+
38
+ def add_related_identity(relationship_name, identity)
39
+ initialize_related(relationship_name)
40
+ @related[relationship_name.to_sym] << identity if identity
41
+ end
42
+
43
+ def merge_related_identities(relationship_name, identities)
44
+ initialize_related(relationship_name)
45
+ @related[relationship_name.to_sym].merge(identities) if identities
46
+ end
47
+
48
+ def add_related_from(identity)
49
+ @related_from << identity
50
+ end
51
+
52
+ def add_attribute(name, value)
53
+ @attributes[name] = value
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONAPI
4
+
5
+ # ResourceIdentity describes a unique identity of a resource in the system.
6
+ # This consists of a Resource class and an identifier that is unique within
7
+ # that Resource class. ResourceIdentities are intended to be used as hash
8
+ # keys to provide ordered mixing of resource types in result sets.
9
+ #
10
+ #
11
+ # == Creating a ResourceIdentity
12
+ #
13
+ # rid = ResourceIdentity.new(PostResource, 12)
14
+ #
15
+ class ResourceIdentity
16
+ attr_reader :resource_klass, :id
17
+
18
+ def initialize(resource_klass, id)
19
+ @resource_klass = resource_klass
20
+ @id = id
21
+ end
22
+
23
+ def ==(other)
24
+ # :nocov:
25
+ eql?(other)
26
+ # :nocov:
27
+ end
28
+
29
+ def eql?(other)
30
+ other.is_a?(ResourceIdentity) && other.resource_klass == @resource_klass && other.id == @id
31
+ end
32
+
33
+ def hash
34
+ [@resource_klass, @id].hash
35
+ end
36
+
37
+ # Creates a string representation of the identifier.
38
+ def to_s
39
+ # :nocov:
40
+ "#{resource_klass}:#{id}"
41
+ # :nocov:
42
+ end
43
+ end
44
+ end