Skip to content

Instantly share code, notes, and snippets.

@shuhei
Last active December 20, 2015 02:49
Show Gist options
  • Save shuhei/6059459 to your computer and use it in GitHub Desktop.
Save shuhei/6059459 to your computer and use it in GitHub Desktop.
module DestroyableReadonly
extend ActiveSupport::Concern
included do
before_update :raise_readonly
end
private
def raise_readonly
raise ActiveRecord::ReadOnlyRecord
end
end
require 'spec_helper'
shared_examples 'destroyable readonly' do
let(:described_class_name) { described_class.name.underscore.to_sym }
describe '#save' do
context 'with new object' do
subject { FactoryGirl.build described_class_name }
its(:save) { should be_true }
end
context 'with saved object' do
subject { FactoryGirl.create described_class_name }
it 'raises readonly error' do
expect { subject.save }.to raise_error ActiveRecord::ReadOnlyRecord
end
end
end
describe '#destroy' do
subject! { FactoryGirl.create described_class_name }
it 'deletes itself' do
expect { subject.destroy }.to change { described_class.count }.from(1).to(0)
end
it 'returns itself' do
expect(subject.destroy).to eq subject
end
end
end
class Foo < ActiveRecord::Base
include DestroyableReadonly
end
require 'spec_helper'
describe Foo do
it_behaves_like 'destroyable readonly'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment