Sha256: 4248c884f0dfe323663cbf606ded1298613e06c76e0c893666c934a300e70ecc

Contents?: true

Size: 1.03 KB

Versions: 21

Compression:

Stored size: 1.03 KB

Contents

require 'spec_helper'

describe 'Inheritance' do
  before do
    module Examples
      class Base
        include Virtus.model
      end

      class First < Base
        attribute :id, Fixnum
        attribute :name, String, default: ->(first, _) { "Named: #{first.id}" }
        attribute :description, String
      end

      class Second < Base
        attribute :something, String
      end
    end
  end

  it 'inherits model from the base class' do
    expect(Examples::First.attribute_set.map(&:name)).to eql([:id, :name, :description])
    expect(Examples::Second.attribute_set.map(&:name)).to eql([:something])
  end

  it 'sets correct attributes on the descendant classes' do
    first = Examples::First.new(:id => 1, :description => 'hello world')

    expect(first.id).to be(1)
    expect(first.name).to eql('Named: 1')
    expect(first.description).to eql('hello world')

    second = Examples::Second.new

    expect(second.something).to be(nil)

    second.something = 'foo bar'

    expect(second.something).to eql('foo bar')
  end
end

Version data entries

21 entries across 19 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/integration/inheritance_spec.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/integration/inheritance_spec.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/integration/inheritance_spec.rb
virtus2-2.1.0 spec/integration/inheritance_spec.rb
virtus2-2.0.2 spec/integration/inheritance_spec.rb
virtus2-2.0.1 spec/integration/inheritance_spec.rb
virtus-2.0.0 spec/integration/inheritance_spec.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/virtus-1.0.5/spec/integration/inheritance_spec.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/virtus-1.0.5/spec/integration/inheritance_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/inheritance_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/inheritance_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/inheritance_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/inheritance_spec.rb
virtus-1.0.5 spec/integration/inheritance_spec.rb
virtus-1.0.4 spec/integration/inheritance_spec.rb
virtus-1.0.3 spec/integration/inheritance_spec.rb
virtus-1.0.2 spec/integration/inheritance_spec.rb
virtus-1.0.1 spec/integration/inheritance_spec.rb
virtus-1.0.0 spec/integration/inheritance_spec.rb
virtus-1.0.0.rc2 spec/integration/inheritance_spec.rb