Skip to content

Instantly share code, notes, and snippets.

View dwg's full-sized avatar

Árni Einarsson dwg

  • Iceland
  • 05:15 (UTC)
View GitHub Profile
@dwg
dwg / gist:7372744
Last active December 27, 2015 18:49
Dynamic host mailer example for rails
# Base class
class DynamicHostMailer < ActionMailer::Base
def initialize method_name=nil, host, *args
@host = host
super method_name, *args
end
def url_options
# Host should be checked against a relevant whitelist, this is only an example
{ host: @host }
@dwg
dwg / association_primary_key.rb
Created May 12, 2009 11:17
Adds :primary_key option to associations for Rails 2.1.1
module AssociationPrimaryKey
module ActiveRecord
private
def create_has_many_reflection(association_id, options, &extension)
options.assert_valid_keys(
:class_name, :table_name, :foreign_key, :primary_key,
:dependent,
:select, :conditions, :include, :order, :group, :limit, :offset,
:as, :through, :source, :source_type,
@dwg
dwg / flash_helper.rb
Created April 20, 2009 22:39
Helper to embed flash content in a standards compliant, cross browser fashion. Uses the nested objects method (http://alistapart.com/articles/flashembedcagematch).
module FlashHelper
def embed_flash swf, options={}, &block
outer_options = options.slice(:id, :width, :height).merge(:classid=>'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000')
inner_options = options.slice(:width, :height).merge(:type=>'application/x-shockwave-flash', :data=>swf)
params = options[:params] && options[:params].map {|name, value| tag(:param, :name=>name, :value=>value)}.join || ''
if block_given?
embed_flash_with_block swf, outer_options, inner_options, params, &block
else
embed_flash_without_block swf, outer_options, inner_options, params
end
@dwg
dwg / labelling_form_builder.rb
Created February 12, 2009 23:49
Custom form builder that labels elements, handles errors and wraps in appropriate markup
# Drop the following in an initializer and enjoy consistency and less code when marking up forms.
# <% form_for @user do |f| %>
# <%= f.text_field :username %>
# <%= f.check_box :terms_accepted, :label=>'I accept the terms' %>
# <%= f.submit 'Save' %>
# <% end %>
# produces
# <form ...>
# <p>
# <label for="user_username">Username</label><br/>