Skip to content

Instantly share code, notes, and snippets.

@skojin
skojin / gist:205971
Created October 9, 2009 12:25
prefix validation message with ^ to display it without attribute name
# prefix validation message with ^ to display it without attribute name
# original idea: http://speakmy.name/2009/01/custom-validation-errors-in-rails-and-activerecord/
class ActiveRecord::Error
CUSTOM_PREFIX = '^'
def full_message_with_custom_prefix
if @message.is_a?(String) && @message.mb_chars[0..(CUSTOM_PREFIX.size-1)] == CUSTOM_PREFIX
@message.mb_chars[(CUSTOM_PREFIX.size)..-1]
else
full_message_without_custom_prefix
end
@skojin
skojin / rescue_from_validation_error.rb
Created October 22, 2009 15:59
way to handle validation in controller
# implement alternative way to handle form validation error, via rescue_from ActiveRecord::RecordInvalid
# use Record.create! and @record.update_attributes!
module RescueFromValidationError
# usage: rescue_from_validation_error('@user')
def rescue_from_validation_error(instance_variable_name, options = {})
rescue_from ActiveRecord::RecordInvalid, :with => :handle_validation_error
define_method(:handle_validation_error) do |e|
instance_variable_set(instance_variable_name, e.record)
@skojin
skojin / cache
Created February 3, 2010 19:31
apache rewrite rules for rails cache_page in /public/cache directory
RewriteEngine On
# home page cache
RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/([^.]+)$
RewriteCond %{DOCUMENT_ROOT}/cache/%1.html -f
RewriteRule ^/[^.]+$ /cache/%1.html [QSA,L]
# general pages cache
@skojin
skojin / gist:315760
Created February 26, 2010 14:43
set expire header for images/assets that have version info in query string, apache conf
# mark versioned assets
RewriteCond %{REQUEST_URI} ^/(images|stylesheets|javascripts)/.*\.(css|js|jpe?g|png|gif|ico)$
RewriteCond %{QUERY_STRING} ^[0-9]+$
RewriteRule ^.*$ $0 [E=IS_VERSIONED_ASSET:true,L]
# expire them in future
Header onsuccess set "Cache-Control" "max-age=31536000" env=IS_VERSIONED_ASSET
@skojin
skojin / formatted_date_attribute.rb
Created March 9, 2010 17:06
virtual formatted date attribute
# inspired by http://railsforum.com/viewtopic.php?pid=6676#p6676
module FormattedDateAttribute
# define virtual formatted date attribute
# formatted_date_attribute(:start_on, :formatted_start_on, :us_format)
# formatted_date_attribute(:start_on, :formatted_start_on, "%m/%d/%Y")
def formatted_date_attribute(attr_name, formatted_name, format)
define_method(formatted_name) do
return self[attr_name] unless self[attr_name]
format.is_a?(Symbol) ? self[attr_name].to_s(format) : self[attr_name].strftime(format)
end
@skojin
skojin / toggle cookie with javascript
Created April 21, 2010 13:28
toggle session cookie with javascript
# recaptcha gem fail when load as ajax, do this in right way
module RecaptchaHelper
def ajax_recaptcha_script_url(options = {})
uri = options[:ssl] ? Recaptcha::RECAPTCHA_API_SECURE_SERVER : Recaptcha::RECAPTCHA_API_SERVER
"#{uri}/js/recaptcha_ajax.js"
end
# Your public API can be specified in the +options+ hash or preferably
# the environment variable +RECAPTCHA_PUBLIC_KEY+.
def ajax_recaptcha_tags(options = {})
# rails 3 like, default scopes
module Rails3Scopes
def self.included(base)
base.named_scope :where, lambda{ |conditions| {:conditions => conditions} }
base.named_scope :order, lambda{ |order| {:order => order} }
base.named_scope :limit, lambda{ |n| {:limit => n} }
base.named_scope :joins, lambda{ |j| {:joins => j} }
end
end
class BitLy
include HTTParty
base_uri "http://api.bit.ly"
default_params 'login' => 'bitlyapidemo', 'apiKey' => 'R_0da49e0a9118ff35f52f629d2d71bf07'
format :json
def self.shorten(url)
r = get('/v3/shorten', :query => {'longUrl' => url} )
r['data']['url'] rescue nil
end
@skojin
skojin / strip_attributes.rb
Created October 22, 2010 11:16
simple active record/model extension to strip attributes
module StripAttributes
def strip_attributes!(*attrs)
attrs.map(&:to_s)
before_validation do |r|
attrs.each do |v|
r[v].strip! if r[v]
end
end
end
end