Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/redacted_attributes/extensions/active_record/base.rb

Class Method Summary collapse

Class Method Details

.attr_redacted(*params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/redacted_attributes/extensions/active_record/base.rb', line 4

def attr_redacted(*params)
  define_attribute_methods rescue nil

  if const_defined?(:RedactedAttributes, _search_ancestors = false)
    mod = const_get(:RedactedAttributes)
  else
    mod = const_set(:RedactedAttributes, Module.new)
    include mod
  end

  params.each do |attribute|
    mod.module_eval(<<-REDACTED, __FILE__, __LINE__ + 1)
      def #{attribute}=(value)
        if value
          self.redacted_#{attribute} = value[0..2]
        end
        super(value)
      end
    REDACTED
  end
end