Skip to content

Instantly share code, notes, and snippets.

ingemar ingemar

Block or report user

Report or block ingemar

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ingemar
ingemar / params_for.rb
Created Sep 1, 2013
A DRY way to deal with strong parameters in Ruby on Rails 4 together with the excellent gem decent_exposure
View params_for.rb
module ParamsFor
def params_for(model, *attributes)
return if method_defined? "#{model}_params"
define_method "#{model}_params" do
params.require(model).permit attributes
end
end
end
View rspec.rake
# in lib/tasks/rspec.rake
# within namespace
namespace :spec do
desc "Run all specs with RCov"
RSpec::Core::RakeTask.new('rcov') do |t|
t.pattern = "./spec/**/*_spec.rb"
t.rcov = true
t.rcov_opts = ['-Ispec --exclude', 'rubygems/*,rcov,.gem/*,blueprints.rb,features/*']
end
@ingemar
ingemar / gist:649270
Created Oct 27, 2010
How to structure a ruby class
View gist:649270
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...
View cucumber_rows_to_attributes
#table must be a two columned cucumber table
keys = table.rows_hash.keys.map { |key| key.parameterize("_") }
attributes = keys.zip(table.rows_hash.values).flatten
attributes = Hash[*attributes]
View Copy stuff from IRB to OS X clipboard
# 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 = obj.map { |element| ":#{element.to_s}" }
end
out = obj.join(", ")
elsif obj.respond_to?(:inspect)
View rails_model_with_custom_log_example.rb
class Article < ActiveRecord::Base
LOGFILE = File.join(RAILS_ROOT, '/log/', "article_#{RAILS_ENV}.log")
def validate
log "was validated!"
end
def log(*args)
args.size == 1 ? (message = args; severity = :info) : (severity, message = args)
You can’t perform that action at this time.