2011-08-08 10:03:13 -04:00
|
|
|
describe "attribute overrides" do
|
|
|
|
before do
|
2018-10-07 21:45:51 -04:00
|
|
|
define_model("User", admin: :boolean)
|
|
|
|
define_model("Post", title: :string,
|
2012-03-09 17:20:38 -05:00
|
|
|
secure: :boolean,
|
|
|
|
user_id: :integer) do
|
2011-08-08 10:03:13 -04:00
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
def secure=(value)
|
2018-10-07 18:02:54 -04:00
|
|
|
return unless user&.admin?
|
2011-08-08 10:03:13 -04:00
|
|
|
write_attribute(:secure, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-08-08 10:03:13 -04:00
|
|
|
factory :user do
|
|
|
|
factory :admin do
|
2018-07-29 11:30:02 -04:00
|
|
|
admin { true }
|
2011-08-08 10:03:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :post do
|
|
|
|
user
|
2018-07-29 11:30:02 -04:00
|
|
|
title { "default title" }
|
2011-08-08 10:03:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
let(:admin) { FactoryBot.create(:admin) }
|
2011-08-08 10:03:13 -04:00
|
|
|
|
|
|
|
let(:post_attributes) do
|
2012-03-09 16:14:06 -05:00
|
|
|
{ secure: false }
|
2011-08-08 10:03:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:non_admin_post_attributes) do
|
2017-10-20 15:20:28 -04:00
|
|
|
post_attributes[:user] = FactoryBot.create(:user)
|
2011-08-08 10:03:13 -04:00
|
|
|
post_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:admin_post_attributes) do
|
|
|
|
post_attributes[:user] = admin
|
|
|
|
post_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an admin posting" do
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.create(:post, admin_post_attributes) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:secure) { should eq false }
|
2011-08-08 10:03:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with a non-admin posting" do
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.create(:post, non_admin_post_attributes) }
|
2011-08-08 10:03:13 -04:00
|
|
|
its(:secure) { should be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with no user posting" do
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.create(:post, post_attributes) }
|
2011-08-08 10:03:13 -04:00
|
|
|
its(:secure) { should be_nil }
|
|
|
|
end
|
|
|
|
end
|