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 all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Force ther original JoinPart to load
require 'active_record/associations/join_dependency/join_part'

module ActiveRecord
module Associations
class JoinDependency
class JoinPart
# Prevent virtual attributes from being included in JOIN sql queries.
# This will work with both ActiveRecord 4 an 5 because in the original
# implementation this methos is delegated to the base class - the model.
def column_names
column_names = base_klass.column_names

if base_klass.methods.include? :persistable_attribute_names
column_names = column_names & base_klass.persistable_attribute_names
end

column_names
end
end
end
end
end
5 changes: 5 additions & 0 deletions lib/vault/rails.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'active_record'
require 'vault'

require 'base64'
Expand All @@ -12,6 +13,10 @@
require_relative 'rails/serializers/float_serializer'
require_relative 'rails/version'

if ActiveRecord.version < Gem::Version.new('5.0.0')
require_relative 'active_record/associations/join_dependency/join_part'
end

module Vault
module Rails
# The list of serializers.
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
class Person < ActiveRecord::Base
include Vault::EncryptedModel

has_many :problems

before_validation :format_ssn, if: -> { ssn_changed? }

def format_ssn; end

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