Skip to content

Instantly share code, notes, and snippets.

View ingemar's full-sized avatar

ingemar ingemar

View GitHub Profile
ingemar / params_for.rb
Created September 1, 2013 12:50
A DRY way to deal with strong parameters in Ruby on Rails 4 together with the excellent gem decent_exposure
module ParamsFor
def params_for(model, *attributes)
return if method_defined? "#{model}_params"
define_method "#{model}_params" do
params.require(model).permit attributes
# in lib/tasks/rspec.rake
# within namespace
namespace :spec do
desc "Run all specs with RCov"'rcov') do |t|
t.pattern = "./spec/**/*_spec.rb"
t.rcov = true
t.rcov_opts = ['-Ispec --exclude', 'rubygems/*,rcov,.gem/*,blueprints.rb,features/*']
ingemar / gist:649270
Created October 27, 2010 15:32
How to structure a ruby class
require 'open-magazine' # requirements
class Article
include Any::Aweseome::Module # includes and extensions
belongs_to :author # relations to other models
attr_accessor :title, :pages # attribute definitions
RESERVED_TITLES = ['Coding Ruby'] # constants...
#table must be a two columned cucumber table
keys = { |key| key.parameterize("_") }
attributes =
attributes = Hash[*attributes]
# stick it in your ~/.pryrc
# use 'xsel' or 'xclip' if you're in a Linux environment
def _cp(obj = Readline::HISTORY.entries[-2], *options)
if obj.respond_to?(:join) && !options.include?(:a)
if options.include?(:s)
obj = { |element| ":#{element.to_s}" }
out = obj.join(", ")
elsif obj.respond_to?(:inspect)
class Article < ActiveRecord::Base
LOGFILE = File.join(RAILS_ROOT, '/log/', "article_#{RAILS_ENV}.log")
def validate
log "was validated!"
def log(*args)
args.size == 1 ? (message = args; severity = :info) : (severity, message = args)