Last active
December 20, 2015 02:49
-
-
Save shuhei/6059459 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DestroyableReadonly | |
extend ActiveSupport::Concern | |
included do | |
before_update :raise_readonly | |
end | |
private | |
def raise_readonly | |
raise ActiveRecord::ReadOnlyRecord | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo < ActiveRecord::Base | |
include DestroyableReadonly | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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