Skip to content

Instantly share code, notes, and snippets.

@Dante83
Created May 22, 2015 19:38
Show Gist options
  • Save Dante83/a4deffb7bf08f053961b to your computer and use it in GitHub Desktop.
Save Dante83/a4deffb7bf08f053961b to your computer and use it in GitHub Desktop.
def received
connection = ActiveRecord::Base.connection_pool.checkout
returned_notes_array = connection.execute("SELECT message_chain_id, note_read, note_id, sender_username, subject, body, created_at FROM (SELECT note_read, note_id FROM note_statuses WHERE note_statuses.deleted = 'f' AND note_statuses.user_id = #{current_user.id}) AS rs INNER JOIN notes ON notes.id = rs.note_id ORDER BY created_at desc")
ActiveRecord::Base.connection_pool.checkin(connection)
#Filter out messages where the only sender is the current user
results_with_different_senders = []
current_username = User.find(current_user.id).username
returned_notes_array.each do |note|
unless note['sender_username'] == current_username
results_with_different_senders.append(note)
end
end
#Gather all notes from unique message chain ids
uniq_returned_notes_array = results_with_different_senders.uniq{|n| n['message_chain_id']}
unpaginated_notes = []
uniq_returned_notes_array.each do |returned_note|
send_time = human_dating( DateTime.parse(returned_note['created_at']) )
unpaginated_notes.append(OpenStruct.new(:message_chain_id => returned_note['message_chain_id'],
:note_read => (returned_note['note_read'] == 't' ? true : false),
:note_id => returned_note['note_id'],
:sender_username => returned_note['sender_username'],
:subject => returned_note['subject'],
:body => returned_note['body'],
:message_sent_at => send_time ))
end
unless unpaginated_notes.empty?
@notes = unpaginated_notes.paginate(:page => params[:page], :per_page => 30)
end
render_appropriate_layout()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment