Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1ec6a8a
Do some refactoring
tiagopog Oct 1, 2018
36c7601
Compatibility with jsonapi-resources@0.9.9
krasnoukhov Jun 26, 2019
26b1c16
Compatibility with jsonapi-resources@0.9.10
grk Oct 15, 2019
8ba1e27
Compatibility with jsonapi-resources@0.9.11
grk Dec 17, 2019
6250513
Remove redundant _paginator from docs example
brateq Jan 2, 2020
069f44f
Update Travis config with new Ruby and Rails
brateq Jan 2, 2020
fde6375
Update default rails dependency from 5.1 to 6.0
brateq Jan 2, 2020
ff69e39
Update development dependencies
brateq Jan 2, 2020
cf5892c
Add to .travis before install bundler
brateq Jan 2, 2020
578023a
Set dev dependency for bundler ~> 1.10, < 2.2
brateq Jan 2, 2020
92ed702
Change dev dependency for bundler to ~> 2.0
brateq Jan 8, 2020
6fe00af
Remove travis bundler_args without development
brateq Jan 8, 2020
09e232b
Apply some minor refactoring
tiagopog Apr 12, 2020
2f44204
Merge branch 'master' of https://github.com/tiagopog/jsonapi-utils
tiagopog Apr 12, 2020
e26a9ff
Update rake version
tiagopog Apr 12, 2020
4523300
Merge pull request #100 from simplepractice/resources-0.9.9
tiagopog Apr 12, 2020
01be251
Merge branch 'master' into resources-0.9.10
tiagopog Apr 13, 2020
ad4f71b
Merge pull request #101 from grk/resources-0.9.10
tiagopog Apr 13, 2020
3ecf4b0
Merge branch 'master' into update_travis
tiagopog Apr 13, 2020
e5dde19
Merge pull request #104 from brateq/update_travis
tiagopog Apr 13, 2020
6d30f23
Merge pull request #103 from brateq/remove_paginator_docs
tiagopog Apr 13, 2020
df06bd4
Merge pull request #105 from grk/resources-0.9.11
tiagopog Apr 14, 2020
d1939e9
Update bundler and factory_bot
tiagopog Apr 24, 2020
f4b294a
Update README and bump up gem's version
tiagopog Apr 26, 2020
e761c83
Improve version requirements of some development dependencies
tiagopog Apr 26, 2020
f36ea9c
Add support for passing in custom meta information in responses.
seanabrahams Sep 10, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
language: ruby
sudo: false
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

before_install:
- gem install bundler
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ 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:

```ruby
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:
Expand Down Expand Up @@ -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
```

Expand Down
18 changes: 9 additions & 9 deletions jsonapi-utils.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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.11'

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 'sqlite3', '~> 1.3.6'
spec.add_development_dependency 'rspec-rails', '~> 3.1'
spec.add_development_dependency 'factory_girl', '~> 4.8'
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', '>= 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.10.3'
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
42 changes: 29 additions & 13 deletions lib/jsonapi/utils/response/formatters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
#
Expand All @@ -133,13 +133,15 @@ 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 options[:source].present? && related_resource_operation?
source_resource = turn_source_into_resource(options[:source])
if related_resource_operation?(options)
source_resource = turn_source_into_resource(options[:source])
relationship_type = get_source_relationship(options)
JSONAPI::RelatedResourcesOperationResult.new(:ok,

JSONAPI::RelatedResourcesOperationResult.new(
:ok,
source_resource,
relationship_type,
records,
Expand All @@ -152,11 +154,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.
Expand Down Expand Up @@ -263,16 +278,17 @@ 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
data[:meta] = options.fetch(:meta, {})

if include_pagination_links?
data[:pagination_params] = pagination_params(records, options)
end

if JSONAPI.configuration.top_level_meta_include_record_count
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
Expand Down
29 changes: 25 additions & 4 deletions lib/jsonapi/utils/support/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@ 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

# Apply proper pagination to the records.
#
# @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.
Expand All @@ -32,9 +52,10 @@ 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
return {} unless include_pagination_links?
paginator.links_page_params(record_count: record_count_for(records, options))
end

Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/utils/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module JSONAPI
module Utils
VERSION = '0.7.2'.freeze
VERSION = '0.7.3'.freeze
end
end
2 changes: 1 addition & 1 deletion spec/controllers/posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions spec/features/page_count_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ def TestApp.draw_page_count_test_routes
# Feature Tests
##


describe PageCountTestController, type: :controller do
include_context 'JSON API headers'

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
Expand Down
2 changes: 1 addition & 1 deletion spec/features/record_count_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/jsonapi/utils/support/pagination_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
end

before(:all) do
FactoryGirl.create_list(:user, 2)
FactoryBot.create_list(:user, 2)
end

let(:options) { {} }
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/support/controllers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
37 changes: 20 additions & 17 deletions spec/support/factories.rb
Original file line number Diff line number Diff line change
@@ -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}" }
Expand All @@ -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
Expand All @@ -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