0.9 no longer automatically removes ? from boolean methods
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}}"
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?
I'd be willing to accept a PR for this, I agree that the previous behaviour was much nicer.
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?
Could we get this behavior ported to master please?
for a work around for now, you could do:
attributes :has_a_bar
def has_a_bar
object.has_a_bar?
end
Yeah, but sort of a pain to rewrite 20 serializers with around 10 of these methods each after upgrading.
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: I disagree, no way to use 'em sensibly from js-side of things (object.is_visible vs object['is_visible?'])
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 :)
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?
? 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, that's a good reason / argument -- I could get on board with that. I'm curious what other @rails-api/ams members think.
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.
I can submit a failing test real quick, one moment