Module: DeprecatedAttributes::ClassMethods

Defined in:
lib/deprecated_attributes.rb

Overview

Class Methods Definitions

Instance Method Summary collapse

Instance Method Details

#accessorsObject



59
60
61
# File 'lib/deprecated_attributes.rb', line 59

def accessors
  ["", "=", "_before_type_cast", "?", "_changed?", "_change", "_will_change!", "_was"]
end

#clear_deprecated_attributes!Object

clears deprecated attributes for this class



55
56
57
# File 'lib/deprecated_attributes.rb', line 55

def clear_deprecated_attributes!
  @deprecated_attributes.clear
end

#deprecated_attribute(attrs = [], message: nil) ⇒ Object

Safely marks an attribute as deprecated (non-destructive).



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/deprecated_attributes.rb', line 28

def deprecated_attribute(attrs = [], message: nil)
  # pre-initialize any deprecated attributes
  attributes = Set.new(ensure_array(attrs).compact)
  @deprecated_attributes ||= Set.new

  # pre-initialize a new instance of our ActiveRecord model from `attributes`
  new(attributes.zip(attributes.map {}).to_h) if defined?(ActiveRecord) && ancestors.include?(ActiveRecord::Base)

  # Taking the difference of the two sets ensures we don't deprecate the same attribute more than once
  (attributes - deprecated_attributes).each do |attribute|
    override_deprecated_attribute(attribute, message)
  end

  @deprecated_attributes += attributes
end

#deprecated_attribute?(attribute) ⇒ Boolean

check if the passed attribute is defined

Returns:

  • (Boolean)


50
51
52
# File 'lib/deprecated_attributes.rb', line 50

def deprecated_attribute?(attribute)
  @deprecated_attributes.include?(attribute)
end

#deprecated_attributesObject

return a list of all deprecated attributes for this class



45
46
47
# File 'lib/deprecated_attributes.rb', line 45

def deprecated_attributes
  (@deprecated_attributes || Set.new).to_a
end

#notify_deprecated_attribute(payload) ⇒ Object

Dispatch a notification to the the Rails Logger



79
80
81
# File 'lib/deprecated_attributes.rb', line 79

def notify_deprecated_attribute(payload)
  ActiveSupport::Notifications.instrument("deprecated_attributes.active_record", payload)
end

#override_deprecated_attribute(attribute, message) ⇒ Object

Wrap the original attribute method with appropriate notification and errors



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/deprecated_attributes.rb', line 64

def override_deprecated_attribute(attribute, message)
  accessors.each do |term|
    original_attribute_method = instance_method("#{attribute}#{term}")
    define_method("#{attribute}#{term}") do |*args|
      backtrace = ActiveSupport::BacktraceCleaner.new.clean(caller)
      self.class.notify_deprecated_attribute(klass: self.class, attribute: attribute,
        args: args, backtrace: backtrace, msg: message)
      self.class.raise_deprecated_attribute

      original_attribute_method.bind_call(self, *args)
    end
  end
end

#raise_deprecated_attributeObject

Raises a DeprecatedAttributeError if DeprecatedAttributes is configurated to do so.



84
85
86
# File 'lib/deprecated_attributes.rb', line 84

def raise_deprecated_attribute
  raise DeprecatedAttributeError if DeprecatedAttributes.configuration.raise
end