Skip to content

Instantly share code, notes, and snippets.

View suweller's full-sized avatar
🕺

Steven Weller suweller

🕺
View GitHub Profile
@suweller
suweller / menu_helper.rb
Last active August 29, 2015 13:59
Display menu items based on role-based access control
# Given an app with Role-based access control (for example cancan)
module MenuHelper
def menu_item(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
klass = args.pop
return nil unless can?(:index, klass)
args << klass.model_name.route_key
path = "/#{args.join('/')}"
content_tag(:li, class: ('active' if request.path.starts_with?(path))) do
@suweller
suweller / keybase.md
Created March 27, 2014 08:57
keybase.md

Keybase proof

I hereby claim:

  • I am suweller on github.
  • I am suweller (https://keybase.io/suweller) on keybase.
  • I have a public key whose fingerprint is A0A2 7C72 616A 1DCB DA01 1E33 B373 EBDA 3CD8 CADC

To claim this, I am signing this object:

@suweller
suweller / Emoji-etiquette.md
Last active December 25, 2022 23:20
Bring flavour to your pull-requests using Emoji!

Whitespace fish - 🐟

fish

The ultimate humiliation. Given for not adhering to whitespace guidelines.

Emoji cop - 👮

cop

For using the wrong Emoij

@suweller
suweller / inherit_from_matcher.rb
Created June 26, 2012 13:50
Rspec - Inheritance matcher
RSpec::Matchers.define :inherit_from do |superclass|
match do |klass|
klass.class.ancestors.include? superclass
end
failure_message_for_should do |klass|
"expected #{klass.class.name} to inherit from #{superclass}"
end
failure_message_for_should_not do |klass|
@suweller
suweller / data-toggle.coffee
Last active June 29, 2017 18:46
Show or hide elements in a form using data-show or data-hide
class window.DataToggle
get: (str) ->
$("##{str}")
ids: (type, e) ->
$(e).attr("data-#{type}").split(' ')
constructor: ->
_self = @
_self.data_show(_self, $(e).parent()) for e in $('[data-show]')
_self.data_hide(_self, $(e).parent()) for e in $('[data-hide]')
@suweller
suweller / raise SuicidalMissingTranslationData on I18n.translate.rb
Created March 5, 2010 15:33
Suicidal Missing Translation Exceptions.
module I18n
class SuicidalMissingTranslationData < RuntimeError; end
class << self
def translate_with_suicidal_exceptions(*args)
translate_without_suicidal_exceptions(*args)
rescue I18n::MissingTranslationData => e
if %w(test cucumber).include? ENV['RAILS_ENV']
raise I18n::SuicidalMissingTranslationData, e
elsif %(staging production).include? ENV['RAILS_ENV']
module ActionView
module Helpers
module TranslationHelper
# Override translate method to consider translate_scope.
def translate(key, options = {})
I18n.translate(key, {:raise => I18n.raise_on_missing, :scope => (translate_scope.dup << options.delete(:add_scope)).compact}.merge(options))
end
alias :t :translate
# Append scopes to the current translation scope.