Sha256: 796586437e56d53f7e8cc9aaa571403ef1fe9c321d13ed3b32ba00cac21a94b8

Contents?: true

Size: 942 Bytes

Versions: 3

Compression:

Stored size: 942 Bytes

Contents

describe Post do
  before(:all) { Post.create(title: "Test Post", body: "This is a test.", uuid: SecureRandom.uuid) }
  after(:all)  { Post.first.destroy }

  context "with standard after_initialize" do
    let(:klass) do
      Class.new(Post).tap do |k|
        k.after_initialize { self.uuid ||= SecureRandom.uuid }
      end
    end

    it "works fine when all columns are selected" do
      expect{ klass.first }.to_not raise_error
    end

    it "raises ActiveModel::MissingAttributeError when uuid is not selected" do
      expect{ klass.select(:title).first }.to raise_error(ActiveModel::MissingAttributeError)
    end
  end

  context "with safe_initialize" do
    let(:klass) do
      Class.new(Post).tap do |k|
        k.safe_initialize :uuid, with: ->{ SecureRandom.uuid }
      end
    end

    it "does not raise an error when uuid is missing" do
      expect{ klass.select(:title).first }.to_not raise_error
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-safe_initialize-0.2.0 spec/post_spec.rb
activerecord-safe_initialize-0.1.0 spec/post_spec.rb
activerecord-safe_initialize-0.0.1 spec/post_spec.rb