Skip to content

Commit

Permalink
Use quote_table_name_for_assignment...
Browse files Browse the repository at this point in the history
...in `update_all` calls to guard against reserved word column names.
  • Loading branch information
brendon committed Apr 8, 2024
1 parent 140c069 commit aca5509
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased]

- Fetch the adapter_name from #connection_db_config (@tijn)
- Use `quote_table_name_for_assignment` in `update_all` calls to guard against reserved word column names.

## [0.2.0] - 2024-03-12

Expand Down
12 changes: 8 additions & 4 deletions lib/positioning/mechanisms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def primary_key
@positioned.send primary_key_column
end

def quoted_column
base_class.connection.quote_table_name_for_assignment base_class.table_name, @column
end

def record_scope
base_class.where("#{primary_key_column}": primary_key)
end
Expand Down Expand Up @@ -86,13 +90,13 @@ def move_out_of_the_way
end

def expand(scope, range)
scope.where("#{@column}": range).update_all "#{@column} = #{@column} * -1"
scope.where("#{@column}": ..-1).update_all "#{@column} = #{@column} * -1 + 1"
scope.where("#{@column}": range).update_all "#{quoted_column} = #{quoted_column} * -1"
scope.where("#{@column}": ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 + 1"
end

def contract(scope, range)
scope.where("#{@column}": range).update_all "#{@column} = #{@column} * -1"
scope.where("#{@column}": ..-1).update_all "#{@column} = #{@column} * -1 - 1"
scope.where("#{@column}": range).update_all "#{quoted_column} = #{quoted_column} * -1"
scope.where("#{@column}": ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 - 1"
end

def solidify_position
Expand Down
5 changes: 5 additions & 0 deletions test/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Post < ActiveRecord::Base
positioned column: :order

default_scope { order(:order) }
end
7 changes: 7 additions & 0 deletions test/support/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
end

add_index :authors, [:list_id, :enabled, :position], unique: true

create_table :posts, force: true do |t|
t.string :name
t.integer :order, null: false
end

add_index :posts, :order, unique: true
end
end

Expand Down
32 changes: 32 additions & 0 deletions test/test_positioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative "models/author"
require_relative "models/author/student"
require_relative "models/author/teacher"
require_relative "models/post"

class TestRelativePositionStruct < Minitest::Test
def test_struct_takes_keyword_arguments
Expand Down Expand Up @@ -527,6 +528,37 @@ def test_that_not_destroyed_via_positioning_scope_closes_gap
end
end

class TestPositioningColumns < Minitest::Test
include Minitest::Hooks

def around
ActiveRecord::Base.transaction do
super
raise ActiveRecord::Rollback
end
end

def test_that_a_column_named_order_works
first_post = Post.create name: "First Post"
second_post = Post.create name: "Second Post"
third_post = Post.create name: "Third Post"

assert_equal [1, 2, 3], [first_post.reload, second_post.reload, third_post.reload].map(&:order)

second_post.update order: {before: first_post}

assert_equal [1, 2, 3], [second_post.reload, first_post.reload, third_post.reload].map(&:order)

first_post.update order: {after: third_post}

assert_equal [1, 2, 3], [second_post.reload, third_post.reload, first_post.reload].map(&:order)

third_post.destroy

assert_equal [1, 2], [second_post.reload, first_post.reload].map(&:order)
end
end

class TestPositioning < Minitest::Test
include Minitest::Hooks

Expand Down

0 comments on commit aca5509

Please sign in to comment.