Module: ActiveEnquo::ActiveRecord::BaseExtension

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_enquo.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_read_attribute(attr_name, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_enquo.rb', line 77

def _read_attribute(attr_name, &block)
  t = self.class.attribute_types[attr_name]
  if t.is_a?(::ActiveEnquo::Type)
    relation = self.class.arel_table.name
    value = @attributes.fetch_value(attr_name, &block)
    return nil if value.nil?
    field = ::ActiveEnquo.root.field(relation, attr_name)
    begin
      t.decrypt(value, @attributes.fetch_value(@primary_key).to_s, field)
    rescue Enquo::Error
      # If the record had not yet been inserted into the database at the time the
      # attribute was originally written, then that attribute's context will be empty.
      # This is troublesome, but it's tricky to solve at this layer, so we'll have to
      # take the risk and try and decryption with empty context.
      t.decrypt(value, "", field)
    end
  else
    super
  end
end

#_write_attribute(attr_name, value) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_enquo.rb', line 98

def _write_attribute(attr_name, value)
  t = self.class.attribute_types[attr_name]
  if t.is_a?(::ActiveEnquo::Type)
    relation = self.class.arel_table.name
    field = ::ActiveEnquo.root.field(relation, attr_name)
    attr_opts = self.class.enquo_attribute_options.fetch(attr_name.to_sym, {})
    db_value = t.encrypt(value, @attributes.fetch_value(@primary_key).to_s, field, **attr_opts)
    @attributes.write_from_user(attr_name, db_value)
  else
    super
  end
end