Skip to content

Instantly share code, notes, and snippets.

@yelps
yelps / url_format_validator.rb
Created March 11, 2012 21:46
Rails:Validator: validate url formats with regular expressions
require 'uri'
class UrlFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ URI.regexp(['http'])
object.errors.add(attribute, t('errors.messages.url_format'))
end
end
end
@yelps
yelps / standard_form_validator.rb
Created March 11, 2012 21:26
Rails:Validator: validate standard form with regular expressions
class NameFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /^([^\d\s\<\>,;\.:\-_\@#\'\€\+\*°\^!\"§\$%&\/\|\\\(\)\[\]=\?\{\}]{1}[^\d\<\>,;:_\@#\€\+\*°\^!\"§\$%\/\|\\\(\)\[\]=\?\{\}]{1,})$/
object.errors.add(attribute, t('errors.messages.name_format'))
end
end
end
class StreetNumberFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
@yelps
yelps / application_helper.rb
Created March 11, 2012 19:32
Rails:Devise: customize display of field with errors
def field_with_errors(object, method, &block)
if block_given?
if object.errors[method].empty?
content = with_output_buffer(&block)
content.html_safe
else
content = with_output_buffer(&block)
content_tag(:span, content, :class => "field_with_errors")
end
end
@yelps
yelps / application.rb
Created March 11, 2012 19:26
Rails:Devise: no red borders for textareas with field errors
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<textarea/
html_tag
else
"<span class=\"field_with_errors\">#{html_tag}</span>".html_safe
end
end
@yelps
yelps / sessions_controller.rb
Created March 11, 2012 19:00
Rails:Devise: customize session handling
def new
resource = build_resource
clean_up_passwords(resource)
if request.xhr?
render :partial => "/#{resource_name.to_s.pluralize}/sessions/new"
else
respond_with_navigational(resource, stub_options(resource)){ render_with_scope :new }
end
end
@yelps
yelps / application_controller.rb
Created March 11, 2012 18:57
Rails:force_ssl: override max age of hsts header
private
# Sets header to remove HTTPS force
# Needed if we once set config.force_ssl = true (TTL: 1year)
# (https://github.com/josh/rack-ssl/blob/master/lib/rack/ssl.rb)
# Note: Setting this outside HTTPS has no effect
def set_hsts_header
if request.ssl?
response.headers["Strict-Transport-Security"] = 'max-age=3600'
end
@yelps
yelps / application_controller.rb
Created March 11, 2012 18:54
Rails:Devise: resource specfic redirect after login
def after_sign_in_path_for(resource)
if resource.is_a?(User)
user_path(resource.id)
else
:home_index
end
end
@yelps
yelps / application_controller.rb
Created March 11, 2012 18:48
Rails:force`ssl: force non ssl
class << self
def force_ssl(options = {})
host = options.delete(:host)
before_filter(options) do
if !request.ssl?
redirect_options = {:protocol => 'https://', :host => request.host, :status => :moved_permanently}
redirect_options.merge!(:host => host) if host
redirect_to "#{redirect_options[:protocol]}#{redirect_options[:host]}#{request.fullpath}", :status => redirect_options[:status]
end
end
@yelps
yelps / collections.rb
Created March 11, 2012 15:54
Ruby:Extension: Hash to OrderedHash
class Hash
def to_sorted_array
self.stringify_keys!
self.sort{|a,b| a[1] <=> b[1]}
end
def to_ordered_hash
self.stringify_keys!
ActiveSupport::OrderedHash[self.to_sorted_array]