rails-api / active_model_serializers

ActiveModel::Serializer implementation and Rails hooks
MIT License
5.33k stars 1.39k forks source link

filter method not triggered #516

Closed michaelirey closed 10 years ago

michaelirey commented 10 years ago

In my serializer

class ProductSerializer < ActiveModel::Serializer
  attributes :id, :name, :description
  def filter(keys)
    keys - [:name]
  end
end

However, name attribute stays in.

I have tried this:

class ProductSerializer < ActiveModel::Serializer
  attributes :id, :name, :description
  def filter(keys)
    binding.pry
  end
end

The pry is not invoked.

zhaohanweng commented 10 years ago

I have the same issue #513 with filtering, you can try

def include_name?; fasle end

My problem is the filter is overridden if the filtered key is used in the embed association.

michaelirey commented 10 years ago

Thanks for the tip, and glad to hear others are experiencing a similar issue.

I tried what you suggested, still does not get invoked.

michaelirey commented 10 years ago

@zhaohanweng, your suggestion does work. Still have broken filter though.

ericpeters0n commented 10 years ago

+1

shabuta commented 10 years ago

filter not triggered for me on active_model_serializers (0.8.1) either. Ended up overriding attributes method instead (like in this rails cast: http://railscasts.com/episodes/409-active-model-serializers)

def attributes data = super data.delete(:name) data end

IgorDobryn commented 10 years ago

Doesn't work for me too. active_model_serializers 0.8.1.

r4m commented 10 years ago

The problem appears with active_model_serializers prior to version 0.9.0. I think that versions < 0.9.0 do not support the filter function, at least considering that this method is not mentioned in the stable documentation

lucascaton commented 10 years ago

+1

raeno commented 10 years ago

@r4m, you're right, according to their changelog ( https://github.com/rails-api/active_model_serializers/blob/master/CHANGELOG.md ) filter method was added in 0.9.0 pre1 release.

Emerson commented 10 years ago

How stable is 0.9? Really want the filter method but a little nervous about upgrading given all the API changes.

localredhead commented 10 years ago

+1

ChadEubanks commented 10 years ago

+1

pelle commented 10 years ago

+1

mehulkar commented 10 years ago

+1 here as well. Looks like 0.9 is in alpha, so I'm using serializable_hash like this right now:

def serializable_hash
  all = super
  attrs_to_serialize = some_method
  all.slice(*attrs_to_serialize)
end

(some_method returns a set of attributes)

steveklabnik commented 10 years ago

0.9.0 is coming out later today, and yes, it's not in 0.8.

linesarefuzzy commented 9 years ago

Why is the filter feature not in the master Readme? It is described in branch 0-9-stable, but it works in my project, which has simply gem 'active_model_serializers' in the Gemfile (and 0.9.3 listed in Gemfile.lock). It took me way too long to find out about it. Many people are still posting super complicated solutions like using 3 separate serializers for one model.

marknadig commented 9 years ago

As one how is/was searching... surprised not in README... guess the 10.x overshadowed it.

joaomdmoura commented 9 years ago

Hi @linesarefuzzy and @marknadig, indeed our efforts are deeply focused on 0.10.x, but we still want to help ppl who use older versions!

First of all, we strongly recommend you to check 0.10.x version and give it a try, we already have a RC2 and you can also point it to master if you like to live on the cutting-edge.

I've just checked the 0.9-stable branch and it seems the filter features is mentioned there:

Also, as with attributes, serializers will execute a filter method to determine which associations should be included in the output. For example:

class PostSerializer < ActiveModel::Serializer
  attributes :id, :title, :body
  has_many :comments

  def filter(keys)
    keys.delete :comments if object.comments_disabled?
    keys
  end
end

Or ...

class PostSerializer < ActiveModel::Serializer
  attributes :id, :title, :body
  has_one :author
  has_many :comments

  def filter(keys)
    keys.delete :author unless scope.admin?
    keys.delete :comments if object.comments_disabled?
    keys
  end
end
linesarefuzzy commented 9 years ago

@joaomdmoura Yes, it's described in 0.9-stable but not in master. However, it works in whatever version is used when the Gemfile says gem 'active_model_serializers', which I assume is master or previous to master. Shouldn't it then also be in the master readme?