Sha256: 5916cbde13d4836d53ff8378d6338f45028ea224e839e29d2f9d64c418838be3

Contents?: true

Size: 997 Bytes

Versions: 1

Compression:

Stored size: 997 Bytes

Contents

require 'models'

RSpec.describe "ActiveRecord::Base subclass with #actable" do
  subject { Product }

  let(:pen_attributes) { {name: 'pen', price: 0.8, color: 'red'} }
  let(:pen) { Pen.new pen_attributes }

  it "has a polymorphic belongs_to :actable relation" do
    association = subject.reflect_on_all_associations.find { |r| r.name == :actable }
    expect(association).to_not be_nil
    expect(association.macro).to eq(:belongs_to)
    expect(association).to be_polymorphic
  end

  describe ".specific" do
    it "return specific submodel" do
      pen.save
      expect(Product.find(pen.acting_as.id).specific).to eq(pen)
    end
  end

  it "deletes specific subclass on destroy" do
    pen.save
    pen.product.destroy
    expect { pen.reload }.to raise_error(ActiveRecord::RecordNotFound)
  end

  it "saves submodel on save" do
    pen.save
    product = pen.acting_as
    product.specific.color = 'blue'
    product.save
    pen.reload
    expect(pen.color).to eq('blue')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record-acts_as-1.0.0.pre spec/actable_spec.rb