Skip to content

Instantly share code, notes, and snippets.

Avatar

Nils Sommer nsommer

View GitHub Profile
View papertail-versioning.rb
# Gemfile
gem 'paper_trail'
# app/models/my_model.rb
class MyModel < ApplicationRecord
has_paper_trail
end
View medium-post-scope-1.rb
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
@nsommer
nsommer / .bash_profile
Created Apr 29, 2018
Bash prompt that displays current git branch and directory
View .bash_profile
# 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
@nsommer
nsommer / local_date_time_attr_readers.rb
Last active Feb 8, 2018
Concern that adds _local attr methods to convert times to a given time zone on a per user basis
View local_date_time_attr_readers.rb
# 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 / time_zone_validator.rb
Created Feb 7, 2018
ActiveModel Validator that checks whether the string value of an attribute is a valid ActiveSupport::TimeZone
View time_zone_validator.rb
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 / lstdefinelanguage-gherkin.tex
Created Sep 1, 2017
Gherkin syntax highlighting for LaTeX
View lstdefinelanguage-gherkin.tex
\lstdefinelanguage{Gherkin}{
morekeywords = {
Given,
When,
Then,
And,
Scenario,
Feature,
But,
Background,
@nsommer
nsommer / alphadin.bst
Created Aug 24, 2017
Addition to alphadin.bst that enables overriding of labels by the key attribute of BibTeX entries.
View alphadin.bst
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 / brew-coffee
Created Nov 30, 2016
Brews as much ☕️ as you want it to 🙂
View brew-coffee
#!/usr/bin/env ruby
if ARGV.length == 0
puts "☕️"
else
puts "☕️" * ARGV.first.to_i
end
@nsommer
nsommer / .bash_profile
Created Jul 24, 2016
Hack to enable `npm exec` command as bash alias, inspired by `bundle exec`
View .bash_profile
# 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 / bower.rake
Created Oct 8, 2015
Bower integration for capistrano deployments
View bower.rake
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
You can’t perform that action at this time.