Sha256: 2a856a0791d7a4a4ae0af6a2d5ad96beb53f93eaf045d5c28951b9c49c8cee19

Contents?: true

Size: 1.04 KB

Versions: 6

Compression:

Stored size: 1.04 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 { |c, i| [model.lockbox_attributes[c.to_sym], i] }.select(&:first)
      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

6 entries across 6 versions & 1 rubygems

Version Path
lockbox-0.4.9 lib/lockbox/calculations.rb
lockbox-0.4.8 lib/lockbox/calculations.rb
lockbox-0.4.7 lib/lockbox/calculations.rb
lockbox-0.4.6 lib/lockbox/calculations.rb
lockbox-0.4.5 lib/lockbox/calculations.rb
lockbox-0.4.4 lib/lockbox/calculations.rb