Added custom versions option to the mod serializer

master
Zequez 2015-08-01 06:50:06 -03:00
parent 985a304308
commit d273be15fd
2 changed files with 22 additions and 0 deletions

View File

@ -7,6 +7,9 @@ class ModSerializer < ActiveModel::Serializer
attribute :official_url, key: :homepage
has_many :versions, key: :releases
def versions
@options[:versions] || object.versions
end
def categories
object.categories.map(&:slug)

View File

@ -11,6 +11,10 @@ describe ModSerializer do
official_url: 'http://castleblack.com',
summary: 'This mod adds the ability to farm potatoes on Factorio.',
categories: [create(:category, name: 'Potato'), create(:category, name: 'Apple')]
create :mod_version, mod: @mod
create :mod_version, mod: @mod
create :mod_version, mod: @mod
end
it 'should return the public API structure' do
@ -26,6 +30,21 @@ describe ModSerializer do
categories: ['potato', 'apple'],
releases: @mod.versions.map{|mv| ModVersionSerializer.new mv}.as_json
})
end
it 'should accept an option to serialize just certain versions' do
specific_versions = [@mod.versions[0], @mod.versions[1]]
expect(ModSerializer.new(@mod, versions: specific_versions).as_json).to eq({
id: @mod.id,
title: 'Potato Galaxy',
name: 'potato-galaxy-mod',
url: 'http://localhost:3000/mods/potato-galaxy',
description: 'This mod adds the ability to farm potatoes on Factorio.',
homepage: 'http://castleblack.com',
contact: 'Send a homing pigeon to Castle Black',
authors: ['John Snow Zombie', 'THAT Guy'],
categories: ['potato', 'apple'],
releases: specific_versions.map{|mv| ModVersionSerializer.new mv}.as_json
})
end
end