Skip to content

Instantly share code, notes, and snippets.

@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
@tlux
tlux / flcodec.rb
Last active August 29, 2015 14:07
Freelancer Savegame Encoder/Decoder
# This script is based on the C++ implementation by Sherlog <sherlog@t-online.de> and Jor <flcodec@jors.net>
require 'active_support/all'
require 'singleton'
class FLCodec
include Singleton
class << self
delegate :encode, :decode, :load_file, :decode_file, :encode_file, to: :instance
@tlux
tlux / flhash.rb
Last active August 29, 2015 14:08
Freelancer Hash Encoder
require 'active_support/all'
require 'singleton'
class FLHash
include Singleton
FLHASH_POLYNOMIAL = 0xa001;
LOGICAL_BITS = 30;
PHYSICAL_BITS = 32;