-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow type to be a model #597
Comments
I like it. We do the same, just a bit more explicitly with an |
I think the DSL should allow something more evolved, maybe evolves :user_id, into: User, with: ActiveRecordEvolverManagerTransformer |
This is definitely a common pattern and probably worth abstracting. It seems like we'd actually want to go the other way to really be describing what's happening. Something like:
I've had a convention for a long time of creating a class-level # if a symbol is passed to load, it does options[:type].send(options[:load], params[:source])
require :user, type: User, source: :user_id, load: :find_by_id
# if a lambda is passed, it passes the param to the lambda
# note that this pattern would look for anything that responds to `.call` so
# could be a class, whatever
requires :user, type: User, source: :user_id, load: ->(param){ User.find_by_id(param) } Something more like that maybe? |
👍 |
@mbleigh Very nice. It makes total sense to require the loaded object vs. the source parameter. We might want to rename require :user, type: User, param: :user_id, with: :find_by_id Furthermore, this should support a group of params or multiple params. In general the inside of that load block should have access to all params, and possibly to the entire API endpoint, I think. |
This could also just be a block: requires :user do
User.find(params[:user_id]
end |
Note that if this is implemented it might conflict with #829, so we need to find a DSL that works for both. |
#1039 mostly implemented this, provided you don't mind adding a class User < ActiveRecord::Base
# ...
def self.parse(param)
find(param.to_i)
end
end
# ...
params do
requires :user_id, type: User
end
get do
params[:user_id] # is a User already
end However, it would be nice to alias |
I don't have an idea for the best syntax but I find myself loading models from the params hash a lot. What if you were able to do something like this?
Thoughts?
The text was updated successfully, but these errors were encountered: