Skip to content

Instantly share code, notes, and snippets.

View amiel's full-sized avatar

Amiel Martin amiel

View GitHub Profile
module HelperClasses
# TabCreator
#
# generate html for tabs using partials and run the correct javascripts
#
# Example:
# <% HelperClasses::TabCreator.new 'account_tabs', self do |tabs| %> # => you must pass self so that TabCreator will have access to the template helpers
# <%= tabs.item 'General Settings', :partial => 'general_info_form' %>
# <% tabs.item 'Photo' do %> # => renders block, id will be #photo_tab
# foobar
class ApplicationController < ActionController::Base
# ...
layout :no_layout_for_xhr
private
def no_layout_for_xhr
request.xhr? ? nil : 'application'
end
end
$.fn.tabularosa = function(value) {
this.each(function() {
var self = $(this),
klass = 'tabularosa',
value = value || self.attr('data-tabularosa'),
set_value = function() {
if ($.trim(self.val()) == '' || self.val() == value)
self.val(value).addClass(klass);
},
clear_value = function() {
var MyClass = (function(){
var private_var = "private variable", self;
function initialize(a,b) {
self = this; // self gives access to "this" for private functions
this.a = a;
this.b = private_function(b);
print(this.a,this.b);
var MyModule = (function() {
function private_helper_function(a,b) {
Ruby.puts(a,b);
}
var functions = {
foo: function(a) {
private_helper_function(a, "world");
},
function complex_function() {
var local_variable = "foo"; // gets defined every time complex_function is run
var result;
var private_helper = function() { // this function gets declared every time complex_function is run
return local_variable;
};
// ok, this is actually what this function does
# my bash one-liner of the day
for file in app/models/trade_plan/*;do l=`head -1 $file|ruby -p -e 'm=$_.match /TradePlan::(\w+)/;$_=m[1]'`; ruby -p -i -e 'if $_.match /end/;$_=%( belongs_to :option, :class_name => "Listing::'$l'"\nend);end' $file;done
$.with_scope = function(scope, callback) {
var $scope = $(scope),
jquery_proxy = function(selector, context) { return $scope.find(selector, context); };
return callback(jquery_proxy);
};
// -- or --
$.fn.as_scope = function(callback) {
var self = this, jquery_proxy = function(selector, context) { return self.find(selector, context); };
$.with_scope('#post_15', function($) {
$('a.hide').click(function() {
$('.comments').hide();
});
});
$('#post_15').as_scope(function($) {
$('a.hide').click(function() {
$('.comments').hide();
module IncrementifyString
# Helps create a user friendly unique string
# For example, calling incrementify_string! repeatedly starting with
# 'string' would yield:
# 'string' => 'string1' => 'string2' => 'string3' ... 'string9' => 'string10' => 'string11' ... etc
def incrementify_string!(bar)
if bar.match(/[0-8]$/)
bar.succ! # this is faster than regex parse and to_i + 1 to_s
elsif bar.ends_with?('9')
m = bar.match(/(\d+)$/)