-
Notifications
You must be signed in to change notification settings - Fork 897
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We are now passing an array of table names into references. We were passing a nested hash of relations into references which is not valid. This is undefined behavior, and not guaranteed to work in the future. Currently, tails treats this as "just use references for all". We are introducing includes_to_references. It converts an includes compatible (hash of references) to a references compatible (array of table names)
- Loading branch information
Showing
7 changed files
with
60 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module ArReferences | ||
# Given a nested hash of associations (used by includes) | ||
# convert into an array of table names (used by references) | ||
# If given an array of table names, will output the same array | ||
def includes_to_references(inc) | ||
return [] unless inc | ||
|
||
inc = Array(inc) unless inc.kind_of?(Hash) | ||
inc.flat_map do |n, v| | ||
if (ref = reflect_on_association(n.to_sym)) && !ref.polymorphic? | ||
n_table = ref.table_name | ||
v_tables = v ? ref.klass.try(:includes_to_references, v) : [] | ||
[n_table] + v_tables | ||
elsif reflection_with_virtual(n.to_sym) # ignore polymorphic and virtual attribute | ||
[] | ||
else # it is probably a table name - keep it | ||
n | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
describe "ar_references" do | ||
describe ".includes_to_references" do | ||
it "supports none" do | ||
expect(Vm.includes_to_references(nil)).to eq([]) | ||
expect(Vm.includes_to_references([])).to eq([]) | ||
end | ||
|
||
it "supports arrays" do | ||
expect(Vm.includes_to_references(%w[host operating_system])).to eq(%w[hosts operating_systems]) | ||
expect(Vm.includes_to_references(%i[host])).to eq(%w[hosts]) | ||
end | ||
|
||
it "supports hashes" do | ||
expect(Hardware.includes_to_references(:vm => {})).to eq(%w[vms]) | ||
expect(Hardware.includes_to_references(:vm => :ext_management_system)).to eq(%w[vms ext_management_systems]) | ||
expect(Hardware.includes_to_references(:vm => {:ext_management_system => {}})).to eq(%w[vms ext_management_systems]) | ||
end | ||
|
||
it "supports table array" do | ||
expect(Vm.includes_to_references(%w[hosts operating_systems])).to eq(%w[hosts operating_systems]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters