new feature
achempion opened this issue · 4 comments
achempion commented
Lets add new config option that can delete any field from api response if field value is nil or false.
I suggest that it will be very useful.
fabrik42 commented
What about the :if
and :unless
options?
achempion commented
@fabrik42 need use it for each option but I write about all options at once.
I think that add something like allow_null: false
to api_accessible options must be very userful
fabrik42 commented
Hmm - I'm not sure about this. This basically makes your response schema unpredictable.
You can make it work yourself with something like this (not tested)
module ActsAsApi
class ApiTemplate < Hash
def add_if_present(val, options = {})
options[:if] ||= lambda { |model| model.try(val).present? }
add(val, options)
end
end
end
So you can use t.add_if_present
in your Template definition instead of t.add
.