Sha256: 37ca965b1a9df841eb6a942d6b862bb97c75c7f56e013a1fa40f336ed4ab3c3d

Contents?: true

Size: 1.04 KB

Versions: 24

Compression:

Stored size: 1.04 KB

Contents

require 'test_helper'

module PolymorphicExtender
  def self.extended(model)
    model.extend(representer_name_for(model))
  end
  
  def self.representer_name_for(model)
    PolymorphicTest.const_get("#{model.class.to_s.split("::").last}Representer")
  end
end


class PolymorphicTest < MiniTest::Spec
  class PopSong < Song
  end
  
  module SongRepresenter
    include Representable::JSON
    property :name
  end
  
  module PopSongRepresenter
    include Representable::JSON
    property :name, :from => "known_as"
  end
  
  class Album
    attr_accessor :songs
  end
  
  module AlbumRepresenter
    include Representable::JSON
    collection :songs, :extend => PolymorphicExtender
  end
  
  
  describe "PolymorphicExtender" do
    it "extends each model with the correct representer in #to_json" do
      album = Album.new
      album.songs = [PopSong.new("Let Me Down"), Song.new("The 4 Horsemen")]
      assert_equal "{\"songs\":[{\"known_as\":\"Let Me Down\"},{\"name\":\"The 4 Horsemen\"}]}", album.extend(AlbumRepresenter).to_json
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
representable-1.3.5 test/polymorphic_test.rb
representable-1.3.4 test/polymorphic_test.rb
representable-1.3.3 test/polymorphic_test.rb
representable-1.3.2 test/polymorphic_test.rb
representable-1.3.1 test/polymorphic_test.rb
representable-1.3.0 test/polymorphic_test.rb
representable-1.2.9 test/polymorphic_test.rb
representable-1.2.8 test/polymorphic_test.rb
representable-1.2.7 test/polymorphic_test.rb
representable-1.2.6 test/polymorphic_test.rb
representable-1.2.5 test/polymorphic_test.rb
representable-1.2.4 test/polymorphic_test.rb
representable-1.2.3 test/polymorphic_test.rb
representable-1.2.2 test/polymorphic_test.rb
representable-1.2.1 test/polymorphic_test.rb
representable-1.2.0 test/polymorphic_test.rb
representable-1.1.7 test/polymorphic_test.rb
representable-1.1.6 test/polymorphic_test.rb
representable-1.1.5 test/polymorphic_test.rb
representable-1.1.4 test/polymorphic_test.rb