Fix badge serialization issue.

This commit is contained in:
Vikhyat Korrapati 2014-04-11 07:33:17 +05:30
parent 3f4c5ed451
commit 89b9f9e2cb
2 changed files with 59 additions and 8 deletions

View File

@ -135,16 +135,8 @@ class UserSerializer < BasicUserSerializer
object.user_badges.count
end
def include_badge_count?
SiteSetting.enable_badges?
end
def featured_user_badges
# The three rarest badges this user has received should be featured.
object.user_badges.joins(:badge).order('badges.grant_count ASC').includes(:granted_by, badge: :badge_type).limit(3)
end
def include_featured_user_badges?
SiteSetting.enable_badges?
end
end

View File

@ -0,0 +1,59 @@
# Just ignore included associations that are to be embedded in the root instead of
# throwing an exception in AMS 0.8.x.
#
# The 0.9.0 branch does exactly this, see:
# https://github.com/rails-api/active_model_serializers/issues/377
module ActiveModel
class Serializer
# This method is copied over verbatim from the AMS version, except for silently
# ignoring associations that cannot be embedded without a root instead of
# raising an exception.
def include!(name, options={})
unique_values =
if hash = options[:hash]
if @options[:hash] == hash
@options[:unique_values] ||= {}
else
{}
end
else
hash = @options[:hash]
@options[:unique_values] ||= {}
end
node = options[:node] ||= @node
value = options[:value]
if options[:include] == nil
if @options.key?(:include)
options[:include] = @options[:include].include?(name)
elsif @options.include?(:exclude)
options[:include] = !@options[:exclude].include?(name)
end
end
association_class =
if klass = _associations[name]
klass
elsif value.respond_to?(:to_ary)
Associations::HasMany
else
Associations::HasOne
end
association = association_class.new(name, self, options)
if association.embed_ids?
node[association.key] = association.serialize_ids
if association.embed_in_root? && hash.nil?
# Don't raise an error!
elsif association.embed_in_root? && association.embeddable?
merge_association hash, association.root, association.serializables, unique_values
end
elsif association.embed_objects?
node[association.key] = association.serialize
end
end
end
end