Sha256: 2cfee016e9572f2eb3bf8087c26cc492ff15eda47fbe799cf922255edf10fbc9

Contents?: true

Size: 1.78 KB

Versions: 19

Compression:

Stored size: 1.78 KB

Contents

require 'rails_helper'

describe LHS::Record do

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

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

  context 'has_many' do

    before 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 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

19 entries across 19 versions & 1 rubygems

Version Path
lhs-16.1.5 spec/record/has_many_spec.rb
lhs-16.1.4 spec/record/has_many_spec.rb
lhs-16.1.3 spec/record/has_many_spec.rb
lhs-16.1.2 spec/record/has_many_spec.rb
lhs-16.1.1 spec/record/has_many_spec.rb
lhs-16.1.0 spec/record/has_many_spec.rb
lhs-16.0.1 spec/record/has_many_spec.rb
lhs-16.0.0 spec/record/has_many_spec.rb
lhs-15.7.0 spec/record/has_many_spec.rb
lhs-15.6.1 spec/record/has_many_spec.rb
lhs-15.6.0 spec/record/has_many_spec.rb
lhs-15.5.1 spec/record/has_many_spec.rb
lhs-15.5.0 spec/record/has_many_spec.rb
lhs-15.4.1 spec/record/has_many_spec.rb
lhs-15.4.0 spec/record/has_many_spec.rb
lhs-15.4.0.pre.hasone.1 spec/record/has_many_spec.rb
lhs-15.3.3 spec/record/has_many_spec.rb
lhs-15.3.3.pre.fixoptions.1 spec/record/has_many_spec.rb
lhs-15.3.2 spec/record/has_many_spec.rb