Skip to content

Instantly share code, notes, and snippets.

@unnitallman
unnitallman / gist:8802165
Created February 4, 2014 11:38
Rails UJS extension to add 'prompt'
/*
How to use:
%= link_to 'link text', url, data: {prompt: 'Enter your password', param-name: 'password', method: :delete}
*/
RailsJSPrompt = {
prompt: function(l){
@unnitallman
unnitallman / file_splitter.rb
Created March 23, 2014 03:16
CSV File splitter
# Usage: FileSplitter.new('/usr/share/dict/words').split_into 10
class FileSplitter
def initialize(path)
@file = File.open path
end
def split_into(n)
i = 1
m = slice_size(n)
Randwick - 1 year - site wide - traffic graph
SELECT COUNT(DISTINCT user_id, user_type) as count, DATE( FROM_UNIXTIME(date)) as created_on FROM `beta_reports_user_session_types` WHERE `beta_reports_user_session_types`.`site_id` = 224 AND `beta_reports_user_session_types`.`project_id` IN (2788, 2804, 2797, 2816, 2800, 5060, 7397, 2822, 7611, 6445, 6617, 4217, 2808, 6731, 2813, 6744, 5046, 2792, 6953, 2809, 2805, 2823, 2799, 2810, 2827, 2795, 2794, 2791, 2825, 2819, 2821, 2824, 2978, 2798, 6619, 2811, 2787, 2801, 2807, 2989, 2826, 8019, 2786, 2814, 2790, 2818, 2820, 5690, 2806, 2796, 2793, 2803, 2789, 2785, 2815, 6298, 2817, 2802, 2812, 2828) AND (`beta_reports_user_session_types`.`date` BETWEEN 1387426750 AND 1417492931) GROUP BY created_on ORDER BY created_on
On demo server
| 110 | 2014-11-15 |
| 157 | 2014-11-16 |
+-------+------------+
332 rows in set (16.82 sec)
@unnitallman
unnitallman / README.md
Created March 12, 2015 06:03 — forked from oodavid/README.md
Mysql to S3 backup

Backup MySQL to Amazon S3

This is a simple way to backup your MySQL tables to Amazon S3 for a nightly backup - this is all to be done on your server :-)

Sister Document - Restore MySQL from Amazon S3 - read that next

1 - Install s3cmd

this is for Centos 5.6, see http://s3tools.org/repositories for other systems like ubuntu etc

<script type="text/javascript">
function validate_contact_admin_form(form_id) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.forms[form_id].elements['email'].value;
if(reg.test(address) == false) {
alert('Invalid Email Address');
return false;
}
}
</script>
@unnitallman
unnitallman / gist:1010056
Created June 6, 2011 10:36
Setting a cookie with an expiry time
cookies[:survey_user_id] = { :value => self.current_user.id.to_s, :expires => 1.year.from_now }
@unnitallman
unnitallman / gist:1153108
Created August 18, 2011 01:47
FTP with Ruby
require 'net/ftp'
ftp=Net::FTP.new
ftp.connect(URL,21)
ftp.login(username,passwd)
ftp.chdir(directory)
ftp.getbinaryfile(filename)
ftp.close
http://ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/index.html
@unnitallman
unnitallman / gist:1188060
Created September 2, 2011 06:50
Specifying rails version to use when creating a new application
rails _2.2.2_ myapp
@unnitallman
unnitallman / connection_fix.rb
Created September 3, 2011 09:13 — forked from defunkt/connection_fix.rb
MySQL server has gone away fix
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
@unnitallman
unnitallman / gist:1237177
Created September 23, 2011 11:47
Typecasting a base class object to a derived class object
u = User.find(id)
u = u.becomes(u.type.constantize)