Skip to content

Instantly share code, notes, and snippets.

View thomasklemm's full-sized avatar

Thomas Klemm thomasklemm

View GitHub Profile
@thomasklemm
thomasklemm / cast_vote_planning.rb
Created April 2, 2016 00:21
Planning the vote casting for GoVote
# config/routes.rb
# "Cast a Vote"-Workflow
get 'vote', to: 'cast_votes#get_voter_token', as: :new_vote
post 'vote', to: 'cast_votes#verify_voter_token'
get 'cast_vote', to: 'cast_votes#get_choices', as: :cast_vote
post 'cast_vote', to: 'cast_votes#cast_vote'
get 'thank_you', to: 'cast_votes#thank_you', as: :thank_you
class CastVotesController < ApplicationController
@thomasklemm
thomasklemm / planning-go-vote.rb
Last active March 31, 2016 12:45
Planning the models and database structure for go.vote
Planning the database structure for go.vote
# Model structure
The main models are:
- Election
- Candidate
- Voter
- Vote
The models are associated as follows:
@thomasklemm
thomasklemm / postgres_set_case.sql
Created June 9, 2015 16:18
Postgres SET using a CASE statement
UPDATE media_images
SET collectively_manage_publication_for_all_locales = (
CASE
WHEN ((published_de = published_en) AND (published_from_de = published_from_en))
THEN TRUE
ELSE FALSE
END
)
@thomasklemm
thomasklemm / calendar_week.rb
Created October 29, 2013 22:02
Calendar week calculations
require 'active_support/core_ext'
def calendar_week(week)
now = Date.current
year = now.cweek < week ? now.year : now.year + 1
Date.commercial(year, week, 1)
end
p calendar_week(49)
# => Mon, 02 Dec 2013
@thomasklemm
thomasklemm / bootstrap-forms.sass
Created October 5, 2013 14:08
Style a basic Rails form with Twitter Bootstrap 3.0
form
.field
@extend .form-group
input[type=text], textarea, input[type=email], input[type=search], input[type=password]
@extend .form-control
label
@extend .control-label
.field .field_with_errors
@extend .has-error
@thomasklemm
thomasklemm / formtastic-custom.sass
Created January 4, 2013 12:25
Formtastic Custom - Base Stylesheet transformed to SASS
// Formtastic stylesheet based on the gem's version, retrieved 2013-01-04
// This version reflects the gem exactly.
// Use as a base for modifications.
.formtastic
margin: 0
padding: 0
// resets
ul, ol, li, fieldset, legend, input, button, textarea, select, p
@thomasklemm
thomasklemm / toc_ruby.rb
Created December 5, 2012 20:27
Table of Contents - Structured Layout in Ruby
##
# A ruby-powered table of contents layout
words = %w(we are the world lets make it a better place we are the world)
words.each_with_index {|word, page| puts word.ljust(10, '.') + (page*page).to_s.rjust(3, '.')}
# => Output
# we..........0
# are.........1
@thomasklemm
thomasklemm / range_operations.rb
Created October 30, 2012 14:01
Operations with Ranges
class Range
def end_at(x)
Range.new(self.begin, x)
end
def start_at(x)
Range.new(x, self.end)
end
end
@thomasklemm
thomasklemm / array_of_arrays_to_hash.rb
Created September 15, 2012 17:07
Array of arrays to hash conversion
##
# Array of arrays to hash conversion
#
##
# Version 1
array = [[1, 1], [2, 2], [3, 3]]
hash = Hash[*array.flatten]
@thomasklemm
thomasklemm / speaking_timer.rb
Created September 15, 2012 16:40
Speaking Timer from my training
# encoding: utf-8
require 'talks'
class Hash
##
# Cascading Fetch
#
# Tries to fetch provided symbols one after the other
# and return fallback non-symbol value otherwise