Archive for March, 2008

sex spam

“Bang your way through all barriers” — Cleve Alibhoy

interactive treetop

# All credits go to Clifford Heath
# --------------------------------

require 'rubygems'
require 'treetop'
require 'my_grammar'
require 'readline'

parser = MyGrammarParser.new
while line = Readline::readline("? ", [])
  begin
    p parser.parse(line)
    rescue => e
    puts e
  end
end
puts

sex spam

“We invite you to the world of happiness!” — Rosalie Daly

prototype’s Element#removeClassName gotchas

// multiple classes MUST be removed by
// multiple calls to Element#removeClassName

element.removeClassName("show-resource inline");
// is NOT the same as
element.removeClassName("inline show-resource");

// THIS WORKS
element.removeClassName("show-resource");
element.removeClassName("inline");

a monkey’s stylable button_to

  def button_to(name, options = {}, html_options = {}, additional_content = "", descriptor_container = :span)

    html_options = html_options.stringify_keys
    convert_boolean_attributes!(html_options, %w( disabled ))

    method_tag = ''
    if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s)
     method_tag = tag(
       :input,
       :type => 'hidden',
       :name => '_method',
       :value => method.to_s
     )
    end

    form_id = (f_id = html_options.delete('form_id')) ? "id='#{f_id}'" : ""
    form_method = method.to_s == 'get' ? 'get' : 'post'

    request_token_tag = ''
    if form_method == 'post' && protect_against_forgery?
     request_token_tag = tag(
       :input,
       :type => "hidden",
       :name => request_forgery_protection_token.to_s,
       :value => form_authenticity_token
     )
    end

    if confirm = html_options.delete("confirm")
     html_options["onclick"] = "return #{confirm_javascript_function(confirm)};"
    end

    url = escape_once(options.is_a?(String) ? options : self.url_for(options))
    name ||= url

    descriptor = content_tag(descriptor_container.to_s, name, html_options)

    button_options = { "name" => "#{name.underscore}", "value" => name }
    button_id = html_options.delete("button_id")
    button_type = html_options.delete("button_type")
    button_options.merge!("id" => button_id) if button_id
    button_options.merge!("type" => button_type) if button_type

    form =  "
” form << "
” form << " #{method_tag}" form << " #{request_token_tag}" form << " #{content_tag("button", descriptor, button_options) }" form << "
” form << additional_content form << "
” form end

“locals” inside if branches

irb(main):008:0> def foo
irb(main):009:1>   if(bar = true)
irb(main):010:2>     puts "assigned bar"
irb(main):011:2>   end
irb(main):012:1>   puts "after conditional: bar = #{bar}"
irb(main):013:1> end
(irb):9: warning: found = in conditional, should be ==
=> nil
irb(main):014:0> foo
assigned bar
after conditional: bar = true

polymorphic link_to

# Why didn't I try out earlier if this is possible? Well, it is!

<%= link_to("Show", [ @foo, @bar ]) %>

rhythms

“Night is a state of consciousness and not a time of the day”

– me

truncate in active record models

# Put this in a config/initializers file e.g. config/initializers/truncate_helper.rb
# to make truncate available to all ActiveRecord::Base models

# credits for truncate go to:
# http://daniel.collectiveidea.com/blog/2007/7/10/a-prettier-truncate-helper

class ActiveRecord::Base

  # Awesome truncate
  # taken from http://daniel.collectiveidea.com/blog/2007/7/10/a-prettier-truncate-helper
  # -------------------------------------------------------------------------------------
  # First regex truncates to the length, plus the rest of that word, if any.
  # Second regex removes any trailing whitespace or punctuation (except ;).
  # Unlike the regular truncate method, this avoids the problem with cutting
  # in the middle of an entity ex.: truncate("this & that",9)  => "this &am..."
  # though it will not be the exact length.
  def truncate(text, length = 30, truncate_string = "...")
    return if text.nil?
    l = length - truncate_string.chars.length
    text.chars.length > length ? text[/\A.{#{l}}\w*\;?/m][/.*[\w\;]/m] + truncate_string : text
  end
end

sex spam

“Your rod is set to conquer” — Alphonso Degus

« Older Entries