houdini/lib/update/update_order.rb

23 lines
725 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2020-06-12 20:03:43 +00:00
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
require 'qx'
module UpdateOrder
# data is an array of hashes of:
# - id : id of row to update
# - order: new order of row to update
def self.with_data(table_name, data)
vals = data.map { |h| "(#{h[:id].to_i}, #{h[:order].to_i})" }.join(', ')
from_str = "(VALUES #{vals}) AS data(id, \"order\")"
Qx.update(table_name.to_s)
.set('"order"="data"."order"')
.timestamps
.from(from_str)
.where("data.id=#{table_name}.id")
.returning("#{table_name}.order", "#{table_name}.id")
.execute
end
end