Skip to content

Instantly share code, notes, and snippets.

View nsommer's full-sized avatar

Nils Sommer nsommer

View GitHub Profile
@nsommer
nsommer / bower.rake
Created October 8, 2015 17:09
Bower integration for capistrano deployments
namespace :bower do
%w[install list].each do |command|
desc "#{command} bower packages"
task command do
on roles(:web) do
within release_path do
execute 'bower', 'install'
end
end
end
@nsommer
nsommer / .bash_profile
Created July 24, 2016 14:44
Hack to enable `npm exec` command as bash alias, inspired by `bundle exec`
# Shim the npm binary to enable custom command `npm exec`.
npm_shim() {
# Make real npm calls with full path, otherwise the alias is called recursively.
npm_binary=$(which npm)
if [ $# -eq 0 ]
then
$npm_binary
elif [ $# -eq 1 ]
then
@nsommer
nsommer / brew-coffee
Created November 30, 2016 10:01
Brews as much ☕️ as you want it to 🙂
#!/usr/bin/env ruby
if ARGV.length == 0
puts "☕️"
else
puts "☕️" * ARGV.first.to_i
end
@nsommer
nsommer / alphadin.bst
Created August 24, 2017 14:26
Addition to alphadin.bst that enables overriding of labels by the key attribute of BibTeX entries.
FUNCTION {key.label}
{ key #20 text.prefix$ } %% Length of 20 chars should be sufficient
FUNCTION {calc.label}
{ type$ "book" =
type$ "booklet" =
type$ "inbook" =
or or
'author.editor.key.label
{ type$ "proceedings" =
@nsommer
nsommer / lstdefinelanguage-gherkin.tex
Created September 1, 2017 09:50
Gherkin syntax highlighting for LaTeX
\lstdefinelanguage{Gherkin}{
morekeywords = {
Given,
When,
Then,
And,
Scenario,
Feature,
But,
Background,
@nsommer
nsommer / time_zone_validator.rb
Created February 7, 2018 17:28
ActiveModel Validator that checks whether the string value of an attribute is a valid ActiveSupport::TimeZone
class TimeZoneValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless ActiveSupport::TimeZone[value]
record.errors[attribute] << (options[:message] || 'is not a valid time zone!')
end
end
end
# Usage with rails:
#
@nsommer
nsommer / local_date_time_attr_readers.rb
Last active November 10, 2021 22:24
Concern that adds _local attr methods to convert times to a given time zone on a per user basis
# ActiveRecord stores timestamps in UTC and converts them into the application's time zone
# (see config.time_zone in config/application.rb).
#
# This module provides a simple to use API to automatically generate '_local' suffix methods
# for model attribute readers that convert dates and times according to a per model time zone.
module LocalDateTimeAttrReaders
extend ActiveSupport::Concern
included do
include ActiveModel::AttributeMethods
@nsommer
nsommer / .bash_profile
Created April 29, 2018 16:05
Bash prompt that displays current git branch and directory
# Get the Git branch
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# Custom bash prompt
#
# Includes custom character for the prompt, path, and Git branch name.
#
# Source: kirsle.net/wizards/ps1.html
class Article < ApplicationRecord
has_many :comments
has_many :tags
scope :draft, -> { where(published_at: nil) }
scope :published, -> { where.not(published_at: nil) }
scope :commented, -> { joins(:comments).distinct }
end
Article.published.count
# Gemfile
gem 'paper_trail'
# app/models/my_model.rb
class MyModel < ApplicationRecord
has_paper_trail
end