Sha256: b51f7385ea47cf45643c6f066a5ebd8fee875ead86d6786a2ae50e456924a569

Contents?: true

Size: 1.51 KB

Versions: 11

Compression:

Stored size: 1.51 KB

Contents

require 'rails_helper'

describe LHS::Record do

  let(:transaction) { Transaction.find(1) }
  let(:user) { transaction.user }

  before do
    stub_request(:get, 'http://myservice/transactions/1')
      .to_return(body: {
        user: {
          email_address: 'steve@local.ch'
        }
      }.to_json)
  end

  context 'has_one' do

    before do
      class Transaction < LHS::Record
        endpoint 'http://myservice/transactions'
        endpoint 'http://myservice/transactions/{id}'
        has_one :user
      end

      class User < LHS::Record

        def email
          self[:email_address]
        end
      end
    end

    it 'casts the relation into the correct type' do
      expect(user).to be_kind_of(User)
      expect(user.email).to eq 'steve@local.ch'
    end

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

  context 'custom class_name' do

    before do
      class Transaction < LHS::Record
        endpoint 'http://myservice/transactions'
        endpoint 'http://myservice/transactions/{id}'
        has_one :user, class_name: 'Custom::User'
      end

      module Custom
        class User < LHS::Record
          def email
            self[:email_address]
          end
        end
      end
    end

    it 'casts the relation into the correct type' do
      expect(user).to be_kind_of(Custom::User)
      expect(user.email).to eq 'steve@local.ch'
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
lhs-16.1.0 spec/record/has_one_spec.rb
lhs-16.0.1 spec/record/has_one_spec.rb
lhs-16.0.0 spec/record/has_one_spec.rb
lhs-15.7.0 spec/record/has_one_spec.rb
lhs-15.6.1 spec/record/has_one_spec.rb
lhs-15.6.0 spec/record/has_one_spec.rb
lhs-15.5.1 spec/record/has_one_spec.rb
lhs-15.5.0 spec/record/has_one_spec.rb
lhs-15.4.1 spec/record/has_one_spec.rb
lhs-15.4.0 spec/record/has_one_spec.rb
lhs-15.4.0.pre.hasone.1 spec/record/has_one_spec.rb