Skip to content

Instantly share code, notes, and snippets.

@rurabe
rurabe / gist:6c869ee1a3386c803b40
Last active August 29, 2015 14:22
Reorder as a concern
module IsOrderable
extend ActiveRecord::Concern
def change_position_to(n)
safe_n = self.class.sanitize(n)
current_position = self.class.sanitize(position)
connection.execute(<<-SQL
BEGIN;
UPDATE #{self.class.table_name} SET position = position + 1 WHERE parent_id = #{parent_id} AND position < #{current_position} AND position >= #{safe_n};
UPDATE #{self.class.table_name} SET position = position - 1 WHERE parent_id = #{parent_id} AND position > #{current_position} AND position <= #{safe_n};
(function(){
var copyToClipboard = str => {
var el = document.createElement('textarea'); // Create a <textarea> element
el.value = str; // Set its value to the string that you want copied
el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
el.style.position = 'absolute';
el.style.left = '-9999px'; // Move outside the screen to make it invisible
document.body.appendChild(el); // Append the <textarea> element to the HTML document
var selected =
document.getSelection().rangeCount > 0 // Check if there is any content selected previously