Hi Jeremy,
What would you suggest is the best method to only select a set of
predefined "*public*" columns for API purposes? Currently, I have a
PUBLIC_COLUMNS constant defined inside the User model and am doing the
following for GET/POST request for */api/v1/users *endpoint. Is there some
sort of a scope I can define inside the model that would work for both
multiple and single resource requests?
class User < Sequel::Model
PUBLIC_COLUMNS = %i{id name email phone timezone created_at updated_at}
end
class App
hash_branch(:api_v1, 'users') do |r|
r.is do
r.get do
@users = User.select(User::PUBLIC_COLUMNS)
{ status: 200, data: @users }
end
r.post do
@user = User.new(user_params)
if @user.valid?
@user.save; @user.columns.delete_if { |k|
!User::PUBLIC_COLUMNS.include?(k) }
{ status: 200, data: @user }
else
{ status: 422, data: @user.errors }
end
end
end
end
def user_params
typecast_params.convert!(symbolize: true) do |tp|
tp.str([:name, :email, :phone, :timezone])
end.delete_if { |k, v| v.nil? }
end
end
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/776bbed7-9229-4e37-9797-80de18d76a57%40googlegroups.com.