Skip to content

Instantly share code, notes, and snippets.

@theareba
Created April 20, 2015 19:33
Show Gist options
  • Save theareba/c44a502594b11fd71f78 to your computer and use it in GitHub Desktop.
Save theareba/c44a502594b11fd71f78 to your computer and use it in GitHub Desktop.
Ruby and Js codes adapted from my apps
class Conversation < ActiveRecord::Base
belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :involving, -> (user) do
where("conversations.sender_id =? OR conversations.recipient_id =?",user.id,user.id)
end
scope :between, -> (sender_id,recipient_id) do
where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
end
end
var ready, set_positions;
set_positions = function(){
// loop through and give each task a data-pos
// attribute that holds its position in the DOM
$('.panel.panel-default').each(function(i){
$(this).attr("data-pos",i+1);
});
}
ready = function(){
// call set_positions function
set_positions();
$('.sortable').sortable();
// after the order changes
$('.sortable').sortable().bind('sortupdate', function(e, ui) {
// array to store new order
updated_order = []
// set the updated positions
set_positions();
// populate the updated_order array with the new task positions
$('.panel.panel-default').each(function(i){
updated_order.push({ id: $(this).data("id"), position: i+1 });
});
// send the updated order via ajax
$.ajax({
type: "PUT",
url: '/tasks/sort',
data: { order: updated_order }
});
});
}
$(document).ready(ready);
/**
* if using turbolinks
*/
$(document).on('page:load', ready);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment