From 1ec6a8a0099e0460acf843c51293ff65092e47e7 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Mon, 1 Oct 2018 10:31:20 -0300 Subject: [PATCH 01/18] Do some refactoring --- lib/jsonapi/utils/response/formatters.rb | 35 ++++++++++++++---------- lib/jsonapi/utils/support/pagination.rb | 10 +++++++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/jsonapi/utils/response/formatters.rb b/lib/jsonapi/utils/response/formatters.rb index 055b026..54c75d3 100644 --- a/lib/jsonapi/utils/response/formatters.rb +++ b/lib/jsonapi/utils/response/formatters.rb @@ -120,8 +120,8 @@ def build_response_document(object, options) # @option options [JSONAPI::Resource] :resource which resource class to be used # rather than using the default one (inferred) # - # @option options [ActiveRecord::Base, JSONAPI::Resource] :source source of related resource, - # the result should be interpreted as a related resources response + # @option options [ActiveRecord::Base, JSONAPI::Resource] :source parent model/resource + # of the related resource # # @option options [String, Symbol] :relationship which relationship the data is from # @@ -136,15 +136,10 @@ def build_collection_result(object, options) records = build_collection(object, options) result_options = result_options(object, options) - if options[:source].present? && related_resource_operation? + if related_resource_operation?(options) source_resource = turn_source_into_resource(options[:source]) relationship_type = get_source_relationship(options) - JSONAPI::RelatedResourcesOperationResult.new(:ok, - source_resource, - relationship_type, - records, - result_options - ) + JSONAPI::RelatedResourcesOperationResult.new(:ok, source_resource, relationship_type, records, result_options) else JSONAPI::ResourcesOperationResult.new(:ok, records, result_options) end @@ -152,11 +147,24 @@ def build_collection_result(object, options) # Is this a request for related resources? # + # In order to answer that it needs to check for some {options} + # controller params like {params[:source]} and {params[:relationship]}. + # + # @option options [Boolean] :related when true, jsonapi-utils infers the parent and + # related resources from controller's {params} values. + # + # @option options [ActiveRecord::Base, JSONAPI::Resource] :source parent model/resource + # of the related resource + # + # @option options [String, Symbol] :relationship which relationship the data is from + # # @return [Boolean] # # @api private - def related_resource_operation? - params[:source].present? && params[:relationship].present? + def related_resource_operation?(options) + (options[:related] || options[:source].present?) && + params[:source].present? && + params[:relationship].present? end # Apply a proper action setup for custom requests/actions. @@ -263,8 +271,7 @@ def get_source_relationship(options) # @api private def result_options(records, options) {}.tap do |data| - if JSONAPI.configuration.default_paginator != :none && - JSONAPI.configuration.top_level_links_include_pagination + if include_pagination_links? data[:pagination_params] = pagination_params(records, options) end @@ -272,7 +279,7 @@ def result_options(records, options) data[:record_count] = record_count_for(records, options) end - if JSONAPI.configuration.top_level_meta_include_page_count + if include_page_count? data[:page_count] = page_count_for(data[:record_count]) end end diff --git a/lib/jsonapi/utils/support/pagination.rb b/lib/jsonapi/utils/support/pagination.rb index c8f1eaf..5ef3130 100644 --- a/lib/jsonapi/utils/support/pagination.rb +++ b/lib/jsonapi/utils/support/pagination.rb @@ -4,6 +4,15 @@ module Support module Pagination RecordCountError = Class.new(ArgumentError) + def include_pagination_links? + JSONAPI.configuration.default_paginator != :none && + JSONAPI.configuration.top_level_links_include_pagination + end + + def include_page_count? + JSONAPI.configuration.top_level_meta_include_page_count + end + # Apply proper pagination to the records. # # @param records [ActiveRecord::Relation, Array] collection of records @@ -32,6 +41,7 @@ def apply_pagination(records, options = {}) # @return [Hash] # e.g.: {"first"=>{"number"=>1, "size"=>2}, "next"=>{"number"=>2, "size"=>2}, "last"=>{"number"=>2, "size"=>2}} # + # # @api public def pagination_params(records, options) return {} unless JSONAPI.configuration.top_level_links_include_pagination From 36c76019472b692295172a7945f40c8da9d85441 Mon Sep 17 00:00:00 2001 From: Dmitry Krasnoukhov Date: Wed, 26 Jun 2019 16:31:04 +0300 Subject: [PATCH 02/18] Compatibility with jsonapi-resources@0.9.9 --- jsonapi-utils.gemspec | 2 +- spec/controllers/users_controller_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index a89bc18..1ad964d 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_runtime_dependency 'jsonapi-resources', '0.9.8' + spec.add_runtime_dependency 'jsonapi-resources', '0.9.9' spec.add_development_dependency 'bundler', '~> 1.10' spec.add_development_dependency 'rake', '~> 10.0' diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 56786ef..50ed116 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -28,7 +28,7 @@ expect(response).to have_http_status :ok expect(response).to have_primary_data('users') expect(response).to have_data_attributes(fields) - expect(response).to have_relationships(relationships) + expect(response).to have_relationships(relationships - ['profile']) end context 'with "include"' do From 26b1c16e531cf82fd51d13fadb296d37c8d3c224 Mon Sep 17 00:00:00 2001 From: Grzesiek Kolodziejczyk Date: Tue, 15 Oct 2019 14:14:59 +0200 Subject: [PATCH 03/18] Compatibility with jsonapi-resources@0.9.10 --- jsonapi-utils.gemspec | 2 +- spec/controllers/users_controller_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index a89bc18..a04b149 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_runtime_dependency 'jsonapi-resources', '0.9.8' + spec.add_runtime_dependency 'jsonapi-resources', '0.9.10' spec.add_development_dependency 'bundler', '~> 1.10' spec.add_development_dependency 'rake', '~> 10.0' diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 56786ef..50ed116 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -28,7 +28,7 @@ expect(response).to have_http_status :ok expect(response).to have_primary_data('users') expect(response).to have_data_attributes(fields) - expect(response).to have_relationships(relationships) + expect(response).to have_relationships(relationships - ['profile']) end context 'with "include"' do From 8ba1e2735e43d64b061fad92234c88eaa8a48310 Mon Sep 17 00:00:00 2001 From: Grzesiek Kolodziejczyk Date: Tue, 17 Dec 2019 11:24:43 +0100 Subject: [PATCH 04/18] Compatibility with jsonapi-resources@0.9.11 --- jsonapi-utils.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index a04b149..25d196f 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_runtime_dependency 'jsonapi-resources', '0.9.10' + spec.add_runtime_dependency 'jsonapi-resources', '0.9.11' spec.add_development_dependency 'bundler', '~> 1.10' spec.add_development_dependency 'rake', '~> 10.0' From 6250513460e486c8e9ad1f20a0e01c138d17200c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Fija=C5=82kowski?= Date: Thu, 2 Jan 2020 12:08:03 +0100 Subject: [PATCH 05/18] Remove redundant _paginator from docs example There is no need to add _paginator in pagination config- paginator_klass do this already: def paginator_klass "#{JSONAPI.configuration.default_paginator}_paginator".classify.constantize end --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bc2a712..d66cddc 100644 --- a/README.md +++ b/README.md @@ -225,12 +225,12 @@ class CustomPaginator < JSONAPI::Paginator end ``` -And then it can be either set at the resource class level (e.g. UserResource.paginator :custom_paginator) or via config initializer: +And then it can be either set at the resource class level (e.g. UserResource.paginator :custom) or via config initializer: ```ruby # config/initializers/jsonapi_resources.rb JSONAPI.configure do |config| - config.default_paginator = :custom_paginator + config.default_paginator = :custom end ``` From 069f44ff491084ea4292d06e208497fe299b7a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Fija=C5=82kowski?= Date: Thu, 2 Jan 2020 12:39:45 +0100 Subject: [PATCH 06/18] Update Travis config with new Ruby and Rails --- .travis.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8bf1951..24ab966 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,13 @@ language: ruby sudo: false +bundler_args: --without development env: - - 'RAILS_VERSION=5.1.2' + matrix: + - "RAILS_VERSION=5.2" + - "RAILS_VERSION=6.0" rvm: - - 2.3.3 -matrix: -before_install: gem install bundler -v 1.13.6 + - 2.4 + - 2.5 + - 2.6 + - 2.7 script: bundle exec rspec spec - From fde6375acd902bf98103c8217b6e73f796510b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Fija=C5=82kowski?= Date: Thu, 2 Jan 2020 12:41:09 +0100 Subject: [PATCH 07/18] Update default rails dependency from 5.1 to 6.0 --- jsonapi-utils.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index a89bc18..2e0d27c 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -23,7 +23,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler', '~> 1.10' spec.add_development_dependency 'rake', '~> 10.0' - spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 5.1' + spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 6.0' spec.add_development_dependency 'sqlite3', '~> 1.3.6' spec.add_development_dependency 'rspec-rails', '~> 3.1' spec.add_development_dependency 'factory_girl', '~> 4.8' From ff69e39b1a5de6e80fb33ac42ac0f92dbb3198fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Fija=C5=82kowski?= Date: Thu, 2 Jan 2020 13:56:16 +0100 Subject: [PATCH 08/18] Update development dependencies --- jsonapi-utils.gemspec | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index 2e0d27c..4247f1b 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -23,11 +23,11 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler', '~> 1.10' spec.add_development_dependency 'rake', '~> 10.0' - spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 6.0' - spec.add_development_dependency 'sqlite3', '~> 1.3.6' - spec.add_development_dependency 'rspec-rails', '~> 3.1' + spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 5.2.4' + spec.add_development_dependency 'sqlite3', '~> 1.4' + spec.add_development_dependency 'rspec-rails', '~> 3.9.0' spec.add_development_dependency 'factory_girl', '~> 4.8' spec.add_development_dependency 'smart_rspec', '~> 0.1.6' - spec.add_development_dependency 'pry', '~> 0.10.3' + spec.add_development_dependency 'pry', '~> 0.12.2' spec.add_development_dependency 'pry-byebug' end From cf5892c9d895d685d7bd499249c4b049a1bf60c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Fija=C5=82kowski?= Date: Thu, 2 Jan 2020 16:19:02 +0100 Subject: [PATCH 09/18] Add to .travis before install bundler --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 24ab966..f229703 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,3 +11,6 @@ rvm: - 2.6 - 2.7 script: bundle exec rspec spec + +before_install: + - gem install bundler From 578023a6eb381d6676b51b2f19c22c1183b02d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Fija=C5=82kowski?= Date: Thu, 2 Jan 2020 17:21:49 +0100 Subject: [PATCH 10/18] Set dev dependency for bundler ~> 1.10, < 2.2 --- jsonapi-utils.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index 4247f1b..3453da4 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'jsonapi-resources', '0.9.8' - spec.add_development_dependency 'bundler', '~> 1.10' + spec.add_development_dependency 'bundler', '~> 1.10', '< 2.2' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 5.2.4' spec.add_development_dependency 'sqlite3', '~> 1.4' From 92ed70292304a8d634c12b0e78459ce2924e4d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Fija=C5=82kowski?= Date: Wed, 8 Jan 2020 14:31:28 +0100 Subject: [PATCH 11/18] Change dev dependency for bundler to ~> 2.0 --- jsonapi-utils.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index 3453da4..be47927 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'jsonapi-resources', '0.9.8' - spec.add_development_dependency 'bundler', '~> 1.10', '< 2.2' + spec.add_development_dependency 'bundler', '~> 2.0' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 5.2.4' spec.add_development_dependency 'sqlite3', '~> 1.4' From 6fe00af6ebb15b6835d2647b210b65eb01d18808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Fija=C5=82kowski?= Date: Wed, 8 Jan 2020 14:57:21 +0100 Subject: [PATCH 12/18] Remove travis bundler_args without development --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f229703..be72d86 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,4 @@ language: ruby -sudo: false -bundler_args: --without development env: matrix: - "RAILS_VERSION=5.2" From 09e232bb6335a23d6c3e04e2725887df04ab03d5 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 12 Apr 2020 17:23:45 -0300 Subject: [PATCH 13/18] Apply some minor refactoring --- lib/jsonapi/utils/support/pagination.rb | 19 +++++++++++++++---- spec/features/page_count_spec.rb | 1 - 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/jsonapi/utils/support/pagination.rb b/lib/jsonapi/utils/support/pagination.rb index 5ef3130..5687fe1 100644 --- a/lib/jsonapi/utils/support/pagination.rb +++ b/lib/jsonapi/utils/support/pagination.rb @@ -4,11 +4,20 @@ module Support module Pagination RecordCountError = Class.new(ArgumentError) + # Check whether pagination links should be included. + # + # @api public + # @return [Boolean] def include_pagination_links? JSONAPI.configuration.default_paginator != :none && JSONAPI.configuration.top_level_links_include_pagination end + # Check whether pagination's page count should be included + # on the "meta" key. + # + # @api public + # @return [Boolean] def include_page_count? JSONAPI.configuration.top_level_meta_include_page_count end @@ -18,15 +27,17 @@ def include_page_count? # @param records [ActiveRecord::Relation, Array] collection of records # e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }] # - # @param options [Hash] JU's options + # @param options [Hash] JSONAPI::Utils' options # e.g.: { resource: V2::UserResource, count: 100 } # # @return [ActiveRecord::Relation, Array] # # @api public def apply_pagination(records, options = {}) - return records unless apply_pagination?(options) - records.is_a?(Array) ? records[paginate_with(:range)] : paginate_with(:paginator).apply(records, nil) + if !apply_pagination?(options) then records + elsif records.is_a?(Array) then records[paginate_with(:range)] + else paginate_with(:paginator).apply(records, nil) + end end # Mount pagination params for JSONAPI::ResourcesOperationResult. @@ -44,7 +55,7 @@ def apply_pagination(records, options = {}) # # @api public def pagination_params(records, options) - return {} unless JSONAPI.configuration.top_level_links_include_pagination + return {} unless include_pagination_links? paginator.links_page_params(record_count: record_count_for(records, options)) end diff --git a/spec/features/page_count_spec.rb b/spec/features/page_count_spec.rb index a85d761..b779b22 100644 --- a/spec/features/page_count_spec.rb +++ b/spec/features/page_count_spec.rb @@ -29,7 +29,6 @@ def TestApp.draw_page_count_test_routes # Feature Tests ## - describe PageCountTestController, type: :controller do include_context 'JSON API headers' From e26a9ff0eb6d01fcb0c25f094ac28804ada63cf3 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 12 Apr 2020 17:30:37 -0300 Subject: [PATCH 14/18] Update rake version --- jsonapi-utils.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index a89bc18..8dc6676 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'jsonapi-resources', '0.9.8' spec.add_development_dependency 'bundler', '~> 1.10' - spec.add_development_dependency 'rake', '~> 10.0' + spec.add_development_dependency 'rake', '~> 12.3.3' spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 5.1' spec.add_development_dependency 'sqlite3', '~> 1.3.6' spec.add_development_dependency 'rspec-rails', '~> 3.1' From d1939e90f5ceed19f738568461b19d57f4609fe6 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Thu, 23 Apr 2020 23:52:42 -0300 Subject: [PATCH 15/18] Update bundler and factory_bot --- jsonapi-utils.gemspec | 4 +- spec/controllers/posts_controller_spec.rb | 2 +- spec/controllers/users_controller_spec.rb | 2 +- spec/features/page_count_spec.rb | 2 +- spec/features/record_count_spec.rb | 2 +- spec/jsonapi/utils/support/pagination_spec.rb | 2 +- spec/spec_helper.rb | 4 +- spec/support/factories.rb | 37 ++++++++++--------- 8 files changed, 29 insertions(+), 26 deletions(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index ad5e39d..c7b78d1 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -21,12 +21,12 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'jsonapi-resources', '0.9.11' - spec.add_development_dependency 'bundler', '~> 2.0' + spec.add_development_dependency 'bundler', '>= 1.14' spec.add_development_dependency 'rake', '~> 12.3.3' spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 5.2.4' spec.add_development_dependency 'sqlite3', '~> 1.4' spec.add_development_dependency 'rspec-rails', '~> 3.9.0' - spec.add_development_dependency 'factory_girl', '~> 4.8' + spec.add_development_dependency 'factory_bot', '~> 5.1' spec.add_development_dependency 'smart_rspec', '~> 0.1.6' spec.add_development_dependency 'pry', '~> 0.12.2' spec.add_development_dependency 'pry-byebug' diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index b709d08..45b74f6 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -4,7 +4,7 @@ include_context 'JSON API headers' before(:all) do - @post = FactoryGirl.create_list(:post, 3).first + @post = FactoryBot.create_list(:post, 3).first end before(:each) do diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 50ed116..8556f94 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -4,7 +4,7 @@ include_context 'JSON API headers' before(:all) do - @user = FactoryGirl.create_list(:user, 3, :with_posts).first + @user = FactoryBot.create_list(:user, 3, :with_posts).first end before(:each) do diff --git a/spec/features/page_count_spec.rb b/spec/features/page_count_spec.rb index b779b22..9c3ec64 100644 --- a/spec/features/page_count_spec.rb +++ b/spec/features/page_count_spec.rb @@ -34,7 +34,7 @@ def TestApp.draw_page_count_test_routes before(:all) do TestApp.draw_page_count_test_routes - FactoryGirl.create_list(:user, 3, :with_posts) + FactoryBot.create_list(:user, 3, :with_posts) end describe 'page count with a paged paginator' do diff --git a/spec/features/record_count_spec.rb b/spec/features/record_count_spec.rb index b90a982..b8113bc 100644 --- a/spec/features/record_count_spec.rb +++ b/spec/features/record_count_spec.rb @@ -56,7 +56,7 @@ def TestApp.draw_record_count_test_routes before(:all) do TestApp.draw_record_count_test_routes - FactoryGirl.create_list(:user, 3, :with_posts) + FactoryBot.create_list(:user, 3, :with_posts) end describe 'explicit count' do diff --git a/spec/jsonapi/utils/support/pagination_spec.rb b/spec/jsonapi/utils/support/pagination_spec.rb index c1b2632..f032295 100644 --- a/spec/jsonapi/utils/support/pagination_spec.rb +++ b/spec/jsonapi/utils/support/pagination_spec.rb @@ -6,7 +6,7 @@ end before(:all) do - FactoryGirl.create_list(:user, 2) + FactoryBot.create_list(:user, 2) end let(:options) { {} } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d6c565f..17ef5b0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,10 +1,10 @@ require 'smart_rspec' -require 'factory_girl' +require 'factory_bot' require 'support/helpers' RSpec.configure do |config| - config.include FactoryGirl::Syntax::Methods config.include Helpers::ResponseParser + config.include FactoryBot::Syntax::Methods config.define_derived_metadata do |meta| meta[:aggregate_failures] = true diff --git a/spec/support/factories.rb b/spec/support/factories.rb index d99b29e..57da6b6 100644 --- a/spec/support/factories.rb +++ b/spec/support/factories.rb @@ -1,21 +1,9 @@ -require 'factory_girl' +require 'factory_bot' +require_relative './models' -FactoryGirl.define do - factory :category, class: Category do - sequence(:title) { |n| "Title for Category #{n}" } - end - - factory :post, class: Post do - association :author, factory: :user - category - - sequence(:id) { |n| n } - sequence(:title) { |n| "Title for Post #{n}" } - sequence(:body) { |n| "Body for Post #{n}" } - content_type :article - hidden_field 'It\'s a hidden field!' - end +# require 'byebug'; byebug +FactoryBot.define do factory :user, class: User do sequence(:id) { |n| n } sequence(:first_name) { |n| "User##{n}" } @@ -24,7 +12,7 @@ after(:create) { |user| create(:profile, user: user) } trait :with_posts do - transient { post_count 3 } + transient { post_count { 3 } } after(:create) do |user, e| create_list(:post, e.post_count, author: user) end @@ -37,4 +25,19 @@ sequence(:nickname) { |n| "Nickname##{n}" } sequence(:location) { |n| "Location##{n}" } end + + factory :post, class: Post do + association :author, factory: :user + category + + sequence(:id) { |n| n } + sequence(:title) { |n| "Title for Post #{n}" } + sequence(:body) { |n| "Body for Post #{n}" } + content_type { :article } + hidden_field { 'It\'s a hidden field!' } + end + + factory :category, class: Category do + sequence(:title) { |n| "Title for Category #{n}" } + end end From f4b294a464e8744d3a0fecf1a8f6609b2ab55c46 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 26 Apr 2020 16:56:56 -0300 Subject: [PATCH 16/18] Update README and bump up gem's version --- README.md | 6 +++--- lib/jsonapi/utils/response/formatters.rb | 17 ++++++++++++----- lib/jsonapi/utils/version.rb | 2 +- spec/support/controllers.rb | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d66cddc..47494be 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ end Support: * Ruby 1.9+ with Rails 4 -* Ruby 2.3+ with Rails 5 +* Ruby 2.4+ with Rails 5 For Rails 4 add this to your application's Gemfile: @@ -62,10 +62,10 @@ For Rails 4 add this to your application's Gemfile: gem 'jsonapi-utils', '~> 0.4.9' ``` -For Rails 5: +For Rails 5+: ```ruby -gem 'jsonapi-utils', '~> 0.7.2' +gem 'jsonapi-utils', '~> 0.7.3' ``` And then execute: diff --git a/lib/jsonapi/utils/response/formatters.rb b/lib/jsonapi/utils/response/formatters.rb index 54c75d3..00f4f2f 100644 --- a/lib/jsonapi/utils/response/formatters.rb +++ b/lib/jsonapi/utils/response/formatters.rb @@ -15,7 +15,7 @@ module Formatters # @option options [JSONAPI::Resource] resource: it tells the formatter which resource # class to be used rather than use an infered one (default behaviour) # - # @option options [JSONAPI::Resource] source_resource: it tells the formatter that this response is from a related resource + # @option options [JSONAPI::Resource] source: it tells the formatter that this response is from a related resource # and the result should be interpreted as a related resources response # # @option options [String, Symbol] relationship_type: it tells that the formatter which relationship the data is from @@ -32,7 +32,7 @@ module Formatters # @api public def jsonapi_format(object, options = {}) if object.is_a?(Hash) - hash = object.with_indifferent_access + hash = object.with_indifferent_access object = hash_to_active_record(hash[:data], options[:model]) end fix_custom_request_options(object) @@ -133,13 +133,20 @@ def build_response_document(object, options) # # @api private def build_collection_result(object, options) - records = build_collection(object, options) + records = build_collection(object, options) result_options = result_options(object, options) if related_resource_operation?(options) - source_resource = turn_source_into_resource(options[:source]) + source_resource = turn_source_into_resource(options[:source]) relationship_type = get_source_relationship(options) - JSONAPI::RelatedResourcesOperationResult.new(:ok, source_resource, relationship_type, records, result_options) + + JSONAPI::RelatedResourcesOperationResult.new( + :ok, + source_resource, + relationship_type, + records, + result_options + ) else JSONAPI::ResourcesOperationResult.new(:ok, records, result_options) end diff --git a/lib/jsonapi/utils/version.rb b/lib/jsonapi/utils/version.rb index c8c1c81..1bddf34 100644 --- a/lib/jsonapi/utils/version.rb +++ b/lib/jsonapi/utils/version.rb @@ -1,5 +1,5 @@ module JSONAPI module Utils - VERSION = '0.7.2'.freeze + VERSION = '0.7.3'.freeze end end diff --git a/spec/support/controllers.rb b/spec/support/controllers.rb index d937320..96c57b7 100644 --- a/spec/support/controllers.rb +++ b/spec/support/controllers.rb @@ -14,7 +14,7 @@ def index jsonapi_render json: @user.posts, options: { count: 100 } end - # GET /users/:user_id//index_with_hash + # GET /users/:user_id/index_with_hash def index_with_hash @posts = { data: [ { id: 1, title: 'Lorem Ipsum', body: 'Body 4' }, From e761c83b1df7b9afa55b284804b69410de632539 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 26 Apr 2020 17:07:15 -0300 Subject: [PATCH 17/18] Improve version requirements of some development dependencies --- jsonapi-utils.gemspec | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/jsonapi-utils.gemspec b/jsonapi-utils.gemspec index c7b78d1..a26093d 100644 --- a/jsonapi-utils.gemspec +++ b/jsonapi-utils.gemspec @@ -21,13 +21,13 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'jsonapi-resources', '0.9.11' - spec.add_development_dependency 'bundler', '>= 1.14' - spec.add_development_dependency 'rake', '~> 12.3.3' - spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 5.2.4' + spec.add_development_dependency 'bundler', '~> 1.14', '>= 1.14' + spec.add_development_dependency 'rake', '~> 12.3', '>= 12.3.3' + spec.add_development_dependency 'rails', (ENV['RAILS_VERSION'] || '~> 5.2'), '>= 5.2.4' spec.add_development_dependency 'sqlite3', '~> 1.4' - spec.add_development_dependency 'rspec-rails', '~> 3.9.0' + spec.add_development_dependency 'rspec-rails', '~> 3.9', '>= 3.9.0' spec.add_development_dependency 'factory_bot', '~> 5.1' spec.add_development_dependency 'smart_rspec', '~> 0.1.6' - spec.add_development_dependency 'pry', '~> 0.12.2' - spec.add_development_dependency 'pry-byebug' + spec.add_development_dependency 'pry', '~> 0.12', '>= 0.12.2' + spec.add_development_dependency 'pry-byebug', '~> 3.7', '>= 3.7.0' end From f36ea9c80d5a1f6f503c4c45e0ec49e7318d3516 Mon Sep 17 00:00:00 2001 From: Sean Abrahams Date: Tue, 10 Sep 2019 15:25:22 -0700 Subject: [PATCH 18/18] Add support for passing in custom meta information in responses. --- lib/jsonapi/utils/response/formatters.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/jsonapi/utils/response/formatters.rb b/lib/jsonapi/utils/response/formatters.rb index 00f4f2f..c821c36 100644 --- a/lib/jsonapi/utils/response/formatters.rb +++ b/lib/jsonapi/utils/response/formatters.rb @@ -278,6 +278,8 @@ def get_source_relationship(options) # @api private def result_options(records, options) {}.tap do |data| + data[:meta] = options.fetch(:meta, {}) + if include_pagination_links? data[:pagination_params] = pagination_params(records, options) end