Simplify touching a budget when a phase changes

The `belongs_to` method already has that option, so there's no need to
do it manually in an `after_save` callback.
This commit is contained in:
Javi Martín
2019-10-23 02:56:28 +02:00
parent 35e8e9da31
commit bbce3479cf
2 changed files with 13 additions and 6 deletions

View File

@@ -11,7 +11,7 @@ class Budget
include Globalizable
include Sanitizable
belongs_to :budget
belongs_to :budget, touch: true
belongs_to :next_phase, class_name: self.name, inverse_of: :prev_phase
has_one :prev_phase, class_name: self.name, foreign_key: :next_phase_id, inverse_of: :next_phase
@@ -24,7 +24,6 @@ class Budget
validate :next_phase_dates_valid?
after_save :adjust_date_ranges
after_save :touch_budget
scope :enabled, -> { where(enabled: true) }
scope :published, -> { enabled.where.not(kind: "drafting") }
@@ -70,10 +69,6 @@ class Budget
end
end
def touch_budget
budget.touch
end
def prev_phase_dates_valid?
if enabled? && starts_at.present? && prev_enabled_phase.present?
prev_enabled_phase.assign_attributes(ends_at: starts_at)

View File

@@ -115,6 +115,18 @@ describe Budget::Phase do
end
end
describe "#save" do
it "touches the budget when it's updated" do
budget = create(:budget)
travel(10.seconds) do
budget.current_phase.update!(enabled: false)
expect(budget.updated_at).to eq Time.current
end
end
end
describe "#adjust_date_ranges" do
let(:prev_enabled_phase) { second_phase.prev_enabled_phase }
let(:next_enabled_phase) { second_phase.next_enabled_phase }