Skip to content

Instantly share code, notes, and snippets.

# Present
"brain".present?
#=> true
"".present?
#=> false
#Hash#fetch
items = { :apples => 2, :oranges => 3 }
class LowCoverReport < ActiveRecord::Base
# This seems to be working fine, but is it the "right" way to achieve this ?
@@country_query = lambda{|o| {:conditions => ["country_id IN (#{o.countries.collect{|c| c.id}.join(', ')})"]} }
named_scope :for_user, @@country_query
named_scope :for_session, @@country_query
end
@spint
spint / instance_var_caching.rb
Created December 17, 2010 09:42
Caching 'false' in an instance variable in ruby (rails)
# this only gets cached in @admin if true
# how to cache this also in case of false?
def admin?
@admin ||= has_role? 'admin'
end
#is the following ok?
@spint
spint / gradient.sass
Created February 23, 2011 14:43
Cross-browser background gradient
// source: http://shaneriley.tumblr.com/post/3462508768/a-sass-mixin-for-cross-browser-css-background-gradients
= gradient-bg($color1, $color2)
background-color: $color2
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr=#{$color1}, endColorstr=#{$color2})
background-image: -moz-linear-gradient(center top, $color1, $color2)
background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from($color1), to($color2))
background-repeat: no-repeat
#container
@spint
spint / shadow_rounding_mixin.sass
Created March 2, 2011 15:22
sass mixins to apply shadow and rounding to boxes
@mixin shadow($a, $b, $color)
-webkit-box-shadow: $a $a $b $color
-moz-box-shadow: $a $a $b $color
box-shadow: $a $a $b $color
@mixin rounding($rad)
-moz-border-radius: $rad /* FF1+ */
-webkit-border-radius: $rad /* Saf3-4, iOS 1+, Android 1.5+ */
border-radius: $rad
@spint
spint / application.js
Created September 1, 2011 07:59
Build up jQuery-UI dialogs on the fly
// Creates a jQuery UI dialog on the fly, every time a link .user-link is clicked,
// dialog content will be loaded from the url specified by the clicked link
$(function(){
$(".user-link").live('click', function(){
var link = $(this);
$("<div><img src='" + BASEPATH + "images/small-spinner.gif' /> </div>")
.dialog({
autoOpen: true, //for info, true is default
modal: true,
# These are my notes from the PragProg book on CoffeeScript of things that either
# aren't in the main CS language reference or I didn't pick them up there. I wrote
# them down before I forgot, and put it here for others but mainly as a reference for
# myself.
# assign arguments in constructor to properties of the same name:
class Thingie
constructor: (@name, @url) ->
# is the same as:
@spint
spint / disable-back-swipe.js
Created November 15, 2013 09:31
Disabling back history navigation with swipe (chrome)
// http://stackoverflow.com/questions/15829172/stop-chrome-back-forward-two-finger-swipe
$(document).on('mousewheel', function(e) {
var $target = $(e.target).closest('.scrollable-h');
if ($target.scrollLeft () <= 4) {
$target.scrollLeft(5);
return false;
}
});
@spint
spint / elixir_xml.exs
Created September 29, 2015 19:08
Elixir XML processing (to JSON) with SweetXML
# mix.exs file dependencies:
# defp deps do
# [
# {:sweet_xml, "~> 0.4.0"},
# {:json, "~> 0.3.0"}
# ]
# end
defmodule ElixirXml do
import SweetXml