118 Commits

Author SHA1 Message Date
Kevin Pheasey
7e2289006c chore(version): release 1.7.2 2020-05-18 11:21:44 -04:00
Kevin Pheasey
1d2eab2510
fix(Relationship): do not set static_record_type for polymorphic relationships (#82) 2020-05-18 10:09:58 -04:00
Kevin Pheasey
b64a0ceeed
fix(ObjectSerializer): pass to_json arguments (#80)
* fix(ObjectSerializer): pass to_json arguments

to_json takes arguments that serialized_json did not define.  This breaks a lot of things.

* Update CHANGELOG.md with latest fix and release
2020-05-01 09:02:03 -04:00
Stas SUȘCOV
c533634293 Rewrite tests. 2020-04-29 15:30:44 +01:00
Stas SUȘCOV
12e2987420 Rubocop cleanups. 2020-04-29 15:30:44 +01:00
Stas SUȘCOV
297551c551 Version bump. Updated the changelog. 2020-04-29 14:35:11 +01:00
Mark Siemers
0edebd99e6 Check #each instead of #size for is_collection?
See if a resource responds to 'each' instead of 'size' as
things other than collections can respond to 'size' (e.g. File instance)
2020-04-11 12:48:00 +01:00
Markus
990c0acd18
Fix attribute API with lambda (#67)
* add failing spec when attribute is called with lambda
* allow to pass a lambda to `attribute`
2020-02-27 17:49:02 +00:00
Markus
843d943e07
Refactor caching support (#52)
- Very explicit where caching is stored
- No `Rails.cache` automagic with possible colliding keys
- Rails 5.2+ cache versioning support
- Implicit namespace support (by cache instance, e.g.
  ActiveSupport::Cache::MemoryStore.new(namespace: 'jast_jsonapi')
- All cache instance options are supported
2020-02-25 16:49:02 +00:00
Attila Horvath
3faca2d1e0 Fix conditional procedures with lambdas 2020-02-21 14:36:50 +00:00
Attila Horvath
08a20d0ebb Fix relationships with &:proc shorthands 2020-02-20 13:44:18 +00:00
Stas SUȘCOV
1b3d73cbf3
Remove multi-to-json. Add a deprecation warning. 2020-02-15 15:05:06 +00:00
Christopher Sansone
6d01bec146
Improved relationship serializer options (#32)
* allow the relationship's serializer key to be a Proc

* fixes

* specifically test the relationship name and the resource type

* support object blocks without a serializer defined

* stop validation gracefully if the relationship is polymorphic

* improve performance by using instance variables instead of accessor methods

* force initialization up front to avoid the iterative calls

* serializer procs should act like polymorphic when determining the id_method_name

* specs for serializer procs

* updated with more details and examples for relationship serializer options

* adjust specs to define the serializers

* avoid extra method calls for performance

* name change

* one less function call for better performance

* do not require lazy loaded relationships to resolve the serializer

* give polymorphic precedence for backwards compatibility

* move serializer inference into ObjectSerializer to allow for overriding

* move method for better diff

* fix race condition in multi-threaded environments
2020-02-15 14:57:44 +00:00
Aubrey Holland
1d7c18f5da Support for polymorphic id_method_name (#17)
* fix id method bugs, add specs
* Use SecureRandom.uuid
2019-12-03 18:04:04 +00:00
Kevin Pheasey
1cd3f97485 1.6.0 2019-11-04 17:31:35 -05:00
Brad Grzesiak
ddc261d8dc Allow "if: Proc..." on link (#15) 2019-10-25 09:32:56 -04:00
Henning Vogt
587fb2c5fe Add params to set_id block (#16)
* Add params to set_id block arguments

Pull request #331 added a block to the ObjectSerializer.set_id class
method, which allows passing a block to the set_id method. Currently
this block takes only one argument `record`:

```
set_id do |record|
  "#{record.name.downcase}-#{record.id}"
end
```

This PR adds another argument `params` to the block:

```
set id do |record, params|
  params[:admin] ?  record.id : "#{record.name.downcase}-#{record.id}"
end
```

This customization can be useful in situation where we serve different
clients that may need different IDs. One nice side effect is also that
the `set_id` method has the same method signature as the `attribute`
method.

* Update the README
2019-10-15 14:45:46 -04:00
David Pickart
8e2383128e Include data key when lazy-loaded relationships are specified with includes (#10) 2019-10-08 08:30:21 -04:00
Krzysztof Rybka
f2a1934b76 Use each_with_object instead of Hash[map] 2019-10-04 19:44:17 -04:00
Krzysztof Rybka
44a896dda5 Take items of original array instead of dup and delete 2019-10-04 19:41:22 -04:00
Krzysztof Rybka
e4c65a2567 Move transforms mapping to constant 2019-10-04 12:50:22 -04:00
Krzysztof Rybka
fd17386b51 Map split include_item in-place 2019-10-04 12:50:22 -04:00
Jo Potts
83e99b2923 Allow relationship links to be declared as object method (#2)
* Allow relationship links to be declared as object method

* Relationship links note added to readme
2019-10-04 12:50:22 -04:00
Krzysztof Rybka
f04abfd2fe Compute remaining_items once 2019-10-04 12:50:22 -04:00
Daniel Duke
267b706366 validate all include items instead of just the first 2019-10-04 12:50:22 -04:00
Daniel Illi
8d8e5c3059 Fix error on defining anonymous serializer class, fixes #353 2019-10-04 12:50:22 -04:00
Matt Eddy
21ae4aaa0a Allow fieldsets to specify no attributes/relationships 2019-10-04 12:50:22 -04:00
Danil Pismenny
1a407c0030 [#365] Support frozen array in option 2019-10-04 12:50:22 -04:00
Shishir Kakaraddi
ee76e0c69b bump up version to 1.5 2018-11-03 12:11:56 -07:00
Shishir Kakaraddi
cc7f88843a
Merge pull request #331 from larissa/set-id-block
Allow block for id customization
2018-11-03 11:37:56 -07:00
Shishir Kakaraddi
326f9784ca
Merge pull request #294 from nikz/add-links-option-to-relationship-serializer
Adds a :links option to the relationship macros
2018-11-03 11:35:39 -07:00
Shishir Kakaraddi
9d19f32c78
Merge pull request #334 from hmcfletch/fix/as-core_ext-time
require AS core extension for time
2018-11-03 11:30:54 -07:00
Maros Hluska
d5ea95370f Fix params not being hash by default 2018-10-18 22:59:33 +07:00
Shishir Kakaraddi
935fc05beb
Merge pull request #329 from zinosama/improve-readme-with-id_method_name
Improve documentation with id_method_name
2018-10-14 08:31:01 -07:00
Larissa Reis
9fa26fa588 Allow block for id customization
Allow an ID of object to be customized directly on the serializer by
passing a block to `set_id` as opposed to only through a model property.

We already allow for attributes that do not have a model property of the
same name to be customized directly on the serializer using a block.

This customization can be useful in situation in which you have
different classes being serialized using the same serializer. For
example, if we have `HorrorMovie`, `ComedyMovie` and `DramaMovie` using
the same `MovieSerializer`, we can unify their IDs using

```
class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attributes :name, :year
  set_id do |record|
    "#{record.name.downcase}-#{record.id}"
  end
```

which is preferable to creating a `#serialized_id` method in every model
that will use `MovieSerializer` to encapsulate the customization.

Closes #315
2018-10-10 22:26:57 -06:00
Les Fletcher
05ad93084b
require 'active_support/core_ext/numeric/time’
`5.minutes` was failing in the performance spec
2018-10-09 14:53:57 -07:00
nikz
1ad20d6b7b Merge branch 'upstream-dev' into add-links-option-to-relationship-serializer 2018-10-07 21:29:43 +01:00
nikz
85b41c45d4 Adds :lazy_load_data option
If you include a default empty `data` option in your JSON API response,
many frontend frameworks will ignore your `related` link that could be
used to load relationship records, and will instead treat the
relationship as empty.

This adds a `lazy_load_data` option which will:

  * stop the serializer attempting to load the data and;
  * exclude the `data` key from the final response

This allows you to lazy load a JSON API relationship.
2018-10-07 21:23:36 +01:00
Austin Matzko
be701f3e06 Don't attempt to dup a nil 2018-10-03 17:59:58 -04:00
Austin Matzko
1ab5cd387a Don't share data_links among inherited serializers. 2018-10-03 17:51:37 -04:00
nikz
1efdd3372d Fixes dangling comma and unused param 2018-10-02 22:09:15 +01:00
Shishir Kakaraddi
11b5255010
Merge pull request #313 from sakuraineed/fix_set_key_transform
Fix set_key_transform's set_type to give priority to pre-set value
2018-09-19 21:56:59 -07:00
Shishir Kakaraddi
90e0feef3c Merge branch 'release-1.4' of https://github.com/Netflix/fast_jsonapi into release-1.4 2018-09-19 21:33:01 -07:00
Shishir Kakaraddi
b674909830 bump up version to 1.4 2018-09-19 21:32:56 -07:00
Shishir Kakaraddi
fced516356 transforms type for polymorphic relationships too 2018-09-19 20:24:40 -07:00
Kenji Sakurai
6dc34cd4d4 Fix set_key_transform's set_type to give priority to pre-set value 2018-09-08 23:48:44 +09:00
Matthew Lanigan
5a70b1a686 Do not use ActiveSupport core extensions
Core extensions do not play well with many other gems; especially
considering that they only seem to be included for one `to_json` call,
they should be avoided.
2018-09-03 12:01:45 -07:00
François Pradel
955f4f234d Add support for polymorphic includes 2018-08-31 19:17:45 -07:00
François Pradel
3973b312a7 Demodulize relationship record class name 2018-08-31 19:17:45 -07:00
Alessandro Dal Grande
42d9203796 Fix confusing error when using include and no relationship was set
If you forgot to set any `has_many` in the serializer and tried to serialize with an `include` you would get:
```
NoMethodError (undefined method `[]' for nil:NilClass):
```

That is not very helpful. Setting the variable with a default case makes sure the right error message gets displayed.
2018-08-22 20:08:04 -07:00