Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where virtual attribute are included when aliasing joined table column names #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/vault/encrypted_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ def __vault_load_attributes!
end
end

# In ActiveRecord 4.2, virtual attributes are not excluded from joins aliases
if respond_to?(:persistable_attribute_names)
def self.column_names
super & persistable_attribute_names
end
end

# Decrypt and load a single attribute from Vault.
def __vault_load_attribute!(attribute, options)
# If the user provided a value for the attribute, do not try to load it from Vault
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class Person < ActiveRecord::Base
include Vault::EncryptedModel

has_many :problems

vault_attribute :county_plaintext, encrypted_column: :county_encrypted
vault_attribute_proxy :county, :county_plaintext

Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/app/models/problem.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Problem < ActiveRecord::Base
belongs_to :person
end
10 changes: 10 additions & 0 deletions spec/dummy/db/migrate/20181024200800_create_problems.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateProblems < ActiveRecord::Migration[5.0]
def change
create_table :problems do |t|
t.references :person
t.string :title

t.timestamps
end
end
end
44 changes: 24 additions & 20 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,34 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_10_17_154000) do
ActiveRecord::Schema.define(version: 20181024200800) do

create_table "people", force: :cascade do |t|
t.string "name"
t.string "ssn_encrypted"
t.string "cc_encrypted"
t.string "details_encrypted"
t.string "business_card_encrypted"
t.string "favorite_color_encrypted"
t.string "non_ascii_encrypted"
t.string "name"
t.string "ssn_encrypted"
t.string "cc_encrypted"
t.string "details_encrypted"
t.string "business_card_encrypted"
t.string "favorite_color_encrypted"
t.string "non_ascii_encrypted"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email_encrypted"
t.string "integer_data_encrypted"
t.string "float_data_encrypted"
t.string "time_data_encrypted"
t.string "county"
t.string "county_encrypted"
t.string "state"
t.string "state_encrypted"
end

create_table "problems", force: :cascade do |t|
t.integer "person_id"
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email_encrypted"
t.string "address"
t.string "address_encrypted"
t.date "date_of_birth"
t.string "date_of_birth_encrypted"
t.string "integer_data_encrypted"
t.string "float_data_encrypted"
t.string "time_data_encrypted"
t.string "county"
t.string "county_encrypted"
t.string "state"
t.string "state_encrypted"
t.index ["person_id"], name: "index_problems_on_person_id"
end

end
36 changes: 36 additions & 0 deletions spec/unit/encrypted_model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
require "spec_helper"

describe Vault::EncryptedModel do
describe 'query methods' do
let!(:person) { Person.create }
let!(:small_problem) { Problem.create person: person, title: 'small problem' }
let!(:big_problem) { Problem.create person: person, title: 'big problem' }

it 'works with includes' do
expect do
Person.where(id: person.id).includes(:problems).first
end.not_to raise_error
end

it 'works with where and references' do
expect do
Person.includes(:problems).where('problems.title like ?', '%small%').references(:problems).first
end.not_to raise_error
end

it 'works with where and association hash' do
expect do
Problem.where(person: person).first
end.not_to raise_error
end

it 'works with joins' do
expect do
Person.where(id: person.id).joins(:problems).first
end.not_to raise_error
end

it 'works with joins and includes' do
expect do
Person.joins(:problems).includes(:problems).first
end.not_to raise_error
end
end

describe ".vault_attribute" do
let(:person) { Person.new }

Expand Down