Skip to content

Instantly share code, notes, and snippets.

View clemens's full-sized avatar
:octocat:
Getting sh*t done

Clemens Kofler clemens

:octocat:
Getting sh*t done
View GitHub Profile
class DateTimeSelector
include TagHelper
POSITIONS = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
def initialize(date, options={})
@date = date
@options = options
@object = @options.delete(:object)
@object_name = @options.delete(:object_name)
@clemens
clemens / SassMeister-input.scss
Created April 17, 2014 10:28
Generated by SassMeister.com.
// ----
// Sass (v3.2.18)
// Compass (v0.12.4)
// Sass List-Maps (v0.9.7)
// ----
@import "sass-list-maps";
$baseline: 22px;
$category-font-size: 21px;
say -v Cellos 'The Internet is down, down again, down again, oh noes. The Internet is down, oh noes, oh noes, oh noes, oh noes!'
if Rails.env.test?
# inspired by Braintree's original WebhookTestingGateway, but with way more flexibility and complete data
class BraintreeWebhookTestingGateway
class << self
def register_notification(identifier, kind = nil, &builder)
@notifications ||= {}
kind ||= identifier
@notifications[identifier] = [kind, builder]
end
@clemens
clemens / i18n_with_options.html.slim
Created July 8, 2014 18:16
Using Rails' Object#with_options with i18n to scope and DRY up translations
- with_options(:scope => 'foo') do |i18n|
p Some HTML
/ will actually look up the value of the key "foo.bar"
p = i18n.t('bar')
@clemens
clemens / confirmation_reminder.text.erb
Created July 24, 2014 12:35
Example for using with_options for i18n scopes
<%- with_options(:scope => 'devise.mailer.confirmation_reminder') do |i18n| -%>
<%= email_underlined_title(i18n.t('heading')) %>
<%= i18n.t('salutation', :name => @user.first_name) %>
<%= i18n.t('text') %>
<%= confirmation_url(@user, :locale => I18n.locale, :confirmation_token => @user.confirmation_token) %>
-----
@clemens
clemens / invoice.rb
Created July 24, 2014 19:49
invoice number generation per year
class Invoice < ActiveRecord::Base
before_validation :generate_number, on: :create
private
def generate_number
prefix = "R#{Date.today.strftime('%y')}-"
last_order_number_this_year = self.class.where("number LIKE ?", "#{prefix}%").order("number DESC").limit(1).pluck(:number).first
number = last_order_number_this_year ? last_order_number_this_year.match(/\A#{prefix}(\d+)\z/)[1].to_i : 0
@clemens
clemens / delocalize-psych-example.yml
Created August 6, 2014 18:16
Example pulled from the delocalize readme for Ruby 1.9 compatible YAML formatting
en:
number:
format:
separator: '.'
delimiter: ','
precision: 2
date:
input:
formats:
- :default
Object.class_eval do
def deep_clone
Marshal::load(Marshal.dump(self))
end
end
Array.class_eval do
def sum
self.inject(0) { |sum, element| sum += element }
end
url = 'http://example.com/ ?api_token=a-token&username=USERNAME' # => "http://example.com/ ?api_token=a-token&username=USERNAME"
url =~ URI.regexp(%w[http https]) # => 0
URI.parse(url) # => URI::InvalidURIError: bad URI(is not URI?): http://example.com/ ?api_token=a-token&username=USERNAME