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

Handle deprecated attributes (aliased) in VirtualArel.arel_attribute method #16489

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion lib/extensions/ar_virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module VirtualArel
module ClassMethods
def arel_attribute(column_name, arel_table = self.arel_table)
load_schema
if virtual_attribute?(column_name)
if virtual_attribute?(column_name) && !attribute_alias?(column_name)
col = _virtual_arel[column_name.to_s]
col.call(arel_table) if col
else
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/extensions/ar_virtual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,22 @@ def self.connection
end
belongs_to :ref1, :class_name => 'TestClass', :foreign_key => :col1
end

class TestClassWithDeprecatedAttr < TestClassBase
self.table_name = :test_classes
include DeprecationMixin
def self.connection
TestClassBase.connection
end
deprecate_attribute :old_col1, :col1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider adding a new, deprecated column to the existing TestClass? Alternatively, you could add a separate context for the test you added below with its own before/after hooks to manage the lifecycle of this fixture, so that it's not setup/torn down for a lot of tests that don't use it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I think you could just add this to an existing test class

end
end

after(:each) do
TestClassBase.remove_connection
Object.send(:remove_const, :TestClass)
Object.send(:remove_const, :TestClassBase)
Object.send(:remove_const, :TestClassWithDeprecatedAttr)
end

it "should not have any virtual columns" do
Expand Down Expand Up @@ -518,6 +528,12 @@ def col2

expect(c.attribute_supported_by_sql?(:col)).to eq(false)
end

it "supports deprecated attributes" do
expect(TestClassWithDeprecatedAttr.attribute_supported_by_sql?(:old_col1)).to eq(true)
expect(TestClassWithDeprecatedAttr.arel_attribute(:old_col1)).to_not be_nil
expect(TestClassWithDeprecatedAttr.arel_attribute(:old_col1).name).to eq("col1")
end
end

describe ".virtual_delegate" do
Expand Down