Sha256: 2cbff4678a4df2f0ff2e0a26628fd4ce38893f09ee5ddab0c468546e6df72e7c

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

require 'rails_helper'

describe LHS::Record do

  let(:listing) { location.listings.first }

  before(:each) do
    stub_request(:get, 'http://uberall/locations/1')
      .to_return(body: {
        listings: [{
          directory: { name: 'Instagram' }
        }]
      }.to_json)
  end

  context 'has_many' do

    before(:each) do
      class Location < LHS::Record
        endpoint 'http://uberall/locations'
        endpoint 'http://uberall/locations/{id}'
        has_many :listings
      end

      class Listing < LHS::Record

        def supported?
          true
        end
      end
    end

    let(:location) { Location.find(1) }

    it 'casts the relation into the correct type' do
      expect(listing).to be_kind_of(Listing)
      expect(listing.supported?).to eq true
    end

    it 'keeps hirachy when casting it to another class on access' do
      expect(listing._root._raw).to eq location._raw
      expect(listing.parent.parent._raw).to eq location._raw
    end
  end

  context 'custom class_name' do

    before(:each) do
      module Uberall
        class Location < LHS::Record
          endpoint 'http://uberall/locations'
          endpoint 'http://uberall/locations/{id}'
          has_many :listings, class_name: 'Uberall::Listing'
        end
      end

      module Uberall
        class Listing < LHS::Record

          def supported?
            true
          end
        end
      end
    end

    let(:location) { Uberall::Location.find(1) }

    it 'casts the relation into the correct type' do
      expect(listing).to be_kind_of(Uberall::Listing)
      expect(listing.supported?).to eq true
    end

    it 'keeps hirachy when casting it to another class on access' do
      expect(listing._root._raw).to eq location._raw
      expect(listing.parent.parent._raw).to eq location._raw
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lhs-15.3.1 spec/record/has_many_spec.rb
lhs-15.3.1.pre.fixlhc.1 spec/record/has_many_spec.rb
lhs-15.3.0 spec/record/has_many_spec.rb