Skip to content

Instantly share code, notes, and snippets.

View strika's full-sized avatar

Nebojša Stričević strika

View GitHub Profile
@strika
strika / preload_polymorphic.rb
Created October 28, 2016 07:42
Preloading polymorphic associations
class Alert
belongs_to :alertable, polymorphic: true
belongs_to :incident, ->{ joins(:alerts).where(alerts: { id: Alert.where(alertable_type: "Incident") }) }, foreign_key: :alertable_id
belongs_to :action, ->{ joins(:alerts).where(alerts: { id: Alert.where(alertable_type: "Action") }) }, foreign_key: :alertable_id
end
Alert.includes(incident: :categories, action: :agencies)
@strika
strika / enable_rack_mini_profiler_on_staging.md
Created June 23, 2016 09:49
How to enable Rack MiniProfiler on staging?

Add these few lines to Gemfile:

gem 'rack-mini-profiler', :require => false
gem 'flamegraph'
gem 'stackprof'
gem 'memory_profiler'

Add this to config/initializers/rack_profiler.rb

@strika
strika / maximize_firefox.md
Last active April 25, 2016 14:37
How to maximize Firefox in Selenium test

To maximize Firefox window in all Cucumber scenarios, place this in your support/env.rb.

Before do
  Capybara.current_driver == Capybara.javascript_driver
    page.driver.browser.manage.window.maximize
  end
end
@strika
strika / install_latest_postgresql.sh
Created April 4, 2016 07:10
Install latest PostgreSQL on Ubuntu
# Based on http://www.postgresql.org/download/linux/ubuntu/
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
@strika
strika / selenium_browser_logs.rb
Created February 26, 2016 13:56
Print browser logs in selenium-webdriver
def print_browser_logs
logs = page.driver.browser.manage.logs.get(:browser).map { |line| [line.level, line.message] }
logs.reject! { |line| ["WARNING", "INFO"].include?(line.first) }
puts "========== BROWSER LOGS =========="
logs.each do |line|
puts line.join(" - ")
end
puts "=================================="
end
@strika
strika / github_reflog_saver.sh
Created August 27, 2015 09:32
Save commit from GitHub reflog
#!/bin/bash
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: token API_TOKEN" -X POST -d '{"ref":"refs/heads/D-commit", "sha":"LOST_SHA"}' https://api.github.com/repos/ACCOUNT/PROJECT/git/refs
@strika
strika / resolution-change.js
Created February 21, 2015 12:34
Resolution change listener
$(window).on("resize, orientationchange", function() {
$.throttle(function() {
$(".custom-select").customSelect();
}, 500);
});
@strika
strika / rest-design.txt
Last active August 29, 2015 14:14
Turning requirements into read/write resources
1. Figure out the data set
2. Split the data set into resources.
For each kind of resource:
3. Name the resources with URIs.
4. Expose a subset of the uniform interface.
5. Design the representation(s) accepted from the client.
6. Design the representation(s) served to the client.
7. Integrate this resource into eisting resources, using hypermedia links and forms.
8. Consider the typical course of events: what's supposed to happen?
9. Consider error conditions: what might go wrong?
@strika
strika / ng-bindings.js
Created November 12, 2014 11:40
Number of bindings in AngularJS application
function getScopes(root) {
var scopes = [];
function traverse(scope) {
scopes.push(scope);
if (scope.$$nextSibling)
traverse(scope.$$nextSibling);
if (scope.$$childHead)
traverse(scope.$$childHead);
}
traverse(root);