Skip to content

Instantly share code, notes, and snippets.

@tlux
tlux / sanitizable.rb
Last active August 29, 2015 13:56
Sanitizable Concern to sanitize ActiveRecord attributes the way you like. This implementation has been converted to a Gem, which is available here: https://github.com/chilian/acts_as_sanitizable
module Sanitizable
extend ActiveSupport::Concern
module ClassMethods
# Usage
# sanitizes :content # strips content by default
# sanitizes :content, with: :squish
# sanitizes :content, with: :presence
# Chain multiple sanitizer methods, they are executed in the order they are defined
module Archivable
extend ActiveSupport::Concern
included do
default_scope -> { where(archived_at: nil) }
scope :archived, -> { where.not(archived_at: nil) }
end
def archived?
!archived_at.nil?
@tlux
tlux / boolify_extension.rb
Last active August 29, 2015 13:57
Rails: Implementation of Object#to_b to convert any value to Boolean in Rails (uses ActiveRecord Boolean Mappings)
class Object
def to_b
self.in?(ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES)
end
alias_method :to_bool, :to_b
end
@tlux
tlux / presenter.rb
Last active August 29, 2015 13:58
Presenter Pattern on Rails, a lightweight alternative for Draper (https://github.com/drapergem/draper)
class Presenter
class_attribute :presenter_options, instance_accessor: false
self.presenter_options ||= {}
attr_reader :object, :template, :options
def initialize(object, template, options = {})
@object = object
@template = template
@options = options.reverse_merge(self.class.presenter_options)
@tlux
tlux / enumerable_group_by_extension.rb
Created April 24, 2014 12:32
Enumerable#group_by_first and Enumerable#group_by_last do in contrast du Enumerable#group_by not return an Array on the value side of the hash but only a single element.
module Enumerable
def group_by_first
inject({}) do |hash, element|
key = yield(element)
hash[key] ||= element
hash
end
end
def group_by_last(&block)
@tlux
tlux / gravatar.rb
Last active August 29, 2015 14:00
Gravatar Image Tag Helper for your Rails Application
class Gravatar
DEFAULTS = {
default: :not_found,
force_default: false,
size: 100,
secure: false
}.freeze
INSECURE_URL = 'http://www.gravatar.com/avatar/%{hash}'
SECURE_URL = 'https://secure.gravatar.com/avatar/%{hash}'
VALID_OPTIONS = :size, :default, :rating, :secure, :force_default
@tlux
tlux / icon.rb
Created May 6, 2014 08:25
Font Awesome Icon Helper
class FontAwesome::Icon < FontAwesome::Iconish
VALID_OPTIONS = :shape, :direction, :outline, :spin, :size, :rotate, :flip, :inverse, :border, :fixed_width
attr_reader :name
def initialize(name, options = {})
@name = name
@html_options = options.except(*VALID_OPTIONS)
@options = options.slice(*VALID_OPTIONS).reverse_merge(
border: false,
@tlux
tlux / try_first.rb
Last active August 29, 2015 14:01
Object#try_first returns the result of the first responding method specified in an Array
# Usage:
#
# object = MyObject.new
# object.label = "Some content here..."
#
# object.try_first([:name, :label, :caption]) # => "Some content here..."
# object.try_first(:description) # => nil
class Object
def try_first(method_names, *args, &block)
@tlux
tlux / i18n.js.coffee
Last active August 29, 2015 14:01
I18n power for your JavaScript
window.I18n ||= {}
VALID_OPTIONS = ['scope', 'locale', 'default', 'fallbacks']
humanizeKeypath = (keypath) ->
console.warn "I18n: no translation found for #{keypath}"
_(keypath.split('.')).chain().last().humanize().value()
sanitizeKeypath = (keypath) ->
str = ""
@tlux
tlux / is_one_of.rb
Created June 4, 2014 14:35
Check if an object matches one of the specified classes
class Object
def is_one_of?(*classes)
classes.any? { |klass| self.is_a?(klass) }
end
end