fabrik42/acts_as_api

new feature

achempion opened this issue · 4 comments

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 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

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.

@fabrik42 great, thank you!