Sha256: 19172fd444d3f4b4e87b294b2f9276fb44ab165136d32f9fe29a808265e3b96b

Contents?: true

Size: 983 Bytes

Versions: 2

Compression:

Stored size: 983 Bytes

Contents

require 'spec_helper'

describe RestPack::Serializer::Attributes do
  class CustomSerializer
    include RestPack::Serializer
    attributes :a, :b, :c
    attribute :old_attribute, :key => :new_key
    transform [:gonzaga], lambda { |name, model| model.send(name).downcase }
  end

  subject(:attributes) { CustomSerializer.serializable_attributes }

  it "correctly models specified attributes" do
    expect(attributes.length).to be(5)
  end

  it "correctly maps normal attributes" do
    [:a, :b, :c].each do |attr|
      expect(attributes[attr]).to eq(attr)
    end
  end

  it "correctly maps attribute with :key options" do
    expect(attributes[:new_key]).to eq(:old_attribute)
  end

  describe '#transform_attributes' do
    let(:model) { OpenStruct.new(gonzaga: 'IS A SCHOOL') }

    subject(:as_json) { CustomSerializer.as_json(model) }

    it 'uses the transform method on the model attribute' do
      expect(as_json[:gonzaga]).to eq('is a school')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restpack_serializer-0.5.7 spec/serializable/attributes_spec.rb
restpack_serializer-0.5.6 spec/serializable/attributes_spec.rb