This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
listen 80 default_server; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $host; | |
location / { | |
proxy_pass http://desthost.com; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 = {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer