From 2caf009e9faf88ec5253ec97e9d3045b9d1faf2c Mon Sep 17 00:00:00 2001 From: Zeeshan Ishtiaq Date: Fri, 16 Jun 2023 17:10:03 +0100 Subject: [PATCH 1/3] ruby 3 upgrade [WIP] --- .github/workflows/tests.yml | 65 ++++++++++---- lib/jsonapi/error.rb | 4 +- lib/jsonapi/exceptions.rb | 90 +++++++++---------- lib/jsonapi/link_builder.rb | 4 +- lib/jsonapi/operation_result.rb | 22 ++--- lib/jsonapi/paginator.rb | 6 +- lib/jsonapi/processor.rb | 8 +- lib/jsonapi/request_parser.rb | 2 +- lib/jsonapi/resource_serializer.rb | 12 +-- lib/jsonapi/response_document.rb | 2 +- test/test_helper.rb | 8 +- .../jsonapi_request/jsonapi_request_test.rb | 14 +-- test/unit/serializer/link_builder_test.rb | 64 ++++++------- .../unit/serializer/response_document_test.rb | 2 +- 14 files changed, 169 insertions(+), 134 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b8e828a64..93945e3be 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,31 +9,66 @@ on: jobs: tests: runs-on: ubuntu-latest + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: password + POSTGRES_DB: test + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 strategy: fail-fast: false matrix: ruby: - - 2.6.6 - - 2.7.2 + - 2.6 + - 2.7 + - '3.0' + - 3.1 + - 3.2 rails: - - 6.1.1 - - 6.0.3.4 - - 5.2.4.4 + - 7.0.4 + - 6.1.7 + - 6.0.6 + - 5.2.8.1 - 5.1.7 - - 5.0.7.2 - - 4.2.11 + database_url: + - postgresql://postgres:password@localhost:5432/test + - sqlite3:test_db exclude: - - ruby: 2.7.2 - rails: 5.0.7.2 - - ruby: 2.7.2 - rails: 4.2.11 - - ruby: 2.6.6 - rails: 4.2.11 + - ruby: 3.2 + rails: 6.0.6 + - ruby: 3.2 + rails: 5.2.8.1 + - ruby: 3.2 + rails: 5.1.7 + - ruby: 3.1 + rails: 6.0.6 + - ruby: 3.1 + rails: 5.2.8.1 + - ruby: 3.1 + rails: 5.1.7 + - ruby: '3.0' + rails: 6.0.6 + - ruby: '3.0' + rails: 5.2.8.1 + - ruby: '3.0' + rails: 5.1.7 + - ruby: 2.6 + rails: 7.0.4 + - database_url: postgresql://postgres:password@localhost:5432/test + rails: 5.1.7 env: RAILS_VERSION: ${{ matrix.rails }} - name: Ruby ${{ matrix.ruby }} Rails ${{ matrix.rails }} + DATABASE_URL: ${{ matrix.database_url }} + name: Ruby ${{ matrix.ruby }} Rails ${{ matrix.rails }} DB ${{ matrix.database_url }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: diff --git a/lib/jsonapi/error.rb b/lib/jsonapi/error.rb index 41545c13e..db00e7146 100644 --- a/lib/jsonapi/error.rb +++ b/lib/jsonapi/error.rb @@ -2,7 +2,7 @@ module JSONAPI class Error attr_accessor :title, :detail, :id, :href, :code, :source, :links, :status, :meta - def initialize(options = {}) + def initialize(**options) @title = options[:title] @detail = options[:detail] @id = options[:id] @@ -28,7 +28,7 @@ def to_hash class Warning attr_accessor :title, :detail, :code - def initialize(options = {}) + def initialize(**options) @title = options[:title] @detail = options[:detail] @code = if JSONAPI.configuration.use_text_errors diff --git a/lib/jsonapi/exceptions.rb b/lib/jsonapi/exceptions.rb index 6c44de76b..7a010eca4 100644 --- a/lib/jsonapi/exceptions.rb +++ b/lib/jsonapi/exceptions.rb @@ -3,12 +3,12 @@ module Exceptions class Error < RuntimeError attr :error_object_overrides - def initialize(error_object_overrides = {}) + def initialize(**error_object_overrides) @error_object_overrides = error_object_overrides end - def create_error_object(error_defaults) - JSONAPI::Error.new(error_defaults.merge(error_object_overrides)) + def create_error_object(**error_defaults) + JSONAPI::Error.new(**error_defaults.merge(error_object_overrides)) end def errors @@ -21,9 +21,9 @@ def errors class InternalServerError < Error attr_accessor :exception - def initialize(exception, error_object_overrides = {}) + def initialize(exception, **error_object_overrides) @exception = exception - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -46,9 +46,9 @@ def errors class InvalidResource < Error attr_accessor :resource - def initialize(resource, error_object_overrides = {}) + def initialize(resource, **error_object_overrides) @resource = resource - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -64,9 +64,9 @@ def errors class RecordNotFound < Error attr_accessor :id - def initialize(id, error_object_overrides = {}) + def initialize(id, **error_object_overrides) @id = id - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -82,9 +82,9 @@ def errors class UnsupportedMediaTypeError < Error attr_accessor :media_type - def initialize(media_type, error_object_overrides = {}) + def initialize(media_type, **error_object_overrides) @media_type = media_type - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -102,9 +102,9 @@ def errors class NotAcceptableError < Error attr_accessor :media_type - def initialize(media_type, error_object_overrides = {}) + def initialize(media_type, **error_object_overrides) @media_type = media_type - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -123,9 +123,9 @@ def errors class HasManyRelationExists < Error attr_accessor :id - def initialize(id, error_object_overrides = {}) + def initialize(id, **error_object_overrides) @id = id - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -179,9 +179,9 @@ def errors class InvalidFiltersSyntax < Error attr_accessor :filters - def initialize(filters, error_object_overrides = {}) + def initialize(filters, **error_object_overrides) @filters = filters - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -198,9 +198,9 @@ def errors class FilterNotAllowed < Error attr_accessor :filter - def initialize(filter, error_object_overrides = {}) + def initialize(filter, **error_object_overrides) @filter = filter - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -216,10 +216,10 @@ def errors class InvalidFilterValue < Error attr_accessor :filter, :value - def initialize(filter, value, error_object_overrides = {}) + def initialize(filter, value, **error_object_overrides) @filter = filter @value = value - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -236,10 +236,10 @@ def errors class InvalidFieldValue < Error attr_accessor :field, :value - def initialize(field, value, error_object_overrides = {}) + def initialize(field, value, **error_object_overrides) @field = field @value = value - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -289,9 +289,9 @@ def errors class TypeMismatch < Error attr_accessor :type - def initialize(type, error_object_overrides = {}) + def initialize(type, **error_object_overrides) @type = type - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -307,10 +307,10 @@ def errors class InvalidField < Error attr_accessor :field, :type - def initialize(type, field, error_object_overrides = {}) + def initialize(type, field, **error_object_overrides) @field = field @type = type - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -327,10 +327,10 @@ def errors class InvalidInclude < Error attr_accessor :relationship, :resource - def initialize(resource, relationship, error_object_overrides = {}) + def initialize(resource, relationship, **error_object_overrides) @resource = resource @relationship = relationship - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -347,10 +347,10 @@ def errors class InvalidSortCriteria < Error attr_accessor :sort_criteria, :resource - def initialize(resource, sort_criteria, error_object_overrides = {}) + def initialize(resource, sort_criteria, **error_object_overrides) @resource = resource @sort_criteria = sort_criteria - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -367,9 +367,9 @@ def errors class ParameterNotAllowed < Error attr_accessor :param - def initialize(param, error_object_overrides = {}) + def initialize(param, **error_object_overrides) @param = param - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -385,9 +385,9 @@ def errors class ParameterMissing < Error attr_accessor :param - def initialize(param, error_object_overrides = {}) + def initialize(param, **error_object_overrides) @param = param - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -403,9 +403,9 @@ def errors class KeyNotIncludedInURL < Error attr_accessor :key - def initialize(key, error_object_overrides = {}) + def initialize(key, **error_object_overrides) @key = key - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -433,9 +433,9 @@ def errors class RecordLocked < Error attr_accessor :message - def initialize(message, error_object_overrides = {}) + def initialize(message, **error_object_overrides) @message = message - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -450,13 +450,13 @@ def errors class ValidationErrors < Error attr_reader :error_messages, :error_metadata, :resource_relationships, :resource_class - def initialize(resource, error_object_overrides = {}) + def initialize(resource, **error_object_overrides) @error_messages = resource.model_error_messages @error_metadata = resource.validation_error_metadata @resource_class = resource.class @resource_relationships = resource.class._relationships.keys @key_formatter = JSONAPI.configuration.key_formatter - super(error_object_overrides) + super(**error_object_overrides) end def format_key(key) @@ -529,9 +529,9 @@ def errors class PageParametersNotAllowed < Error attr_accessor :params - def initialize(params, error_object_overrides = {}) + def initialize(params, **error_object_overrides) @params = params - super(error_object_overrides) + super(**error_object_overrides) end def errors @@ -550,10 +550,10 @@ def errors class InvalidPageValue < Error attr_accessor :page, :value - def initialize(page, value, error_object_overrides = {}) + def initialize(page, value, **error_object_overrides) @page = page @value = value - super(error_object_overrides) + super(**error_object_overrides) end def errors diff --git a/lib/jsonapi/link_builder.rb b/lib/jsonapi/link_builder.rb index 8f4c0cca1..a5b155c5a 100644 --- a/lib/jsonapi/link_builder.rb +++ b/lib/jsonapi/link_builder.rb @@ -9,7 +9,7 @@ class LinkBuilder @@url_helper_methods = {} - def initialize(config = {}) + def initialize(**config) @base_url = config[:base_url] @primary_resource_klass = config[:primary_resource_klass] @route_formatter = config[:route_formatter] @@ -45,7 +45,7 @@ def query_link(query_params) "#{ url }?#{ query_params.to_query }" end - def relationships_related_link(source, relationship, query_params = {}) + def relationships_related_link(source, relationship, **query_params) if relationship._routed url = "#{ self_link(source) }/#{ route_for_relationship(relationship) }" url = "#{ url }?#{ query_params.to_query }" if query_params.present? diff --git a/lib/jsonapi/operation_result.rb b/lib/jsonapi/operation_result.rb index ce3806255..cd05e3a7b 100644 --- a/lib/jsonapi/operation_result.rb +++ b/lib/jsonapi/operation_result.rb @@ -5,7 +5,7 @@ class OperationResult attr_accessor :links attr_accessor :options - def initialize(code, options = {}) + def initialize(code, **options) @code = code @options = options @meta = options.fetch(:meta, {}) @@ -16,50 +16,50 @@ def initialize(code, options = {}) class ErrorsOperationResult < OperationResult attr_accessor :errors - def initialize(code, errors, options = {}) + def initialize(code, errors, **options) @errors = errors - super(code, options) + super(code, **options) end end class ResourceOperationResult < OperationResult attr_accessor :resource - def initialize(code, resource, options = {}) + def initialize(code, resource, **options) @resource = resource - super(code, options) + super(code, **options) end end class ResourcesOperationResult < OperationResult attr_accessor :resources, :pagination_params, :record_count, :page_count - def initialize(code, resources, options = {}) + def initialize(code, resources, **options) @resources = resources @pagination_params = options.fetch(:pagination_params, {}) @record_count = options[:record_count] @page_count = options[:page_count] - super(code, options) + super(code, **options) end end class RelatedResourcesOperationResult < ResourcesOperationResult attr_accessor :source_resource, :_type - def initialize(code, source_resource, type, resources, options = {}) + def initialize(code, source_resource, type, resources, **options) @source_resource = source_resource @_type = type - super(code, resources, options) + super(code, resources, **options) end end class RelationshipOperationResult < OperationResult attr_accessor :parent_resource, :relationship - def initialize(code, parent_resource, relationship, options = {}) + def initialize(code, parent_resource, relationship, **options) @parent_resource = parent_resource @relationship = relationship - super(code, options) + super(code, **options) end end end diff --git a/lib/jsonapi/paginator.rb b/lib/jsonapi/paginator.rb index 53f8fbbe4..4b8a6a197 100644 --- a/lib/jsonapi/paginator.rb +++ b/lib/jsonapi/paginator.rb @@ -7,7 +7,7 @@ def apply(_relation, _order_options) # relation end - def links_page_params(_options = {}) + def links_page_params(**_options) # :nocov: {} # :nocov: @@ -44,7 +44,7 @@ def apply(relation, _order_options) relation.offset(@offset).limit(@limit) end - def links_page_params(options = {}) + def links_page_params(**options) record_count = options[:record_count] links_page_params = {} @@ -140,7 +140,7 @@ def apply(relation, _order_options) relation.offset(offset).limit(@size) end - def links_page_params(options = {}) + def links_page_params(**options) record_count = options[:record_count] page_count = calculate_page_count(record_count) diff --git a/lib/jsonapi/processor.rb b/lib/jsonapi/processor.rb index 6f99c8054..75ea766aa 100644 --- a/lib/jsonapi/processor.rb +++ b/lib/jsonapi/processor.rb @@ -100,10 +100,10 @@ def find end if JSONAPI.configuration.top_level_links_include_pagination && paginator - page_options[:pagination_params] = paginator.links_page_params(page_options.merge(fetched_resources: resource_records)) + page_options[:pagination_params] = paginator.links_page_params(**page_options.merge(fetched_resources: resource_records)) end - return JSONAPI::ResourcesOperationResult.new(:ok, resource_records, page_options) + return JSONAPI::ResourcesOperationResult.new(:ok, resource_records, **page_options) end def show @@ -210,7 +210,7 @@ def show_related_resources pagination_params = if paginator && JSONAPI.configuration.top_level_links_include_pagination page_options = {} page_options[:record_count] = record_count if paginator.class.requires_record_count - paginator.links_page_params(page_options.merge(fetched_resources: related_resources)) + paginator.links_page_params(**page_options.merge(fetched_resources: related_resources)) else {} end @@ -224,7 +224,7 @@ def show_related_resources source_resource, relationship_type, related_resources, - opts) + **opts) end def create_resource diff --git a/lib/jsonapi/request_parser.rb b/lib/jsonapi/request_parser.rb index 63f5672af..4f9ad4aba 100644 --- a/lib/jsonapi/request_parser.rb +++ b/lib/jsonapi/request_parser.rb @@ -7,7 +7,7 @@ class RequestParser :resource_klass, :context, :paginator, :source_klass, :source_id, :include_directives, :params, :warnings, :server_error_callbacks - def initialize(params = nil, options = {}) + def initialize(params = nil, **options) @params = params @context = options[:context] @key_formatter = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter) diff --git a/lib/jsonapi/resource_serializer.rb b/lib/jsonapi/resource_serializer.rb index 0ef56d31f..0f6d4d0dc 100644 --- a/lib/jsonapi/resource_serializer.rb +++ b/lib/jsonapi/resource_serializer.rb @@ -26,7 +26,7 @@ def initialize(primary_resource_klass, options = {}) @include_directives ||= JSONAPI::IncludeDirectives.new(@primary_resource_klass, @include) @key_formatter = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter) @id_formatter = ValueFormatter.value_formatter_for(:id) - @link_builder = generate_link_builder(primary_resource_klass, options) + @link_builder = generate_link_builder(primary_resource_klass, **options) @always_include_to_one_linkage_data = options.fetch(:always_include_to_one_linkage_data, JSONAPI.configuration.always_include_to_one_linkage_data) @always_include_to_many_linkage_data = options.fetch(:always_include_to_many_linkage_data, @@ -50,7 +50,7 @@ def serialize_to_hash(source) @included_objects = {} - process_source_objects(source, @include_directives.include_directives) + process_source_objects(source, **@include_directives.include_directives) primary_objects = [] @@ -186,9 +186,9 @@ def object_hash(source, include_directives = {}) # requested includes. Fields are controlled fields option for each resource type, such # as fields: { people: [:id, :email, :comments], posts: [:id, :title, :author], comments: [:id, :body, :post]} # The fields options controls both fields and included links references. - def process_source_objects(source, include_directives) + def process_source_objects(source, **include_directives) if source.respond_to?(:to_ary) - source.each { |resource| process_source_objects(resource, include_directives) } + source.each { |resource| process_source_objects(resource, **include_directives) } else return {} if source.nil? add_resource(source, include_directives, true) @@ -522,7 +522,7 @@ def add_resource(source, include_directives, primary = false) existing = @included_objects[type][id] if existing.nil? - obj_hash = object_hash(source, include_directives) + obj_hash = object_hash(source, **include_directives) @included_objects[type][id] = { primary: primary, object_hash: obj_hash, @@ -539,7 +539,7 @@ def add_resource(source, include_directives, primary = false) end end - def generate_link_builder(primary_resource_klass, options) + def generate_link_builder(primary_resource_klass, **options) LinkBuilder.new( base_url: options.fetch(:base_url, ''), primary_resource_klass: primary_resource_klass, diff --git a/lib/jsonapi/response_document.rb b/lib/jsonapi/response_document.rb index 8be03cbcd..33b5d99c7 100644 --- a/lib/jsonapi/response_document.rb +++ b/lib/jsonapi/response_document.rb @@ -68,7 +68,7 @@ def top_level_links if result.is_a?(JSONAPI::RelatedResourcesOperationResult) relationship = result.source_resource.class._relationships[result._type.to_sym] unless relationship.exclude_link?(link_name) - link = @serializer.link_builder.relationships_related_link(result.source_resource, relationship, query_params(params)) + link = @serializer.link_builder.relationships_related_link(result.source_resource, relationship, **query_params(params)) end else unless @serializer.link_builder.primary_resource_klass.exclude_link?(link_name) diff --git a/test/test_helper.rb b/test/test_helper.rb index 30d439ddb..c7550f754 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -80,7 +80,7 @@ class Engine < ::Rails::Engine # Monkeypatch ActionController::TestCase to delete the RAW_POST_DATA on subsequent calls in the same test. if Rails::VERSION::MAJOR >= 5 module ClearRawPostHeader - def process(action, *args) + def process(action, **args) @request.delete_header 'RAW_POST_DATA' super end @@ -519,13 +519,13 @@ def assert_cacheable_jsonapi_get(url, cached_classes = :all) end class ActionController::TestCase - def assert_cacheable_get(action, *args) + def assert_cacheable_get(action, **args) assert_nil JSONAPI.configuration.resource_cache normal_queries = [] normal_query_callback = lambda {|_, _, _, _, payload| normal_queries.push payload[:sql] } ActiveSupport::Notifications.subscribed(normal_query_callback, 'sql.active_record') do - get action, *args + get action, **args end non_caching_response = json_response_sans_backtraces non_caching_status = response.status @@ -559,7 +559,7 @@ def assert_cacheable_get(action, *args) @controller = nil setup_controller_request_and_response @request.headers.merge!(orig_request_headers.dup) - get action, *args + get action, **args end end rescue Exception diff --git a/test/unit/jsonapi_request/jsonapi_request_test.rb b/test/unit/jsonapi_request/jsonapi_request_test.rb index 4a426c0f9..d526b8aad 100644 --- a/test/unit/jsonapi_request/jsonapi_request_test.rb +++ b/test/unit/jsonapi_request/jsonapi_request_test.rb @@ -27,7 +27,7 @@ def test_parse_includes_underscored request = JSONAPI::RequestParser.new( params, - { + **{ context: nil, key_formatter: JSONAPI::Formatter.formatter_for(:underscored_key) } @@ -47,7 +47,7 @@ def test_parse_dasherized_with_dasherized_include request = JSONAPI::RequestParser.new( params, - { + **{ context: nil, key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key) } @@ -67,7 +67,7 @@ def test_parse_dasherized_with_underscored_include request = JSONAPI::RequestParser.new( params, - { + **{ context: nil, key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key) } @@ -88,7 +88,7 @@ def test_parse_fields_underscored request = JSONAPI::RequestParser.new( params, - { + **{ context: nil, key_formatter: JSONAPI::Formatter.formatter_for(:underscored_key) } @@ -110,7 +110,7 @@ def test_parse_dasherized_with_dasherized_fields request = JSONAPI::RequestParser.new( params, - { + **{ context: nil, key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key) } @@ -133,7 +133,7 @@ def test_parse_dasherized_with_underscored_fields e = assert_raises JSONAPI::Exceptions::InvalidField do JSONAPI::RequestParser.new( params, - { + **{ context: nil, key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key) } @@ -157,7 +157,7 @@ def test_parse_dasherized_with_underscored_resource e = assert_raises JSONAPI::Exceptions::InvalidResource do JSONAPI::RequestParser.new( params, - { + **{ context: nil, key_formatter: JSONAPI::Formatter.formatter_for(:dasherized_key) } diff --git a/test/unit/serializer/link_builder_test.rb b/test/unit/serializer/link_builder_test.rb index d7c277ad2..813c43808 100644 --- a/test/unit/serializer/link_builder_test.rb +++ b/test/unit/serializer/link_builder_test.rb @@ -70,7 +70,7 @@ def test_self_link_regular_app url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = primary_resource_klass.new(@steve, nil) expected_link = "#{ @base_url }/api/v1/people/#{ source.id }" @@ -87,7 +87,7 @@ def test_self_link_regular_app_not_routed url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = primary_resource_klass.new(@great_post, nil) @@ -112,7 +112,7 @@ def test_self_link_regular_app_not_routed assert_equal(err, "self_link for Api::Secret::PostResource could not be generated\n") # should only warn once - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) _out, err = capture_subprocess_io do link = builder.self_link(source) assert_nil link @@ -133,7 +133,7 @@ def test_primary_resources_url_not_routed url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) # Should not warn if warn_on_missing_routes is false JSONAPI.configuration.warn_on_missing_routes = false @@ -155,7 +155,7 @@ def test_primary_resources_url_not_routed assert_equal(err, "primary_resources_url for Api::Secret::PostResource could not be generated\n") # should only warn once - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) _out, err = capture_subprocess_io do link = builder.primary_resources_url assert_nil link @@ -176,7 +176,7 @@ def test_relationships_self_link_not_routed url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = primary_resource_klass.new(@great_post, nil) @@ -203,7 +203,7 @@ def test_relationships_self_link_not_routed assert_equal(err, "self_link for Api::Secret::PostResource.author(BelongsToOne) could not be generated\n") # should only warn once - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) _out, err = capture_subprocess_io do link = builder.relationships_self_link(source, relationship) assert_nil link @@ -224,7 +224,7 @@ def test_relationships_related_link_not_routed url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = primary_resource_klass.new(@great_post, nil) @@ -251,7 +251,7 @@ def test_relationships_related_link_not_routed assert_equal(err, "related_link for Api::Secret::PostResource.author(BelongsToOne) could not be generated\n") # should only warn once - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) _out, err = capture_subprocess_io do link = builder.relationships_related_link(source, relationship) assert_nil link @@ -273,7 +273,7 @@ def test_self_link_with_engine_app url_helpers: ApiV2Engine::Engine.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = primary_resource_klass.new(@steve, nil) expected_link = "#{ @base_url }/api_v2/people/#{ source.id }" @@ -290,7 +290,7 @@ def test_self_link_with_engine_namespaced_app url_helpers: MyEngine::Engine.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = primary_resource_klass.new(@steve, nil) expected_link = "#{ @base_url }/boomshaka/api/v1/people/#{ source.id }" @@ -307,7 +307,7 @@ def test_self_link_with_engine_app_and_camel_case_scope url_helpers: MyEngine::Engine.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = primary_resource_klass.new(@steve, nil) expected_link = "#{ @base_url }/boomshaka/admin_api/v1/people/#{ source.id }" @@ -322,7 +322,7 @@ def test_primary_resources_url_for_regular_app url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/api/v1/people" assert_equal expected_link, builder.primary_resources_url @@ -336,7 +336,7 @@ def test_primary_resources_url_for_engine url_helpers: ApiV2Engine::Engine.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/api_v2/people" assert_equal expected_link, builder.primary_resources_url @@ -350,7 +350,7 @@ def test_primary_resources_url_for_namespaced_engine url_helpers: MyEngine::Engine.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/boomshaka/api/v1/people" assert_equal expected_link, builder.primary_resources_url @@ -364,7 +364,7 @@ def test_relationships_self_link_for_regular_app url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = Api::V1::PersonResource.new(@steve, nil) relationship = Api::V1::PersonResource._relationships[:posts] expected_link = "#{ @base_url }/api/v1/people/#{ @steve.id }/relationships/posts" @@ -381,7 +381,7 @@ def test_relationships_self_link_for_regular_app_singleton url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = Api::V1::PreferencesResource.new(@steves_prefs, nil) relationship = Api::V1::PreferencesResource._relationships[:author] expected_link = "#{ @base_url }/api/v1/preferences/relationships/author" @@ -398,7 +398,7 @@ def test_relationships_related_link_for_regular_app_singleton url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = Api::V1::PreferencesResource.new(@steves_prefs, nil) relationship = Api::V1::PreferencesResource._relationships[:author] expected_link = "#{ @base_url }/api/v1/preferences/author" @@ -415,7 +415,7 @@ def test_relationships_self_link_for_engine url_helpers: ApiV2Engine::Engine.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = ApiV2Engine::PersonResource.new(@steve, nil) relationship = ApiV2Engine::PersonResource._relationships[:posts] expected_link = "#{ @base_url }/api_v2/people/#{ @steve.id }/relationships/posts" @@ -432,7 +432,7 @@ def test_relationships_self_link_for_namespaced_engine url_helpers: MyEngine::Engine.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = MyEngine::Api::V1::PersonResource.new(@steve, nil) relationship = MyEngine::Api::V1::PersonResource._relationships[:posts] expected_link = "#{ @base_url }/boomshaka/api/v1/people/#{ @steve.id }/relationships/posts" @@ -449,7 +449,7 @@ def test_relationships_related_link_for_regular_app url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = Api::V1::PersonResource.new(@steve, nil) relationship = Api::V1::PersonResource._relationships[:posts] expected_link = "#{ @base_url }/api/v1/people/#{ @steve.id }/posts" @@ -466,7 +466,7 @@ def test_relationships_related_link_for_engine url_helpers: ApiV2Engine::Engine.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = ApiV2Engine::PersonResource.new(@steve, nil) relationship = ApiV2Engine::PersonResource._relationships[:posts] expected_link = "#{ @base_url }/api_v2/people/#{ @steve.id }/posts" @@ -483,7 +483,7 @@ def test_relationships_related_link_for_namespaced_engine url_helpers: MyEngine::Engine.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = MyEngine::Api::V1::PersonResource.new(@steve, nil) relationship = MyEngine::Api::V1::PersonResource._relationships[:posts] expected_link = "#{ @base_url }/boomshaka/api/v1/people/#{ @steve.id }/posts" @@ -500,14 +500,14 @@ def test_relationships_related_link_with_query_params url_helpers: TestApp.routes.url_helpers, } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) source = Api::V1::PersonResource.new(@steve, nil) relationship = Api::V1::PersonResource._relationships[:posts] expected_link = "#{ @base_url }/api/v1/people/#{ @steve.id }/posts?page%5Blimit%5D=12&page%5Boffset%5D=0" query = { page: { offset: 0, limit: 12 } } assert_equal expected_link, - builder.relationships_related_link(source, relationship, query) + builder.relationships_related_link(source, relationship, **query) end def test_query_link_for_regular_app @@ -519,7 +519,7 @@ def test_query_link_for_regular_app } query = { page: { offset: 0, limit: 12 } } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/api/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0" assert_equal expected_link, builder.query_link(query) @@ -534,7 +534,7 @@ def test_query_link_for_regular_app_with_camel_case_scope } query = { page: { offset: 0, limit: 12 } } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/admin_api/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0" assert_equal expected_link, builder.query_link(query) @@ -549,7 +549,7 @@ def test_query_link_for_regular_app_with_dasherized_scope } query = { page: { offset: 0, limit: 12 } } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/dasherized-namespace/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0" assert_equal expected_link, builder.query_link(query) @@ -564,7 +564,7 @@ def test_query_link_for_engine } query = { page: { offset: 0, limit: 12 } } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/api_v2/people?page%5Blimit%5D=12&page%5Boffset%5D=0" assert_equal expected_link, builder.query_link(query) @@ -579,7 +579,7 @@ def test_query_link_for_namespaced_engine } query = { page: { offset: 0, limit: 12 } } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/boomshaka/api/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0" assert_equal expected_link, builder.query_link(query) @@ -594,7 +594,7 @@ def test_query_link_for_engine_with_dasherized_scope } query = { page: { offset: 0, limit: 12 } } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/boomshaka/dasherized-namespace/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0" assert_equal expected_link, builder.query_link(query) @@ -609,7 +609,7 @@ def test_query_link_for_engine_with_camel_case_scope } query = { page: { offset: 0, limit: 12 } } - builder = JSONAPI::LinkBuilder.new(config) + builder = JSONAPI::LinkBuilder.new(**config) expected_link = "#{ @base_url }/boomshaka/admin_api/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0" assert_equal expected_link, builder.query_link(query) diff --git a/test/unit/serializer/response_document_test.rb b/test/unit/serializer/response_document_test.rb index 17d8c4b82..3e7c260d3 100644 --- a/test/unit/serializer/response_document_test.rb +++ b/test/unit/serializer/response_document_test.rb @@ -13,7 +13,7 @@ def create_response_document(operation_results, resource_klass) JSONAPI::ResponseDocument.new(operation_results, serializer, - { + **{ primary_resource_klass: resource_klass } ) From 2c8c689ff859f4b5865ccc93263ad17f530943a1 Mon Sep 17 00:00:00 2001 From: Zeeshan Ishtiaq Date: Thu, 22 Jun 2023 17:14:45 +0100 Subject: [PATCH 2/3] ruby 3 upgrade II --- lib/jsonapi/cached_resource_fragment.rb | 2 +- lib/jsonapi/processor.rb | 8 +- lib/jsonapi/relationship.rb | 6 +- lib/jsonapi/relationship_builder.rb | 6 +- lib/jsonapi/resource.rb | 110 ++++++++++++------------ lib/jsonapi/resource_serializer.rb | 10 +-- lib/jsonapi/resources/version.rb | 2 +- lib/jsonapi/response_document.rb | 2 +- lib/jsonapi/routing_ext.rb | 2 +- test/fixtures/active_record.rb | 8 +- test/helpers/assertions.rb | 4 +- test/helpers/value_matchers.rb | 14 +-- test/unit/resource/resource_test.rb | 4 +- 13 files changed, 89 insertions(+), 89 deletions(-) diff --git a/lib/jsonapi/cached_resource_fragment.rb b/lib/jsonapi/cached_resource_fragment.rb index 67df1d9a5..793f4487c 100644 --- a/lib/jsonapi/cached_resource_fragment.rb +++ b/lib/jsonapi/cached_resource_fragment.rb @@ -65,7 +65,7 @@ def to_cache_value end def to_real_resource - rs = Resource.resource_for(self.type).find_by_keys([self.id], {context: self.context}) + rs = Resource.resource_for(self.type).find_by_keys([self.id], context: self.context) return rs.try(:first) end diff --git a/lib/jsonapi/processor.rb b/lib/jsonapi/processor.rb index 75ea766aa..d3cc7c06f 100644 --- a/lib/jsonapi/processor.rb +++ b/lib/jsonapi/processor.rb @@ -82,7 +82,7 @@ def find resource_records = if params[:cache_serializer] resource_klass.find_serialized_with_caching(verified_filters, params[:cache_serializer], - find_options) + **find_options) else resource_klass.find(verified_filters, find_options) end @@ -122,9 +122,9 @@ def show resource_record = if params[:cache_serializer] resource_klass.find_by_key_serialized_with_caching(key, params[:cache_serializer], - find_options) + **find_options) else - resource_klass.find_by_key(key, find_options) + resource_klass.find_by_key(key, **find_options) end return JSONAPI::ResourceOperationResult.new(:ok, resource_record) @@ -187,7 +187,7 @@ def show_related_resources related_resources = relationship.resource_klass.find_serialized_with_caching( scope, params[:cache_serializer], - rel_opts + **rel_opts ) else related_resources = source_resource.public_send(relationship_type, rel_opts) diff --git a/lib/jsonapi/relationship.rb b/lib/jsonapi/relationship.rb index 6aa06e2d9..bcfc02b52 100644 --- a/lib/jsonapi/relationship.rb +++ b/lib/jsonapi/relationship.rb @@ -6,7 +6,7 @@ class Relationship attr_accessor :_routed, :_warned_missing_route - def initialize(name, options = {}) + def initialize(name, **options) @name = name.to_s @options = options @acts_as_set = options.fetch(:acts_as_set, false) == true @@ -96,7 +96,7 @@ def exclude_link?(link) class ToOne < Relationship attr_reader :foreign_key_on - def initialize(name, options = {}) + def initialize(name, **options) super @class_name = options.fetch(:class_name, name.to_s.camelize) @foreign_key ||= "#{name}_id".to_sym @@ -121,7 +121,7 @@ def polymorphic_type class ToMany < Relationship attr_reader :reflect, :inverse_relationship - def initialize(name, options = {}) + def initialize(name, **options) super @class_name = options.fetch(:class_name, name.to_s.camelize.singularize) @foreign_key ||= "#{name.to_s.singularize}_ids".to_sym diff --git a/lib/jsonapi/relationship_builder.rb b/lib/jsonapi/relationship_builder.rb index b00f9aa84..78f85de4b 100644 --- a/lib/jsonapi/relationship_builder.rb +++ b/lib/jsonapi/relationship_builder.rb @@ -21,7 +21,7 @@ def define_relationship_methods(relationship_name) relationship = register_relationship( relationship_name, - relationship_class.new(relationship_name, options) + relationship_class.new(relationship_name, **options) ) foreign_key = define_foreign_key_setter(relationship.foreign_key) @@ -60,11 +60,11 @@ def define_resource_relationship_accessor(type, relationship_name) resource_klass = relationship.resource_klass - records = resource_klass.apply_includes(records, options) + records = resource_klass.apply_includes(records, **options) filters = options.fetch(:filters, {}) unless filters.nil? || filters.empty? - records = resource_klass.apply_filters(records, filters, options) + records = resource_klass.apply_filters(records, filters, **options) end sort_criteria = options.fetch(:sort_criteria, {}) diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index 6fa78dffb..6e901a923 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -74,39 +74,39 @@ def remove end end - def create_to_many_links(relationship_type, relationship_key_values, options = {}) + def create_to_many_links(relationship_type, relationship_key_values, **options) change :create_to_many_link do - _create_to_many_links(relationship_type, relationship_key_values, options) + _create_to_many_links(relationship_type, relationship_key_values, **options) end end - def replace_to_many_links(relationship_type, relationship_key_values, options = {}) + def replace_to_many_links(relationship_type, relationship_key_values, **options) change :replace_to_many_links do - _replace_to_many_links(relationship_type, relationship_key_values, options) + _replace_to_many_links(relationship_type, relationship_key_values, **options) end end - def replace_to_one_link(relationship_type, relationship_key_value, options = {}) + def replace_to_one_link(relationship_type, relationship_key_value, **options) change :replace_to_one_link do - _replace_to_one_link(relationship_type, relationship_key_value, options) + _replace_to_one_link(relationship_type, relationship_key_value, **options) end end - def replace_polymorphic_to_one_link(relationship_type, relationship_key_value, relationship_key_type, options = {}) + def replace_polymorphic_to_one_link(relationship_type, relationship_key_value, relationship_key_type, **options) change :replace_polymorphic_to_one_link do - _replace_polymorphic_to_one_link(relationship_type, relationship_key_value, relationship_key_type, options) + _replace_polymorphic_to_one_link(relationship_type, relationship_key_value, relationship_key_type, **options) end end - def remove_to_many_link(relationship_type, key, options = {}) + def remove_to_many_link(relationship_type, key, **options) change :remove_to_many_link do - _remove_to_many_link(relationship_type, key, options) + _remove_to_many_link(relationship_type, key, **options) end end - def remove_to_one_link(relationship_type, options = {}) + def remove_to_one_link(relationship_type, **options) change :remove_to_one_link do - _remove_to_one_link(relationship_type, options) + _remove_to_one_link(relationship_type, **options) end end @@ -246,7 +246,7 @@ def reflect_relationship?(relationship, options) true end - def _create_to_many_links(relationship_type, relationship_key_values, options) + def _create_to_many_links(relationship_type, relationship_key_values, **options) relationship = self.class._relationships[relationship_type] # check if relationship_key_values are already members of this relationship @@ -290,7 +290,7 @@ def _create_to_many_links(relationship_type, relationship_key_values, options) :completed end - def _replace_to_many_links(relationship_type, relationship_key_values, options) + def _replace_to_many_links(relationship_type, relationship_key_values, **options) relationship = self.class._relationships[relationship_type] reflect = reflect_relationship?(relationship, options) @@ -303,7 +303,7 @@ def _replace_to_many_links(relationship_type, relationship_key_values, options) end to_add = relationship_key_values - (relationship_key_values & existing) - _create_to_many_links(relationship_type, to_add, {}) + _create_to_many_links(relationship_type, to_add, **{}) @reload_needed = true elsif relationship.polymorphic? @@ -312,7 +312,7 @@ def _replace_to_many_links(relationship_type, relationship_key_values, options) ids = relationship_key_value[:ids] related_records = relationship_resource_klass - .records(options) + .records(**options) .where({relationship_resource_klass._primary_key => ids}) missed_ids = ids - related_records.pluck(relationship_resource_klass._primary_key) @@ -334,7 +334,7 @@ def _replace_to_many_links(relationship_type, relationship_key_values, options) :completed end - def _replace_to_one_link(relationship_type, relationship_key_value, options) + def _replace_to_one_link(relationship_type, relationship_key_value, **options) relationship = self.class._relationships[relationship_type] send("#{relationship.foreign_key}=", relationship_key_value) @@ -343,7 +343,7 @@ def _replace_to_one_link(relationship_type, relationship_key_value, options) :completed end - def _replace_polymorphic_to_one_link(relationship_type, key_value, key_type, options) + def _replace_polymorphic_to_one_link(relationship_type, key_value, key_type, **options) relationship = self.class._relationships[relationship_type.to_sym] _model.public_send("#{relationship.foreign_key}=", key_value) @@ -354,7 +354,7 @@ def _replace_polymorphic_to_one_link(relationship_type, key_value, key_type, opt :completed end - def _remove_to_many_link(relationship_type, key, options) + def _remove_to_many_link(relationship_type, key, **options) relationship = self.class._relationships[relationship_type] reflect = reflect_relationship?(relationship, options) @@ -386,7 +386,7 @@ def _remove_to_many_link(relationship_type, key, options) fail JSONAPI::Exceptions::RecordNotFound.new(key) end - def _remove_to_one_link(relationship_type, options) + def _remove_to_one_link(relationship_type, **options) relationship = self.class._relationships[relationship_type] send("#{relationship.foreign_key}=", nil) @@ -531,11 +531,11 @@ def routing_resource_options def attributes(*attrs) options = attrs.extract_options!.dup attrs.each do |attr| - attribute(attr, options) + attribute(attr, **options) end end - def attribute(attribute_name, options = {}) + def attribute(attribute_name, **options) attr = attribute_name.to_sym check_reserved_attribute_name(attr) @@ -599,7 +599,7 @@ def has_many(*attrs) # ``` # so in order to invoke the right class from subclasses, # we should call this method to override it. - def model_name(model, options = {}) + def model_name(model, **options) @model_class = nil @_model_name = model.to_sym @@ -662,18 +662,18 @@ def fields _relationships.keys | _attributes.keys end - def resolve_relationship_names_to_relations(resource_klass, model_includes, options = {}) + def resolve_relationship_names_to_relations(resource_klass, model_includes, **options) case model_includes when Array return model_includes.map do |value| - resolve_relationship_names_to_relations(resource_klass, value, options) + resolve_relationship_names_to_relations(resource_klass, value, **options) end when Hash model_includes.keys.each do |key| relationship = resource_klass._relationships[key] value = model_includes[key] model_includes.delete(key) - model_includes[relationship.relation_name(options)] = resolve_relationship_names_to_relations(relationship.resource_klass, value, options) + model_includes[relationship.relation_name(options)] = resolve_relationship_names_to_relations(relationship.resource_klass, value, **options) end return model_includes when Symbol @@ -682,10 +682,10 @@ def resolve_relationship_names_to_relations(resource_klass, model_includes, opti end end - def apply_includes(records, options = {}) + def apply_includes(records, **options) include_directives = options[:include_directives] if include_directives - model_includes = resolve_relationship_names_to_relations(self, include_directives.model_includes, options) + model_includes = resolve_relationship_names_to_relations(self, include_directives.model_includes, **options) records = records.includes(model_includes) if model_includes.present? end @@ -741,7 +741,7 @@ def _build_joins(associations) joins.join("\n") end - def apply_filter(records, filter, value, options = {}) + def apply_filter(records, filter, value, **options) strategy = _allowed_filters.fetch(filter.to_sym, Hash.new)[:apply] if strategy @@ -755,32 +755,32 @@ def apply_filter(records, filter, value, options = {}) end end - def apply_filters(records, filters, options = {}) + def apply_filters(records, filters, **options) required_includes = [] if filters filters.each do |filter, value| if _relationships.include?(filter) if _relationships[filter].belongs_to? - records = apply_filter(records, _relationships[filter].foreign_key, value, options) + records = apply_filter(records, _relationships[filter].foreign_key, value, **options) else required_includes.push(filter.to_s) - records = apply_filter(records, "#{_relationships[filter].table_name}.#{_relationships[filter].primary_key}", value, options) + records = apply_filter(records, "#{_relationships[filter].table_name}.#{_relationships[filter].primary_key}", value, **options) end else - records = apply_filter(records, filter, value, options) + records = apply_filter(records, filter, value, **options) end end end if required_includes.any? - records = apply_includes(records, options.merge(include_directives: IncludeDirectives.new(self, required_includes, force_eager_load: true))) + records = apply_includes(records, **options.merge(include_directives: IncludeDirectives.new(self, required_includes, force_eager_load: true))) end records end - def apply_included_resources_filters(records, options = {}) + def apply_included_resources_filters(records, **options) include_directives = options[:include_directives] return records unless include_directives related_directives = include_directives.include_directives.fetch(:include_related) @@ -795,18 +795,18 @@ def apply_included_resources_filters(records, options = {}) filters = config[:include_filters] next memo unless filters - rel_records = filtering_resource.apply_filters(filtering_resource.records(options), filters, options).references(relationship_name) + rel_records = filtering_resource.apply_filters(filtering_resource.records(**options), filters, **options).references(relationship_name) memo.merge(rel_records) end end - def filter_records(filters, options, records = records(options)) - records = apply_filters(records, filters, options) - records = apply_includes(records, options) - apply_included_resources_filters(records, options) + def filter_records(filters, options, records = records(**options)) + records = apply_filters(records, filters, **options) + records = apply_includes(records, **options) + apply_included_resources_filters(records, **options) end - def sort_records(records, order_options, context = {}) + def sort_records(records, order_options, context) apply_sort(records, order_options, context) end @@ -815,12 +815,12 @@ def count_records(records) records.count(:all) end - def find_count(filters, options = {}) + def find_count(filters, **options) count_records(filter_records(filters, options)) end def find(filters, options = {}) - resources_for(find_records(filters, options), options[:context]) + resources_for(find_records(filters, **options), options[:context]) end def resources_for(records, context) @@ -830,50 +830,50 @@ def resources_for(records, context) end end - def find_by_keys(keys, options = {}) + def find_by_keys(keys, **options) context = options[:context] - records = records(options) - records = apply_includes(records, options) + records = records(**options) + records = apply_includes(records, **options) models = records.where({_primary_key => keys}) models.collect do |model| self.resource_for_model(model).new(model, context) end end - def find_serialized_with_caching(filters_or_source, serializer, options = {}) + def find_serialized_with_caching(filters_or_source, serializer, **options) if filters_or_source.is_a?(ActiveRecord::Relation) records = filters_or_source elsif _model_class.respond_to?(:all) && _model_class.respond_to?(:arel_table) - records = find_records(filters_or_source, options.except(:include_directives)) + records = find_records(filters_or_source, **options.except(:include_directives)) else records = find(filters_or_source, options) end cached_resources_for(records, serializer, options) end - def find_by_key(key, options = {}) + def find_by_key(key, **options) context = options[:context] - records = find_records({_primary_key => key}, options.except(:paginator, :sort_criteria)) + records = find_records({_primary_key => key}, **options.except(:paginator, :sort_criteria)) model = records.first fail JSONAPI::Exceptions::RecordNotFound.new(key) if model.nil? self.resource_for_model(model).new(model, context) end - def find_by_key_serialized_with_caching(key, serializer, options = {}) + def find_by_key_serialized_with_caching(key, serializer, **options) if _model_class.respond_to?(:all) && _model_class.respond_to?(:arel_table) - results = find_serialized_with_caching({_primary_key => key}, serializer, options) + results = find_serialized_with_caching({_primary_key => key}, serializer, **options) result = results.first fail JSONAPI::Exceptions::RecordNotFound.new(key) if result.nil? return result else - resource = find_by_key(key, options) + resource = find_by_key(key, **options) return cached_resources_for([resource], serializer, options).first end end # Override this method if you want to customize the relation for # finder methods (find, find_by_key, find_serialized_with_caching) - def records(_options = {}) + def records(**_options) _model_class.all end @@ -1196,7 +1196,7 @@ def cached_resources_for(records, serializer, options) resources.values end - def find_records(filters, options = {}) + def find_records(filters, **options) context = options[:context] records = filter_records(filters, options) diff --git a/lib/jsonapi/resource_serializer.rb b/lib/jsonapi/resource_serializer.rb index 0f6d4d0dc..b3aca6767 100644 --- a/lib/jsonapi/resource_serializer.rb +++ b/lib/jsonapi/resource_serializer.rb @@ -139,7 +139,7 @@ def config_description(resource_klass) end # Returns a serialized hash for the source model - def object_hash(source, include_directives = {}) + def object_hash(source, **include_directives) obj_hash = {} if source.is_a?(JSONAPI::CachedResourceFragment) @@ -170,7 +170,7 @@ def object_hash(source, include_directives = {}) attributes = attributes_hash(source, fetchable_fields) obj_hash['attributes'] = attributes unless attributes.empty? - relationships = relationships_hash(source, fetchable_fields, include_directives) + relationships = relationships_hash(source, fetchable_fields, **include_directives) obj_hash['relationships'] = relationships unless relationships.blank? meta = meta_hash(source) @@ -272,7 +272,7 @@ def self_referential_and_already_in_source(resource) resource && @top_level_sources.include?(top_level_source_key(resource)) end - def relationships_hash(source, fetchable_fields, include_directives = {}) + def relationships_hash(source, fetchable_fields, **include_directives) if source.is_a?(CachedResourceFragment) return cached_relationships_hash(source, include_directives) end @@ -309,7 +309,7 @@ def relationships_hash(source, fetchable_fields, include_directives = {}) if include_linkage && !relationships_only add_resource(resource, ia) elsif include_linked_children || relationships_only - relationships_hash(resource, fetchable_fields, ia) + relationships_hash(resource, fetchable_fields, **ia) end end end @@ -531,7 +531,7 @@ def add_resource(source, include_directives, primary = false) else include_related = Set.new(include_directives[:include_related].keys) unless existing[:includes].superset?(include_related) - obj_hash = object_hash(source, include_directives) + obj_hash = object_hash(source, **include_directives) @included_objects[type][id][:object_hash].deep_merge!(obj_hash) @included_objects[type][id][:includes].add(include_related) @included_objects[type][id][:primary] = existing[:primary] | primary diff --git a/lib/jsonapi/resources/version.rb b/lib/jsonapi/resources/version.rb index 28232b327..ddea78c53 100644 --- a/lib/jsonapi/resources/version.rb +++ b/lib/jsonapi/resources/version.rb @@ -1,5 +1,5 @@ module JSONAPI module Resources - VERSION = '0.9.12' + VERSION = '0.9.12.1' end end diff --git a/lib/jsonapi/response_document.rb b/lib/jsonapi/response_document.rb index 33b5d99c7..af4985c1c 100644 --- a/lib/jsonapi/response_document.rb +++ b/lib/jsonapi/response_document.rb @@ -1,6 +1,6 @@ module JSONAPI class ResponseDocument - def initialize(operation_results, serializer, options = {}) + def initialize(operation_results, serializer, **options) @operation_results = operation_results @serializer = serializer @options = options diff --git a/lib/jsonapi/routing_ext.rb b/lib/jsonapi/routing_ext.rb index 5aae3537e..172f592f4 100644 --- a/lib/jsonapi/routing_ext.rb +++ b/lib/jsonapi/routing_ext.rb @@ -69,7 +69,7 @@ def jsonapi_resource(*resources, &_block) end end - def jsonapi_relationships(options = {}) + def jsonapi_relationships(**options) res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type)) res._relationships.each do |relationship_name, relationship| if relationship.is_a?(JSONAPI::Relationship::ToMany) diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index 746307c9d..eed3351bb 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -1267,7 +1267,7 @@ def self.find(filters, options = {}) breeds end - def self.find_by_key(id, options = {}) + def self.find_by_key(id, **options) BreedResource.new($breed_data.breeds[id.to_i], options[:context]) end @@ -1333,7 +1333,7 @@ class PreferencesResource < JSONAPI::Resource has_one :author, :foreign_key_on => :related - def self.find_records(filters, options = {}) + def self.find_records(filters, **options) Preferences.limit(1) end end @@ -1715,7 +1715,7 @@ class AuthorResource < JSONAPI::Resource filter :name - def self.find_records(filters, options = {}) + def self.find_records(filters, **options) rel = _model_class filters.each do |attr, filter| if attr.to_s == "id" @@ -1807,7 +1807,7 @@ class CommentResource < CommentResource; end class PostResource < PostResource # Test caching with SQL fragments - def self.records(options = {}) + def self.records(**options) super.joins('INNER JOIN people on people.id = author_id') end end diff --git a/test/helpers/assertions.rb b/test/helpers/assertions.rb index 0d100f985..c465a815b 100644 --- a/test/helpers/assertions.rb +++ b/test/helpers/assertions.rb @@ -2,12 +2,12 @@ module Helpers module Assertions def assert_hash_equals(exp, act, msg = nil) msg = message(msg, '') { diff exp, act } - assert(matches_hash?(exp, act, {exact: true}), msg) + assert(matches_hash?(exp, act, exact: true), msg) end def assert_array_equals(exp, act, msg = nil) msg = message(msg, '') { diff exp, act } - assert(matches_array?(exp, act, {exact: true}), msg) + assert(matches_array?(exp, act, exact: true), msg) end end end diff --git a/test/helpers/value_matchers.rb b/test/helpers/value_matchers.rb index c5bb2ab01..93eff05af 100644 --- a/test/helpers/value_matchers.rb +++ b/test/helpers/value_matchers.rb @@ -1,22 +1,22 @@ module Helpers module ValueMatchers ### Matchers - def matches_value?(v1, v2, options = {}) + def matches_value?(v1, v2, **options) if v1 == :any # any value is acceptable elsif v1 == :not_nil return false if v2 == nil elsif v1.kind_of?(Hash) - return false unless matches_hash?(v1, v2, options) + return false unless matches_hash?(v1, v2, **options) elsif v1.kind_of?(Array) - return false unless matches_array?(v1, v2, options) + return false unless matches_array?(v1, v2, **options) else return false unless v2 == v1 end true end - def matches_array?(array1, array2, options = {}) + def matches_array?(array1, array2, **options) return false unless array1.kind_of?(Array) && array2.kind_of?(Array) if options[:exact] return false unless array1.size == array2.size @@ -31,7 +31,7 @@ def matches_array?(array1, array2, options = {}) matched = {} (0..(array1.size - 1)).each do |i| (0..(array2.size - 1)).each do |j| - if !matched.has_value?(j.to_s) && matches_value?(array1[i], array2[j], options) + if !matched.has_value?(j.to_s) && matches_value?(array1[i], array2[j], **options) matched[i.to_s] = j.to_s break end @@ -42,7 +42,7 @@ def matches_array?(array1, array2, options = {}) end # options => {exact: true} # hashes must match exactly (i.e. have same number of key-value pairs that are all equal) - def matches_hash?(hash1, hash2, options = {}) + def matches_hash?(hash1, hash2, **options) return false unless hash1.kind_of?(Hash) && hash2.kind_of?(Hash) if options[:exact] return false unless hash1.size == hash2.size @@ -52,7 +52,7 @@ def matches_hash?(hash1, hash2, options = {}) hash2 = hash2.deep_symbolize_keys hash1.each do |k1, v1| - return false unless hash2.has_key?(k1) && matches_value?(v1, hash2[k1], options) + return false unless hash2.has_key?(k1) && matches_value?(v1, hash2[k1], **options) end true end diff --git a/test/unit/resource/resource_test.rb b/test/unit/resource/resource_test.rb index 7fe44bbd8..b61d388a5 100644 --- a/test/unit/resource/resource_test.rb +++ b/test/unit/resource/resource_test.rb @@ -348,7 +348,7 @@ def test_to_many_relationship_filters # define apply_filters method on post resource to not respect filters PostResource.instance_eval do - def apply_filters(records, filters, options) + def apply_filters(records, filters, **options) # :nocov: records # :nocov: @@ -361,7 +361,7 @@ def apply_filters(records, filters, options) ensure # reset method to original implementation PostResource.instance_eval do - def apply_filters(records, filters, options) + def apply_filters(records, filters, **options) # :nocov: super # :nocov: From b1314a2069e330a38a47463a6ad5af3970a7722a Mon Sep 17 00:00:00 2001 From: Zeeshan Ishtiaq Date: Fri, 23 Jun 2023 11:51:53 +0100 Subject: [PATCH 3/3] ruby 3 upgrade III --- lib/jsonapi/resource_serializer.rb | 6 +++--- lib/jsonapi/resources/version.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/jsonapi/resource_serializer.rb b/lib/jsonapi/resource_serializer.rb index b3aca6767..2307a6bd8 100644 --- a/lib/jsonapi/resource_serializer.rb +++ b/lib/jsonapi/resource_serializer.rb @@ -149,7 +149,7 @@ def object_hash(source, **include_directives) obj_hash['links'] = source.links_json if source.links_json obj_hash['attributes'] = source.attributes_json if source.attributes_json - relationships = cached_relationships_hash(source, include_directives) + relationships = cached_relationships_hash(source, **include_directives) obj_hash['relationships'] = relationships unless relationships.blank? obj_hash['meta'] = source.meta_json if source.meta_json @@ -274,7 +274,7 @@ def self_referential_and_already_in_source(resource) def relationships_hash(source, fetchable_fields, **include_directives) if source.is_a?(CachedResourceFragment) - return cached_relationships_hash(source, include_directives) + return cached_relationships_hash(source, **include_directives) end include_directives[:include_related] ||= {} @@ -316,7 +316,7 @@ def relationships_hash(source, fetchable_fields, **include_directives) end end - def cached_relationships_hash(source, include_directives) + def cached_relationships_hash(source, **include_directives) h = source.relationships || {} return h unless include_directives.has_key?(:include_related) diff --git a/lib/jsonapi/resources/version.rb b/lib/jsonapi/resources/version.rb index ddea78c53..1eebd13a4 100644 --- a/lib/jsonapi/resources/version.rb +++ b/lib/jsonapi/resources/version.rb @@ -1,5 +1,5 @@ module JSONAPI module Resources - VERSION = '0.9.12.1' + VERSION = '0.9.12.2' end end