Skip to content

Instantly share code, notes, and snippets.

View scarfacedeb's full-sized avatar
👀

Andrew Volozhanin scarfacedeb

👀
View GitHub Profile
@scarfacedeb
scarfacedeb / jquery-pattern.js
Last active December 18, 2015 12:39
jQuery plugin initialization example
$.fn.tabularize = function(options) {
if ( typeof options === 'string' ) {
var args = Array.prototype.slice.call( arguments, 1 );
this.each(function() {
var instance = $.data( this, 'tabularize' );
if ( !instance ) {
console.log( "cannot call methods on tabularize prior to initialization; " +
"attempted to call method '" + options + "'" );
return;
}
@scarfacedeb
scarfacedeb / module_start.js
Created September 9, 2013 20:02
My basic modular pattern for javascript files
var Module = (function(document, window, $){
var self = {},
// DOM/jQuery
$document = $(document),
$window = $(window),
// flags
@scarfacedeb
scarfacedeb / link_to_span.rb
Created September 24, 2013 03:43
Helpers that return either <a> or <span> tags, based on the condition
# lib/spoof_ip.rb
class SpoofIp
def initialize(app, ip)
@app = app
@ip = ip
end
def call(env)
env['REMOTE_ADDR'] = env['action_dispatch.remote_ip'] = @ip
@scarfacedeb
scarfacedeb / onetime.js
Created October 25, 2013 20:27
A One-Time Event function
// source: http://www.sitepoint.com/create-one-time-events-javascript/?
// create a one-time event
function onetime(node, type, callback) {
// create event
node.addEventListener(type, function(e) {
// remove event
e.target.removeEventListener(e.type, arguments.callee);
// call handler
@scarfacedeb
scarfacedeb / report_time_helper.rb
Created October 28, 2013 01:38
Helper to measure execution time
# Author: Ryan Bates
# see: https://github.com/railscasts/137-memoization-revised/blob/master/store-after/app/helpers/application_helper.rb
def report_time(code)
start = Time.now
result = eval(code)
finish = Time.now
render "report", code: code, result: result, time: finish - start
end
@scarfacedeb
scarfacedeb / i18n_widgets.rb
Created February 11, 2014 12:16
Custom view object
# An encapsulated class for i18n-related widgets in the footer
#
# It includes:
# - Locale switcher
# - Currency switcher
#
# Also it's state stores available translated locales for current pages
#
class I18nWidgets
@scarfacedeb
scarfacedeb / conditional_uglifier.rb
Last active August 29, 2015 13:56
Conditional uglifier to skip optimization on specific files
# in config/environments/production.rb
require 'ext/conditional_uglifier'
# Compress JavaScripts and CSS.
config.assets.js_compressor = ConditionalUglifier.new
# in lib/ext/conditional_uglifier.rb
initMasonry = function(){
console.log( "init masonry" );
// We don't need masonry on one-column layouts!
if ( checkMasonryBreakpoint() ) {
requestAnimationFrame(function(){
masonry = new Masonry( artworks, self.settings.masonry );
masonry.on( 'postLayout', function(){
console.log('layout done, just this one time');
@scarfacedeb
scarfacedeb / album_form_builder.rb
Created March 12, 2014 10:01
Polymorphic association for simple_form: 2 implementations
class AlbumFormBuilder < SimpleForm::FormBuilder
def polymorphic(association, options={})
options = options.dup
raise ArgumentError, "Association cannot be used in forms not associated with an object" unless @object
raise ArgumentError, "You need to pass types for select" unless options[:types]
reflection = find_association_reflection(association)
raise "Association #{association.inspect} not found" unless reflection