forked from brendon/acts_as_list
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prefix position_column w/ table name in higher/lower item queries
Joining the relation returned by `#higher_items` or `#lower_items` to a table also having `position_column` would result in an ambiguous column error
- Loading branch information
Nathaniel Williams
committed
May 2, 2016
1 parent
a3b1d4e
commit 8ef09b4
Showing
2 changed files
with
68 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'helper' | ||
|
||
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | ||
ActiveRecord::Schema.verbose = false | ||
|
||
class Section < ActiveRecord::Base | ||
has_many :items | ||
acts_as_list | ||
|
||
scope :visible, -> { where(visible: true) } | ||
end | ||
|
||
class Item < ActiveRecord::Base | ||
belongs_to :section | ||
acts_as_list scope: :section | ||
|
||
scope :visible, -> { where(visible: true).joins(:section).merge(Section.visible) } | ||
end | ||
|
||
class JoinedTestCase < Minitest::Test | ||
def setup | ||
ActiveRecord::Base.connection.create_table :sections do |t| | ||
t.column :position, :integer | ||
t.column :visible, :boolean, default: true | ||
end | ||
|
||
ActiveRecord::Base.connection.create_table :items do |t| | ||
t.column :position, :integer | ||
t.column :section_id, :integer | ||
t.column :visible, :boolean, default: true | ||
end | ||
|
||
ActiveRecord::Base.connection.schema_cache.clear! | ||
[Section, Item].each(&:reset_column_information) | ||
super | ||
end | ||
|
||
def teardown | ||
ActiveRecord::Base.connection.tables.each do |table| | ||
ActiveRecord::Base.connection.drop_table(table) | ||
end | ||
super | ||
end | ||
end | ||
|
||
# joining the relation returned by `#higher_items` or `#lower_items` to another table | ||
# previously could result in ambiguous column names in the query | ||
class TestHigherLowerItems < JoinedTestCase | ||
def test_higher_items | ||
section = Section.create | ||
item1 = Item.create section: section | ||
item2 = Item.create section: section | ||
item3 = Item.create section: section | ||
assert_equal item3.higher_items.visible, [item1, item2] | ||
end | ||
|
||
def test_lower_items | ||
section = Section.create | ||
item1 = Item.create section: section | ||
item2 = Item.create section: section | ||
item3 = Item.create section: section | ||
assert_equal item1.lower_items.visible, [item2, item3] | ||
end | ||
end |