Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View ingemar's full-sized avatar
🔥

ingemar ingemar

🔥
View GitHub Profile
@ingemar
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
end
end
end
# 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 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 = table.rows_hash.keys.map { |key| key.parameterize("_") }
attributes = keys.zip(table.rows_hash.values).flatten
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 = obj.map { |element| ":#{element.to_s}" }
end
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!"
end
def log(*args)
args.size == 1 ? (message = args; severity = :info) : (severity, message = args)