Skip to content

Commit

Permalink
add mark_as_outdated
Browse files Browse the repository at this point in the history
  • Loading branch information
usernaimandrey committed Dec 8, 2023
1 parent 8c7a1c3 commit 7a79f24
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions config/initializers/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module OutdatedAttributes
extend ActiveSupport::Concern

class_methods do
def mark_as_outdated(*attributes)
@outdated_attributes = Set.new(attributes.map(&:to_s))
rewrite_outdated_getters
rewrite_outdated_setters
end

def outdated_attributes
@outdated_attributes || Set.new
end

def rewrite_outdated_getters
outdated_attributes.each do |attribute|
define_method attribute.to_s do
raise "Attribute #{attribute} for class '#{self.class}' was marked as outdated"
end
end
end

def rewrite_outdated_setters
outdated_attributes.each do |attribute|
define_method "#{attribute}=" do |_value|
raise "Attribute #{attribute} for class '#{self.class}' was marked as outdated"
end
end
end
end
end

class ActiveRecord::Base
include OutdatedAttributes
end

0 comments on commit 7a79f24

Please sign in to comment.