Skip to content

Instantly share code, notes, and snippets.

require 'cgi'
require 'active_support'
def verify_and_decrypt_session_cookie(cookie, secret_key_base = Rails.application.secret_key_base)
config = Rails.application.config
cookie = CGI::unescape(cookie)
salt = config.action_dispatch.authenticated_encrypted_cookie_salt
encrypted_cookie_cipher = config.action_dispatch.encrypted_cookie_cipher || 'aes-256-gcm'
# serializer = ActiveSupport::MessageEncryptor::NullSerializer # use this line if you don't know your serializer
serializer = ActionDispatch::Cookies::JsonSerializer
@benfyvie
benfyvie / generic_validations.rb
Created June 10, 2011 17:23
automatically add generic validations to all models
module ActiveRecord::Validations::ClassMethods
#inspired by rails-schema_validations
#https://github.com/gregwebs/rails-schema-validations
def add_generic_validations
begin
self.columns.map do |column|
case column.type
when :date
validates_format_of_date(column.name) #this is a custom validation we defined (not part of rails)
end