Skip to content

Instantly share code, notes, and snippets.

View gmgent's full-sized avatar

Krister Axel gmgent

  • Ashland IO LLC
  • Ashland, OR
View GitHub Profile
@gmgent
gmgent / new_test_user.rb
Created March 3, 2011 01:05
add test users on the fly with unique login
def new_test_user(args={})
i = rand(9999)
random_string = (Time.now.strftime("%s").to_i-i)
final_params = {:password => "testp",
:login => random_string,
:password_confirmation => "testp",
:email => "#{random_string}@gmg-entertainment.com"}.merge!(args)
User.create!(final_params)
end
@gmgent
gmgent / users_test.rb
Created March 3, 2011 01:10
testing all user method calls
require File.dirname(__FILE__) + '/../test_helper'
class UsersTest < ActiveSupport::TestCase
fixtures :roles, :partners
def setup
@u_admin = new_test_user(:role => roles(:admin), :partner => partners(:other))
@u_other = new_test_user(:role => roles(:other), :partner => partners(:other))
@u_admin_gmg = new_test_user(:role => roles(:admin), :partner => partners(:gmg))
@gmgent
gmgent / ftps.rb
Created March 8, 2011 18:19
ruby module for using ftps
require 'socket'
require 'openssl'
require 'net/ftp'
class Net::FTPS < Net::FTP
end
class Net::FTPS::Implicit < Net::FTP
FTP_PORT = 990
function unique(a)
{
var r = [];
o:for(var i = 0, n = a.length; i &lt; n; i++) {
for(var x = i + 1 ; x &lt; n; x++)
{
if(a[x]==a[i]) {continue o;}
}
r[r.length] = a[i];
}
class Blog
require 'open-uri'
require 'rss'
def self.latest
@blog ||= load_blog
end
protected
# CLOSURES IN RUBY Paul Cantrell http://innig.net
# Email: username "cantrell", domain name "pobox.com"
# I recommend executing this file, then reading it alongside its output.
#
# Alteratively, you can give yourself a sort of Ruby test by deleting all the comments,
# then trying to guess the output of the code!
# A closure is a block of code which meets three criteria:
#
class Mod
def self.ago(seconds)
case true
when seconds.to_i < 60
s = seconds.to_i
return "#{s} second#{s>1 ? "s" : ""} ago."
when seconds.to_i < 3600
m = seconds.to_i/60
s = seconds.to_i%60
require 'test_helper'
class ModTest < ActiveSupport::TestCase
test "ago function" do
assert_equal "1 second ago.", Mod.ago(1)
assert_equal "40 seconds ago.", Mod.ago(40)
assert_equal "1 minute, 1 second ago.", Mod.ago(61)
assert_equal "3 minutes, 1 second ago.", Mod.ago(181)
assert_equal "2 minutes, 40 seconds ago.", Mod.ago(160)
assert_equal "1 hour, 3 minutes, 1 second ago.", Mod.ago(3600 + 181)
def to_boolean(value, nil_value = false)
value.downcase! if value.class == String
case value
when "no","false",false, "0", 0
false
when "yes","true",true, "1", 1
true
when nil
nil_value
else
module Extensions
module ActiveRecord
# Understands how to generate counts and entries for the given scope, and offers some reasonable
# pagination helper methods.
class Paginator
attr_reader :count, :entries, :page, :per_page
def initialize(scope, opts={})
@page = opts[:page].try(:to_i) || first_page
@per_page = opts[:per_page].try(:to_i) || scope.proxy_scope.per_page