Skip to content

Conversation

alecho
Copy link

@alecho alecho commented Mar 1, 2021

No description provided.

ekampp and others added 30 commits July 9, 2020 11:09
* Add total records to pagination

* Spec for total records
Allow builds sans git.
`#count` will always execute an SQL COUNT query whereas `#size` will
check if the records are already loaded (and then call `#length`). It
only executes a count when necessary.

See https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-size
* Remove hard coded maximum per-page limit of 30.

* Don't include total in pagination links.

* Wrap default per_page in a method.

* Update readme with jsonapi_page_size example.

* Skip :records in pagination with next.
Starting with Rails 6.1 ActiveModel::Error is an actual class:

> Active Model's errors are now objects with an interface that allows
> your application to more easily handle and interact with errors thrown
> by models. The feature[1] includes a query interface, enables more
> precise testing, and access to error details.
>
> [1] rails/rails#32313

As a result of this `resource.details` looks slightly different than in
previous versions and notably doesn't contain `:message` anymore, if
`object.errors.add(:something, message: 'some error')` was used to
specify a validation error.
https://github.com/rails/rails/blob/2a7ff0a5f54979b14b19f827c99295297dda411d/activemodel/lib/active_model/error.rb#L149

From the test suite:
```
note.errors.add(:title, message: 'has typos') if note.errors.key?(:title)
```

Rails 6.0:
```
{
  :title=>[{:error=>:invalid, :value=>"BAD_TITLE"},
           {:error=>{:message=>"has typos"}}],
  :quantity=>[{:error=>:less_than, :value=>109, :count=>100}]
}
```

Rails 6.1:
```
{
  :title=>[{:error=>:invalid, :value=>"BAD_TITLE"},
           {:error=>:invalid}],
  :quantity=>[{:error=>:less_than, :value=>100, :count=>100}]
}
```

The patch addresses this change by merging the error.message into its detail.
Due to the change in b019536 the validation error message for a required
field changes from "$x can't be blank" to "$x must exist".
* Add Ruby 3.0 to test matrix

* Exclude Ruby 3 + Rails 5

Rails 5 doesn't support Ruby 3
)

* always call jsonapi_page_size method on pagination enabled requests

pass the per_page_param to the jsonapi_page_size method

* pass pagination to jsonapi_page_size and handle string-to-int conversion in jsonapi_page_size

* update readme to reflect changes

* fix woring
Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 3.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
mylescc and others added 14 commits December 5, 2022 12:23
- When resources are empty, but it's an array, we should be removing
  empty links and meta fields if they are nil. This matches the
  behaviour in fast_jsonapi: https://github.com/jsonapi-serializer/jsonapi-serializer/blob/master/lib/fast_jsonapi/object_serializer.rb#L71-L72
- Currently, links will be nil if there is 1+ resource, but present when
  there are none
Thought it would be helpful to add an example similar to how it's done in other sections
Ransack 4 requires explicitly allowlisting ransackable attributes and
associations for each model. This causes the specs to fail with the most
recent Ransack version. This fixes those spec failures.
* Rubocop fix: Use filter_map instead of map { ... }.compact

Currently, builds are failing because of Rubocop complaining about these
three lines.

* Drop support for Ruby 2.6

Array#filter_map is only introduced in Ruby 2.7. This ensures we're
still supporting the 2.x series, but drops support for the now pretty
old Ruby 2.6.
* Bump actions/checkout from 3 to 4

Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@v3...v4)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

* Cleanup rubies/rails.

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Stas SUȘCOV <[email protected]>
Test Ruby 3.3 and Rails 6.1 instead
mamhoff and others added 5 commits June 23, 2024 13:10
* CI Matrix: Drop Rails 6, Ruby 2.7

Test Ruby 3.3 and Rails 6.1 instead

* Refactor error spec

The previous specs were harder to read, and they relied on the ordering
of the errors, which is unnecessary.

* Add spec for non-interpolated error messages

* Simplify ActiveModel::ErrorSerializer

We're only running CI for Rails 6.1+ now, and we can now rely on a
stable API for ActiveModel::Errors.
* Rubocop fix: Use filter_map instead of map { ... }.compact

Currently, builds are failing because of Rubocop complaining about these
three lines.

* Fix specs with Ransack 4.0

Ransack 4 requires explicitly allowlisting ransackable attributes and
associations for each model. This causes the specs to fail with the most
recent Ransack version. This fixes those spec failures.

* Allow decorating objects after pagination

This simplification allows decorating objects after they are paginated,
without losing the correct total object count.

I'm using an instance variable on the including controller here, because
the decorating the paginated collection will have us lose the instance
variable we set on it.

Here's the case where this happens: We have a complex ActiveRecord
collection that we run through Ransack and Kaminari, but before
rendering we want to convert each object in it using a
`SimpleDelegator`.

Here's a simplified version of the controller action we're looking at:

```
  class UserDecorator < SimpleDelegator
    def fantastic_for_rendering
      "Whoah"
    end
  end

  def index
    allowed_fields = [
      :first_name, :last_name, :created_at,
      :notes_created_at, :notes_quantity
    ]
    options = { sort_with_expressions: true }

    jsonapi_filter(User.all, allowed_fields, options) do |filtered|
      result = filtered.result
      jsonapi_paginate(result) do |paginated|
        paginated = paginated.map { |user| UserDecorator.new() }
        render jsonapi: paginated
      end
    end
  end
```

---------

Co-authored-by: Stas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.