docbrown/spec/models/poll_spec.rb
Jeremy Friesen be97f2fc07
Favoring dependent: :delete_all over :destroy (#15442)
* Favoring dependent: :delete_all over :destroy

The `dependent: :destroy` callback is slower than `dependent: :delete`
and `dependent: :delete_all`.  We need only favor the `dependent:
:destroy` when there's callbacks that happen.

In the case of :destroy, ActiveRecord instantiates each object and then
runs destroy.  Whereas in the case of :delete, ActiveRecord issues a SQL
command to delete the related files.

It is often "safer" to use :destroy, as it guarantees that you'll
instantiate the record and run it's callbacks.  But sometimes you have
to go with the speed of SQL.

This relates to #14140.  Note there is still more to consider, but given
that we're looking at moving from :destroy on all of an article's page
views to :delete, we might buy enough time in the callback.

* Removing redundancy of article destroy

Prior to this commit, `before_destroy_actions` called the `bust_cache`
method which in turn called `touch_actor_latest_article_updated_at` but
`bust_cache` did not pass the destroying parameter.  Then
`before_destroy_actions` immediately called
`touch_actor_latest_article_updated_at` with `destroying: true`.

With this change, we remove one of those duplicate calls.

* Fixing specs regarding relationships

* Fixing specs regarding relationships
2021-11-24 13:56:16 -05:00

46 lines
1.5 KiB
Ruby

require "rails_helper"
RSpec.describe Poll, type: :model do
let(:article) { create(:article, featured: true) }
describe "validations" do
let(:poll) { build(:poll, article: article) }
describe "builtin validations" do
subject { poll }
it { is_expected.to have_many(:poll_options).dependent(:delete_all) }
it { is_expected.to have_many(:poll_skips).dependent(:delete_all) }
it { is_expected.to have_many(:poll_votes).dependent(:delete_all) }
it { is_expected.to validate_length_of(:poll_options_input_array).is_at_least(2).is_at_most(15) }
it { is_expected.to validate_presence_of(:poll_options_count) }
it { is_expected.to validate_presence_of(:poll_options_input_array) }
it { is_expected.to validate_presence_of(:poll_skips_count) }
it { is_expected.to validate_presence_of(:poll_votes_count) }
it { is_expected.to validate_presence_of(:prompt_markdown) }
end
describe "#prompt_markdown" do
it "is valid up to 128 chars" do
poll.prompt_markdown = "x" * 128
expect(poll).to be_valid
end
it "is not valid with more than 128 chars" do
poll.prompt_markdown = "x" * 129
expect(poll).not_to be_valid
end
end
end
context "when callbacks are triggered after create" do
it "creates options from input" do
options = %w[hello goodbye heyheyhey]
expect do
create(:poll, article: article, poll_options_input_array: options)
end.to change(PollOption, :count).by(options.size)
end
end
end