Skip to content

Instantly share code, notes, and snippets.

# Convenience method for responding with JSON. Sets the content type,
# serializes, and allows empty responses. If json'ing an ActiveRecord object,
# and the object has errors on it, a 409 Conflict will be returned with a
# list of error messages.
def json(obj, status=200)
obj = {} if obj.nil?
if obj.respond_to?(:errors) && obj.errors.any?
obj = {'errors' => obj.errors.full_messages}
status = 409
end
/////////////////////////////////////////////////////////////////////////////
// The public DocumentViewer API. Available for other scripts to access:
/////////////////////////////////////////////////////////////////////////////
(function(){
DV.api = {
// Return the current page of the document.
currentPage : function() {
return DV.controller.models.document.currentPage();
// When the next click or keypress happens, anywhere on the screen, hide the
// element. 'clickable' makes the element and its contents clickable without
// hiding. The 'onHide' callback runs when the hide fires, and has a chance
// to cancel it.
autohide : function(options) {
var me = this;
options = _.extend({clickable : null, onHide : null}, options || {});
me._autoignore = true;
setTimeout(function(){ delete me._autoignore; }, 0);
<script type="text/javascript">
window.RAILS_ENV = '<%= RAILS_ENV %>';
window.SERVER_ROOT = '<%= DC_CONFIG["server_root"] %>';
<% if @current_account %>
dc.app.organization = <%= @current_organization.to_json %>;
dc.app.accountId = <%= @current_account.id %>;
Accounts.add(new dc.model.Account(<%= @current_account.to_json %>));
Labels.populate(<%= @current_account.labels.alphabetical.to_json %>);
Bookmarks.populate(<%= @current_account.bookmarks.alphabetical.to_json %>);
SavedSearches.populate(<%= @current_account.saved_searches.alphabetical.to_json %>);
def scan_for(matcher, text)
scanner = StringScanner.new(text)
last_byte, last_char = 0, 0
while scanner.scan_until(matcher)
byte = scanner.pos - scanner.matched.length
char = last_char + text[last_byte..byte].unpack('U*').length - 1
length = scanner.matched.unpack('U*').length
last_byte, last_char = byte, char
# Do something with the "char" offset and "length"...
end