Sha256: 9f68713781144d1d66fd2d7f96ea9269d1f1c07a16d2869697a5f48e1ac3ad7b

Contents?: true

Size: 1.14 KB

Versions: 12

Compression:

Stored size: 1.14 KB

Contents

module Lockbox
  module Calculations
    def pluck(*column_names)
      return super unless model.respond_to?(:lockbox_attributes)

      lockbox_columns = column_names.map.with_index do |c, i|
        next unless c.respond_to?(:to_sym)
        [model.lockbox_attributes[c.to_sym], i]
      end.select do |la, _i|
        la && !la[:migrating]
      end

      return super unless lockbox_columns.any?

      # replace column with ciphertext column
      lockbox_columns.each do |la, i|
        column_names[i] = la[:encrypted_attribute]
      end

      # pluck
      result = super(*column_names)

      # decrypt result
      # handle pluck to single columns and multiple
      #
      # we can't pass context to decrypt method
      # so this won't work if any options are a symbol or proc
      if column_names.size == 1
        la = lockbox_columns.first.first
        result.map! { |v| model.send("decrypt_#{la[:encrypted_attribute]}", v) }
      else
        lockbox_columns.each do |la, i|
          result.each do |v|
            v[i] = model.send("decrypt_#{la[:encrypted_attribute]}", v[i])
          end
        end
      end

      result
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
lockbox-2.0.0 lib/lockbox/calculations.rb
lockbox-1.4.1 lib/lockbox/calculations.rb
lockbox-1.4.0 lib/lockbox/calculations.rb
lockbox-1.3.3 lib/lockbox/calculations.rb
lockbox-1.3.2 lib/lockbox/calculations.rb
lockbox-1.3.1 lib/lockbox/calculations.rb
lockbox-1.3.0 lib/lockbox/calculations.rb
lockbox-1.2.0 lib/lockbox/calculations.rb
lockbox-1.1.2 lib/lockbox/calculations.rb
lockbox-1.1.1 lib/lockbox/calculations.rb
lockbox-1.1.0 lib/lockbox/calculations.rb
lockbox-1.0.0 lib/lockbox/calculations.rb