Comments (2)
@ManuelCostanzo It's not possible to exclude ad hoc attributes. However, you can do so by normal params
.
class UserResource
include Alba::Resource
root_key :user
attributes :id, if: proc { !params[:exclude]&.include?(:id) }
attributes :name, :email
end
User = Struct.new(:id, :name, :email)
user = User.new(1, 'John Doe', '[email protected]')
UserResource.new(user, params: {exclude: [:id]}).serialize # => '{"user":{"name":"John Doe","email":"[email protected]"}}'
The reason why Alba doesn't support ad hoc exclude is that we want to make it easy to predict output by just looking at the resource class. In this example, although we need to add explicit if
option for exclude logic, it's easy to know in some places id
will be actually excluded. In contrast, if we provide exclude
option, while we don't have to add if
option, it's more difficult to know in some cases we exclude id
.
from alba.
Thank you very much. That make sense!
from alba.
Related Issues (20)
- key_transform in super class doesn't apply to nested resource HOT 2
- Hash keys to string in 2.0 breaking change HOT 7
- How do we deal with a `nil` object HOT 12
- [ask] How to configure nested associations ? HOT 6
- Configure how date objects are serialized HOT 17
- [Oj] Options from Oj are ignored HOT 8
- I'm unsure if root_key is not working as intended or I'm misusing it HOT 9
- Can't call class method within `has_many` HOT 5
- FrozenError: can't modify frozen String HOT 11
- Why is resource property required for associations? HOT 7
- Support for heterogenous lists? HOT 4
- Decimals are being shown as a string HOT 14
- v3 is breaking existing behavior HOT 6
- v3 is not working for boolean field HOT 3
- Dynamic params HOT 2
- You passed "location" options but ignored. HOT 5
- Rails compatibility `as_json` HOT 3
- `within` kwarg is ignored for nested associations HOT 3
- Custom attribute DSL does not work for inline relation HOT 12
- Rails controller integration
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alba.