Skip to content

Instantly share code, notes, and snippets.

View stex's full-sized avatar

Stefan Exner stex

View GitHub Profile
@stex
stex / localized_validation_errors.rb
Created April 28, 2014 16:28
Unit-Testing for specific localized error messages
#test/test_helper.rb
#
# Looks up the error message for the given error in the locales
#
def localized_validation_error_message(record, attribute, error_name)
I18n.t("activerecord.errors.models.#{record.class.name.underscore}.attributes.#{attribute}.#{error_name}")
end
#
# @return [Boolean] +true+ if the given error was actually included in the validation errors
@stex
stex / all_helpers.rb
Last active August 29, 2015 13:57
Helper Method to exclude certain helpers from all_application_helpers
# Don't forget to turn auto-loading all helpers off:
# config.action_controller.include_all_helpers = false
class ActionController::Base
class << self
def all_helpers(options = {})
all_helpers_from_path(helpers_path) - Array(options[:except])
end
end
end
@stex
stex / lockable.rb
Created August 13, 2013 15:34
Adds lock/unlock functionality to a Ruby on Rails model using a deleted_at column in the corresponding table. To use it, simply "include Lockable" in your model (Rails 2.3, for Rails 3 change named_scope to scope)
module Lockable
def self.included(base)
base.class_eval do
named_scope :not_locked, :conditions => {:deleted_at => nil}
named_scope :locked, :conditions => "#{table_name}.deleted_at IS NOT NULL"
end
end
# Locks the record by setting the deleted_at attribute
# to the current time.
@stex
stex / rendering.rb
Last active December 20, 2015 22:28
Liquid Rendering Helper for Rails controllers and views. Simply include LiquidHelpers::Rendering in each controller you want liquid rendering to be available. The main method you'll have to change to get this working in your application is `get_template_content` which fetches the actual template content either from disk or database.
#This module contains all necessary methods to render liquid templates
#in a controller.
module LiquidHelpers
module Rendering
def self.included(base)
base.class_eval do
helper_method :render_liquid if respond_to?(:helper_method)
end
@stex
stex / activerecord_date_formats.rb
Last active December 17, 2015 15:09
Allows mass assignment of custom date formats to date columns in ActiveRecord columns
before_validation :ensure_default_date_format
# Parses dates in european date formats before saving a record
# Accepts dates in the format "dd.mm.yyyy" and "yyyy-mm-dd"
#--------------------------------------------------------------
def ensure_default_date_format
self.class.columns.select {|c| c.type == :date}.each do |c|
value_before_type_cast = send("#{c.name}_before_type_cast")
date = Date.try(:parse, value_before_type_cast, :eu) || Date.parse(value_before_type_cast)
write_attribute(c.name, date)
@stex
stex / rails-bootstrap-checkboxes.coffee
Created May 22, 2013 11:48
Converts rails checkboxes to bootstrap buttonsets.
# Example: Weekday Selection
# #weekdays{:data => {:toggle => :checkboxes}}
# %label Weekdays
# - [:mon, :tue, :wed, :thu, :fri, :sat, :sun].each do |wday|
# = f.label wday
# = f.check_box wday
# Creates a bootstrap buttonset and hides the original checkboxes
#--------------------------------------------------------------
initializeCheckboxButtonsets: () ->
@stex
stex / field_error_proc.rb
Created May 8, 2013 11:23
Rails field_error_proc to add an error class to invalid input fields
#Overrides the default way how rails marks form fields with errors.
#In this version, it only adds the class 'error' to label and element
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<(input|label|textarea|select)/
html_field = Nokogiri::HTML::DocumentFragment.parse(html_tag)
html_field.children.add_class 'error'
html_field.to_s
else
html_tag
@stex
stex / examples.tex
Last active December 16, 2015 11:58
Simple LaTeX macro to draw a t(ombstone) diagram for compiler chains.
%Creates an image displaying the compilation of a compiler and the result
\begin{picture}(300,75)
\put(0,25) {\tdiagram(from Q to M_2 in Q)}
\put(65,0) {\tdiagram(from Q to M_1 in M_1)}
\put(130,25) {\tdiagram(from Q to M_2 in M_1)}
\end{picture}
%Creates an image of a compiler chain
\begin{picture}(200,50)
\put(0,0) {\tdiagram(from Q to Z in Q)}
@stex
stex / _breadcrumbs.html.haml
Last active October 11, 2015 05:07
A helper method for creating breadcrumbs based on Models. The output partial adds bootstrap dropdown functionality and generally the bootstrap breadcrumbs view.
%ul.breadcrumb
- breadcrumbs.each do |breadcrumb_item|
%li{:class => breadcrumb_item[:classes].join(' ')}
- if breadcrumb_item[:collection] && breadcrumb_item[:collection].size > 1 #Elements with dropdowns
%a.dropdown-toggle{'data-toggle' => 'dropdown', :href => '#'}
= breadcrumb_item[:caption]
%span.caret
%ul.dropdown-menu
- breadcrumb_item[:collection].shift if breadcrumb_item[:active]
- breadcrumb_item[:collection].each do |bi|