Skip to content

Instantly share code, notes, and snippets.

#before
def load_submissions_count
Enrollment.where(initiative_id: @initiative_ids).group(:initiative_id).select('initiative_id, sum(submissions_count) as sum_submissions_count').each do |data|
initiative = @initiatives_data[data.initiative_id]
initiative.cached_submissions_count = data.sum_submissions_count.to_i
end
end
#after
def load_submissions_count
# Format
# owner email address: <email address of owner of the question>
# question: <text of the question>
# total_enrollments: <total number of enrollments for this question>
# primary_user_category:
# name: <category name>:
# values:
# <user_category_value.name>:
# percentage_of_total: <percentage of enrollments in this category_value>
# promoter_percentage: <percentage of enrollments in this category_value that are promoters>
#---------------------------
#profile_controller_spec.rb
#---------------------------
describe ProfileController do
shared_examples_for "User" do
it "should show user profile" do
get :index
response.should be_success
end
user = mock_model(User)
User.stub!(:find).at_least(1).and_return do |id|
if id == mock_user.id.to_s
user
else
User.find_by_id(id)
end
end
def stub_find_for_specific_values(model, stubs)
model.stub!(:find).at_least(1).and_return do |id|
if stubs.has_key? id
stubs[id]
else
model.find_by_id(id)
end
end
end
named_scope :active_within, lambda {|time|
{:joins => {:workflows => :activities},
:conditions => ["activities.created_at > ?", time.ago],
:select => "DISTINCT(users.*)"}
@drewB
drewB / gist:3079141
Created July 9, 2012 21:35
helper for waiting that all dom manipulation had completed
#will wait until all dom elements are rendered
def wait_for_dom(timeout = Capybara.default_wait_time)
uuid = SecureRandom.uuid
page.find(:xpath, "//body")
page.evaluate_script <<-EOS
_.defer(function() {
$('body').append("<div id='#{uuid}'></div>");
});
EOS
wait_until(timeout) {page.find(:xpath, "//div[@id='#{uuid}']")}
@drewB
drewB / gist:85a0befb2ad9b4fc75ad
Last active February 22, 2016 16:27 — forked from anonymous/gist:748f0c95eb5fe4bafbb6
Metawear: example of how to persisting event after disconnects
import Foundation
class MetawearConfig:NSObject, MBLRestorable {
var pulseWidthEvent:MBLEvent!
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.pulseWidthEvent, forKey: "pulseWidthEvent")
}
@drewB
drewB / alarm.js
Last active March 18, 2018 00:41
phantomjs script for arming Alarm.com panel
var page;
page = require('webpage').create();
page.viewportSize = { width: 320, height: 780 };
phantom.waitFor = function(callback) {
do {
// Clear the event queue while waiting.
// This can be accomplished using page.sendEvent()
this.page.sendEvent('mousemove');
@drewB
drewB / alarm_start.js
Last active July 17, 2019 15:41
headless chrome (puppeteer) script for arming Alarm.com panel
const puppeteer = require('puppeteer');
var config;
config = {
username: 'username',
password: 'password'
};
(async () => {
const browser = await puppeteer.launch({