Sha256: 7cb2c97437d091597e18edee7ac94a91aa64a62ddebea1e7b39b8def42fdc7f2

Contents?: true

Size: 1.78 KB

Versions: 13

Compression:

Stored size: 1.78 KB

Contents

require 'spec_helper'

describe Metal do
  describe '#find_or_create_by_path' do
    def assert_correctness(grandchild)
      expect(grandchild).to be_a(Metal)
      expect(grandchild.description).to eq('slag')
      child = grandchild.parent
      expect(child).to be_a(Unobtanium)
      expect(child.description).to eq('frames')
      expect(child.value).to eq('child')
      parent = child.parent
      expect(parent).to be_a(Adamantium)
      expect(parent.description).to eq('claws')
      expect(parent.value).to eq('parent')
    end

    let(:attr_path) do
      [
        {value: 'parent', description: 'claws', metal_type: 'Adamantium'},
        {value: 'child', description: 'frames', metal_type: 'Unobtanium'},
        {value: 'grandchild', description: 'slag', metal_type: 'Metal'}
      ]
    end

    before do
      # ensure the correct root is used in find_or_create_by_path:
      [Metal, Adamantium, Unobtanium].each do |metal|
        metal.find_or_create_by_path(%w(parent child grandchild))
      end
    end if false

    it 'creates children from the proper root' do
      assert_correctness(Metal.find_or_create_by_path(attr_path))
    end

    it 'supports STI from the base class' do
      assert_correctness(Metal.find_or_create_by_path(attr_path))
    end

    it 'supports STI from a subclass' do
      parent = Adamantium.create!(value: 'parent', description: 'claws')
      assert_correctness(parent.find_or_create_by_path(attr_path.last(2)))
    end

    it 'maintains the current STI subclass if attributes are not specified' do
      leaf = Unobtanium.find_or_create_by_path(%w(a b c d))
      expect(leaf).to be_a(Unobtanium)
      expect(leaf.ancestors.map(&:value)).to eq(%w(c b a))
      leaf.ancestors.each do |anc|
        expect(anc).to be_a(Unobtanium)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
closure_tree-6.6.0 spec/metal_spec.rb
closure_tree-6.5.0 spec/metal_spec.rb
closure_tree-6.4.0 spec/metal_spec.rb
closure_tree-6.3.0 spec/metal_spec.rb
closure_tree-6.2.0 spec/metal_spec.rb
closure_tree-6.1.0 spec/metal_spec.rb
closure_tree-6.0.0 spec/metal_spec.rb
closure_tree-6.0.0.gamma spec/metal_spec.rb
closure_tree-6.0.0.alpha spec/metal_spec.rb
closure_tree-5.2.0 spec/metal_spec.rb
closure_tree-5.1.1 spec/metal_spec.rb
closure_tree-5.1.0 spec/metal_spec.rb
closure_tree-5.0.0 spec/metal_spec.rb