blackchestnut / blackchestnut.github.io

Developer notes about Ruby on Rails, React Native, PostgreSQL, etc.
https://kalinichev.net
4 stars 0 forks source link

Disposable::Twin #5

Open blackchestnut opened 8 years ago

blackchestnut commented 8 years ago

Property::Hash module for work with generic hash fields

$ TeamDetail.first.phones
# [{ phone_number: '123', descripton: 'foo' }, { phone_number: '321', descripton: 'bar' } ]
# app/twins/team_detail_twin.rb
require 'disposable/twin/property/hash'

class TeamDetailTwin < Disposable::Twin
  feature Sync
  include Property::Hash

  collection :phones, field: :hash do
    property :number
    property :descripton
  end
end

# spec/twins/team_detail_spec.rb
describe TeamDetailTwin do
  let(:twin) { TeamDetailTwin.new(team_detail) }
  let(:team) { build_stubbed :team }

  describe '#phones' do
    subject { twin.phones }
    let(:team_detail) { build :team_detail, team: team, phones: phones }

    context 'blank' do
      let(:phones) { nil }
      it { is_expected.to be_empty }
    end

    context 'present' do
      let(:phones) do
        [
          { number: '123', descripton: 'foo' },
          { number: '321', descripton: 'bar' }
        ]
      end

      it { is_expected.to have(2).items }
      its('first.number') { is_expected.to eq '123' }
      its('first.descripton') { is_expected.to eq 'foo' }
    end
  end
end

Read more http://trailblazer.to/gems/disposable/api.html#propertyhash