Skip to content

Instantly share code, notes, and snippets.

View lmmendes's full-sized avatar
🏠

Luís Mendes lmmendes

🏠
View GitHub Profile
@lmmendes
lmmendes / rails_flash_messages_helper.rb
Created March 23, 2011 17:22
Rails flash messages helper
def flash_messages(options={})
flash_names = [:success, :warning, :error, :information, :tip]
html = ''
flash_names.each do |flash_type|
html += flash_message_for( flash[flash_type], options ) if flash[flash_type]
end
raw html
end
def flash_message_for(message, options={})
@lmmendes
lmmendes / try_method.rb
Last active February 22, 2016 19:01
try method à la Rails
def try(method, *args, &block)
__send__(method.to_sym, *args, %block) if responds_to?(method.to_sym, true)
end
@lmmendes
lmmendes / contacts.rb
Created March 25, 2011 15:44
A enhanced version of a Rails 2 Tableless model
# Demonstration
class Contact < Tabless
column :name, :string
column :address, :string
end
@lmmendes
lmmendes / tabless.rb
Created March 25, 2011 16:42
Rails 3 tableless model implementation
# based on a Ryan Bates article http://railscasts.com/episodes/219-active-model
class Tableless
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
def self.attr_accessor(*vars)
@attributes ||= []
@lmmendes
lmmendes / worst_errors.gemspec
Created February 18, 2014 14:39
worst_errors
Gem::Specification.new do |s|
s.name = 'worst_errors'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.author = 'Luís Mendes'
s.email = 'lmmendes@gmail.com'
s.summary = 'Worst Errors!'
s.description = 'Worst Errors a more elequent name for better errors'
s.files = []
require 'openssl'
def gen_key(name)
key = OpenSSL::PKey::RSA.new 1048
file = File.new(name, "w")
file.write(key)
file.close
end
def get_key(name)
@lmmendes
lmmendes / fake_email_validator.rb
Created May 13, 2014 14:58
Fake / Temp e-mail validator
# encoding: utf-8
# Validates that the specified attributes are fake e-mail from temp domains
#
# class Person < ActiveRecord::Base
# validates :email, :fake_email => true
# end
#
# class Person < ActiveRecord::Base
# validates :email, :fake_email => { :exclude => ['bobmail.info'] }
@lmmendes
lmmendes / email_validator.rb
Created May 13, 2014 15:12
Simple email validator for ActiveModel
# encoding: utf-8
class EmailValidator < ActiveModel::EachValidator
# Validates that the specified attribute is a valid e-mail address
#
# class Person < ActiveRecord::Base
# validates :email, :email => true
# end
#
@lmmendes
lmmendes / backend.conf
Last active August 29, 2015 14:10
nginx configuration using cloudflare ip and nginx with-http_realip module
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:/servers/sites/backend-saas/shared/tmp/sockets/.unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name passworks.io;
return 301 $scheme://www.passworks.io$request_uri;
}
@lmmendes
lmmendes / 003-mongo-retry.rb
Created May 24, 2015 10:03
Monkey patch for Mongoid 4.x "Could not connect to a primary node for replica set"
#==================================================================================================
# Monkey Patch (lmmendes)
# MongoDB or in this case Mongoid and it's driver Moped have problems running some commands
# when the Primary node goes down and Moped tries to write to the database before
# refreshing the cluster info or at least trying the same command on each node before failing.
# From GitHub:
# Moped::Errors::ConnectionFailure: Could not connect to a primary node for replica set
# https://github.com/mongoid/moped/issues/348
#==================================================================================================