Skip to content

Instantly share code, notes, and snippets.

View laktek's full-sized avatar

Lakshan Perera laktek

View GitHub Profile
make_resourceful do
actions :all
before :show do
@articles = current_object.articles
end
before :index do
@books = AmazonService::get('0QZ1AAT31TZ9RNVANY82', 'hackruby-20', 'ruby programming') unless read_fragment(:fragment => 'books')
@videos = YoutubeService::get('E45845C8A1F5726D', 3) unless read_fragment(:fragment => 'videos')
@laktek
laktek / gist:33004
Created December 7, 2008 00:55
Rake task for Text generation
require "config/environment"
require "faker"
module Faker
class Lorem
Words = %w(am is are was when done here get could can have has smart cool I my we they he it she them Ruby on rails merb jruby web2.0 1.9 mashups advent 2008 tools linux apache Zed Shaw David quoted Gems plugins routes passive aggressive real world code refactor simple testing rspec behavior driven benchmark profile use server client service impressive number generator rake make done )
end
end
namespace :posts do
#check if at least one child has content - i.e. given a list of booleans, at least one is true?
inject(false) { |one_child_has_content, child| one_child_has_content || child.has_content? }
#check if all the constraints are true - i.e. given a list of booleans, all of them are true?
@constraints.inject(true) { |accepted, constraint| accepted && constraint.check(result) }
@laktek
laktek / snapshots.rb
Created January 8, 2009 18:39 — forked from nicksieger/snapshots.rb
script to create backup images
#!/usr/bin/ruby
class Integer
def days; hours * 24; end
def hours; minutes * 60; end
def minutes; seconds * 60; end
def seconds; self; end
end
def snapshot(name)
def tap
yield self
self
end unless Object.respond_to?(:tap)
# Yout could use unless/if to conditionally run a method
#!/bin/bash
# domainavailable
# Fast, domain name checker to use from the shell
# Use globs for real fun:
# domainavailable blah{1..3}.{com,net,org,co.uk}
# Inspired by foca / giles:
# http://gilesbowkett.blogspot.com/2009/02/simple-bash-domain-availability.html
trap 'exit 1' INT TERM EXIT
Given /^I have logged in as "(.*)"$/ do |name|
business = Business.make(:subdomain => "test")
@current_user = User.make(:email => "#{name}@example.com",
:password => "qaz123",
:password_confirmation => "qaz123",
:business => business)
visit "/login"
fill_in "Email", :with => "#{name}@example.com"
fill_in "Password", :with => "qaz123"
@laktek
laktek / gist:72105
Created February 28, 2009 21:01
has_many relationships
class Student < ActiveRecord::Base
has_many :rating
has_many :books, :through => :ratings
end
class Book < ActiveRecord::Base
has_many :ratings
has_many :students, :through => :ratings
end
#without :ujs option
link_to_function("Greeting", "alert('Hello world!')")
# <a href="#" onclick="alert('Hello world!'); return false;">Greeting</a>
#with :ujs and :id option
link_to_function("Greeting", "alert('Hello world!')", :id => "greeting_link", :ujs => true)
# <a href="#" id="greeting_link">Greeting</a>
# <script type="text/javascript">
# //assuming users JS framework of choice is jQuery
# $(\"#greeting_link\").click( function(){ alert('Hello world!'); return false } );
link_to_function("Show me more", nil, :id => "more_link") do |page|
page[:details].visual_effect :toggle_blind
page[:more_link].replace_html "Show me less"
end