Replies: 1 comment 2 replies
-
@CiTroNaK Hi, thank you for the suggestion. class FooResourceV1 < BaseResource
attributes :a, :b, :c
end
class FooResourceV2 < BaseResource
attributes :a, :b, :c, :d
end One of the solutions currently available is to use inheritance. class BaseFooResource < BaseResource
attributes :a, :b, :c
end
class FooResourceV1 < BaseFooResource
end
class FooResourceV2 < BaseFooResource
attributes :d
end The problem of this approach is that it's not flexible due to the nature of inheritance. So, what I'm considering to add to Alba is something like below: Alba.register :base_foo_attributes do
attributes :a, :b, :c
end
class FooResourceV1 < BaseResource
base_foo_attributes
end
class FooResourceV2 < BaseResource
base_foo_attributes
attributes :d
end This approach lets us compose multiple "pre-registered" sets of attributes. I believe this is as flexible as the view of Blueprinter. I'm going to implement it if you think it's good enough. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First, I would like to thank you for this gem.
One thing I like about the Blueprinter gem is the
view
feature. I know a feature request of this kind (please can we have X from Y) is not the most popular, so before I jump on creating PR, I would like to know if that would be accepted (if it makes sense for Alba).Why Alba and not Blueprinter?
We are looking for a more performant serializer that is also well-maintained (Blueprinter was forked, but the future is uncertain).
Alba shares some concepts from Blueprinter, and thus it will be easier for us to use it.
The problem
Let me start with a use case where we leverage it a lot: API versioning.
We did a lot of refactoring (to remove duplication mainly) on our backend Rails app. Part of it, we moved from
v1
,v2
, etc., to monthly named API versions for our public API (it does not mean we have one release per month, but it helps with the depreciation strategy).From many controllers that shared code between API versions (or inherited from each other) and many
jbuilder
files, we now have just one controller/action per endpoint, and the proper output (based on the API version) is processed in the serializer.We set the requested API version using this scope in routes:
Example of the solution with
view
sHere is a small example of the solution that we are now using. Imagine we have 4 API versions:
2022-06
,2022-08
,2022-10
, and2022-12
.For
2022-10
, we changed the names of two fields (a typical breaking change) fromaddress_line_1
toline_1
. This is how our serializer would look like:How it would be used for each API version:
2022-06
defines the fields and uses them all2022-08
has no changes in this serializer, so the previous version is used (2022-06
).2022-10
has changes, so it includes the previous version (include_view
) and does the renaming (excludes
the two fields and adds them under a new name).2022-12
has no changes in this serializer, so the previous version is used (2022-10
).We also have support for the classical usage of
view
s. Here is a small example:Our usage
For this work, we only added a small piece of code that finds the correct
view
based on the requested API version (and the definedview
on render/or in a blueprint association) on the serializer.This works for our use case. We know it is only a temporary solution we are evaluating (another way is using a class-based solution, not a
view
based).We also know that if we want to keep it, we need to improve it (see below for scopes). We also have a deprecation strategy for API versions and do not expect a long list of versions in each serializer, where it could become unusable/hard to maintain.
Main benefits:
view
to serializers that have breaking changes and allowing the new version inPublicApi::VersionConstraint
.2022-06
, they will be available to all newer versions).A small upgrade to the solution
Another concept that would be useful for our usage: scopes.
Here is something (based on the Blueprinter DSL) that I would like to see as a final "version":
What do you think about it? Does it make sense for Alba to support something like that?
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions