Module: ActiveEnquo::ActiveRecord::BaseExtension::ClassMethods

Includes:
QueryFilterMangler
Defined in:
lib/active_enquo.rb

Instance Method Summary collapse

Instance Method Details

#enquo(attr_name, value_or_meta_id, maybe_value = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/active_enquo.rb', line 119

def enquo(attr_name, value_or_meta_id, maybe_value = nil)
  meta_id, value = if value_or_meta_id.is_a?(Symbol)
    [value_or_meta_id, maybe_value]
  else
    [nil, value_or_meta_id]
  end

  t = self.attribute_types[attr_name.to_s]
  if t.is_a?(::ActiveEnquo::Type)
    relation = self.arel_table.name.to_s
    field = ::ActiveEnquo.root.field(relation, attr_name.to_s)
    if meta_id.nil?
      t.encrypt(value, "", field, enable_reduced_security_operations: true)
    else
      t.(meta_id, value, field)
    end
  else
    raise ArgumentError, "Cannot produce encrypted value on a non-enquo attribute '#{attr_name}'"
  end
end

#enquo_attr(attr_name, opts) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/active_enquo.rb', line 159

def enquo_attr(attr_name, opts)
  if opts.key?(:default)
    default_value = opts.delete(:default)
    after_initialize do
      next if persisted?
      next unless self.send(attr_name).nil?
      self.send(:"#{attr_name}=", default_value.duplicable? ? default_value.dup : default_value)
    end
  end

  enquo_attribute_options[attr_name] = @enquo_attribute_options[attr_name].merge(opts)
end

#enquo_attr?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/active_enquo.rb', line 155

def enquo_attr?(attr_name)
  self.attribute_types[attr_name.to_s].is_a?(::ActiveEnquo::Type)
end

#enquo_attribute_optionsObject



172
173
174
# File 'lib/active_enquo.rb', line 172

def enquo_attribute_options
  @enquo_attribute_options ||= Hash.new({})
end

#enquo_encrypt_columns(column_map, batch_size: 10_000) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/active_enquo.rb', line 176

def enquo_encrypt_columns(column_map, batch_size: 10_000)
  plaintext_columns = column_map.keys
  relation = self.arel_table.name
  in_progress = true
  self.reset_column_information

  while in_progress
    self.transaction do
      # The .where("0=1") here is a dummy condition so that the q.or in the .each will work properly
      q = self.select(self.primary_key).select(plaintext_columns).where("0=1")
      column_map.each do |pt_col, ct_col|
        q = q.or(self.where(ct_col => nil).where.not(pt_col => nil))
      end

      q = q.limit(batch_size).lock

      rows = ::ActiveRecord::Base.connection.exec_query(q.to_sql)
      if rows.length == 0
        in_progress = false
      else
        rows.each do |row|
          values = Hash[column_map.map do |pt_col, ct_col|
            field = ::ActiveEnquo.root.field(relation, ct_col)
            attr_opts = self.enquo_attribute_options.fetch(ct_col.to_sym, {})
            t = self.attribute_types[ct_col.to_s]
            db_value = t.encrypt(row[pt_col.to_s], row[self.primary_key].to_s, field, **attr_opts)

            [ct_col, db_value]
          end]

          self.where(self.primary_key => row[self.primary_key]).update_all(values)
        end
      end
    end
  end
end

#find_by(*a) ⇒ Object



114
115
116
117
# File 'lib/active_enquo.rb', line 114

def find_by(*a)
  mangle_query_filter(a)
  super
end

#unenquo(attr_name, value, ctx) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/active_enquo.rb', line 140

def unenquo(attr_name, value, ctx)
  t = self.attribute_types[attr_name.to_s]
  if t.is_a?(::ActiveEnquo::Type)
    relation = self.arel_table.name.to_s
    field = ::ActiveEnquo.root.field(relation, attr_name.to_s)
    begin
      t.decrypt(value, ctx, field)
    rescue Enquo::Error
      t.decrypt(value, "", field)
    end
  else
    raise ArgumentError, "Cannot decrypt value on a non-enquo attribute '#{attr_name}'"
  end
end