Just dumping this here in case someone runs into the same mess…
if ActiveSupport::VERSION::MAJOR == 3
ActiveSupport::SafeBuffer.class_eval do
def to_yaml(*args)
to_str.to_yaml(*args)
end
end
end
Just dumping this here in case someone runs into the same mess…
if ActiveSupport::VERSION::MAJOR == 3
ActiveSupport::SafeBuffer.class_eval do
def to_yaml(*args)
to_str.to_yaml(*args)
end
end
end
Usage
# fast response via precompiled parts
result = user.to_json
result["tickets"] = cache { CompiledJson.new(tickets.to_json) }
render :json => result
# handlebar templates or other crazyness
result = user.to_json
result["tickets"] = CompiledJson.new(
'function(c){ return "foo" + c.name + "bar" }'
)
render :json => result
code
class CompiledJson < String
# activesupport
def encode_json(*args)
self
end
# pure JSON
def to_json
self
end
end
Code
Usage
Deploy using the deploy user and also log who deploys using the original user.
Retaining the color was tricky but script fakes tty so we can keep all the color glory and with sed we strip colors before logging them.
# /usr/bin/capsu
function log {
old_IFS=$IFS
IFS='' # do not split on newline when reading stdin
newline=$'\n'
line=""
while read -d '' -n1 c # read every character
do
# print every character as it comes in for cap shell and password prompts
printf "%s" "$c"
# amend complete line with current user (but without color codes) to log
# so multiple people can run capsu in parallel
if [ "$c" = $newline ]; then
echo "$SUDO_USER: $line" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" >> $1
line=""
else
line+=$c
fi
done
IFS=$old_IFS
}
rvmsudo -u deploy script /dev/null -c "bundle exec cap $@" 2>&1 | log deploy cap.log
ActiveRecord loads the xxx_type in your model, making it blow up when doing includes / using the belongs_to on a missing type.
So we make it un-missing.
Usage
class Waldo < MissingType end
Code
class MissingType < ActiveRecord::Base
default_scope :conditions => "1 = 2", :limit => 0
self.table_name = "schema_migrations"
def self.primary_key
"version"
end
def readonly?
true
end
end