scottwillson/racing_on_rails

View on GitHub
app/models/name_observer.rb

Summary

Maintainability
A
35 mins
Test Coverage
# frozen_string_literal: true

class NameObserver < ActiveRecord::Observer
  def after_create(name)
    case name.nameable
    when Person
      name.nameable.results.each do |result|
        result.cache_attributes! :non_event if result[:name] != name.nameable.name(result.year)
      end
    when Team
      name.nameable.results.each do |result|
        result.cache_attributes! :non_event if result[:team_name] != name.nameable.name(result.year)
      end
    end
    true
  end
end