active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

0.9 no longer automatically removes ? from boolean methods

Open dangalipo opened this issue 12 years ago • 14 comments

So this is more of an issue when using AMS in a class not backed by active model.

In 0.8 you could specify a boolean method and AMS would strip the ? in the JSON output how ever it no longer does this.

Example:

class Foo
  include ActiveModel::Serialization

  def has_a_bar?
    true
  end

  def active_model_serializer
    FooSerializer
  end
end

class FooSerializer < ActiveModel::Serializer
  attributes :has_a_bar?
end

#0.8
FooSerializer.new(Foo.new).to_json # "{\"foo\":{\"has_a_bar\":true}}"
#0.9
FooSerializer.new(Foo.new).to_json #"{\"foo\":{\"has_a_bar?\":true}}"

dangalipo avatar Aug 25 '14 00:08 dangalipo

yup, same problem here. additionally ability to alias attributes has been removed as well so attributes :section? => :section is no longer possible :(

Could we have it back please?

Sija avatar Aug 25 '14 11:08 Sija

I'd be willing to accept a PR for this, I agree that the previous behaviour was much nicer.

steveklabnik avatar Aug 25 '14 16:08 steveklabnik

Yep, used to be gsub'd out https://github.com/rails-api/active_model_serializers/blob/0-8-stable/lib/active_model/serializer.rb#L93

I suppose a similar approach right here would get things patched up.

0.8 spec: https://github.com/rails-api/active_model_serializers/blob/0-8-stable/test/serializer_test.rb#L978

Anyone already working on this?

nickhammond avatar Sep 10 '14 20:09 nickhammond

Could we get this behavior ported to master please?

pedromtavares avatar Mar 08 '16 13:03 pedromtavares

for a work around for now, you could do:

  attributes :has_a_bar
  def has_a_bar
    object.has_a_bar?
  end

NullVoxPopuli avatar Mar 08 '16 13:03 NullVoxPopuli

Yeah, but sort of a pain to rewrite 20 serializers with around 10 of these methods each after upgrading.

pedromtavares avatar Mar 08 '16 13:03 pedromtavares

I know it's a pain, but I think including the ? makes more sense, as far as getting what you specify.

@rails-api/ams ?

NullVoxPopuli avatar Mar 08 '16 13:03 NullVoxPopuli

@NullVoxPopuli: I disagree, no way to use 'em sensibly from js-side of things (object.is_visible vs object['is_visible?'])

Sija avatar Mar 08 '16 14:03 Sija

Yeah it doesn't make sense since '?' isn't used in Javascript. I'm trying to figure out a monkey patch in master for this (my entire Ember app is broken ATM), first time reading AMS code :)

pedromtavares avatar Mar 08 '16 14:03 pedromtavares

what if someone, for some reason wanted a ?, by omitting it by default, how would one specify to keep it?

say, maybe another ruby app is consuming the api?

NullVoxPopuli avatar Mar 08 '16 14:03 NullVoxPopuli

? is IMO as much part of ruby's syntactic sugar as it's attribute name so it's more about compatibility with every other client-side language than with ruby <-> ruby API

Sija avatar Mar 08 '16 14:03 Sija

@Sija, that's a good reason / argument -- I could get on board with that. I'm curious what other @rails-api/ams members think.

NullVoxPopuli avatar Mar 08 '16 14:03 NullVoxPopuli

Just reading in email, but would be best to strip '?' From json key. If previous behavior, def need to fix. Failing test?

B mobile phone

On Mar 8, 2016, at 8:45 AM, L. Preston Sego III [email protected] wrote:

@Sija, that's a good reason / argument -- I could get on board with that. I'm curious what other @rails-api/ams members think.

— Reply to this email directly or view it on GitHub.

bf4 avatar Mar 08 '16 14:03 bf4

I can submit a failing test real quick, one moment

NullVoxPopuli avatar Mar 08 '16 14:03 NullVoxPopuli