Skip to content

Instantly share code, notes, and snippets.

:octocat:
Getting sh*t done

Clemens Kofler clemens

:octocat:
Getting sh*t done
Block or report user

Report or block clemens

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
View kwargs.rb
h = { a: 1, b: 2 }
def foo(a:, b:)
[a, b]
end
foo(h) #=> [1, 2]
View dynamic_operation_methods.rb
module Trailblazer; end
module My; end
class Trailblazer::Operation
def self.inherited(klass)
*mod_names, klass_name = klass.name.split('::')
mod = const_get(mod_names.join('::'))
mod.define_singleton_method(klass_name) do |*args|
View imagemagick-output.txt
tmp[master]% convert logo.jpg -format png -colorspace srgb -profile ../vendor/profiles/sRGBColorSpaceProfile.icm logo.png
tmp[master]% rm logo.png
tmp[master]% cp logo.jpg logo.png
tmp[master]% mogrify -format png -colorspace srgb -profile ../vendor/profiles/sRGBColorSpaceProfile.icm logo.png
mogrify: improper image header `../vendor/profiles/sRGBColorSpaceProfile.icm' @ error/png.c/ReadPNGImage/3981.
View log.txt
2016-08-01T09:24:58.489044+00:00 heroku[router]: at=info method=GET path="/" host=example.com request_id=e8e2f6a9-b50b-4054-8efc-fb35457558e6 fwd="192.168.0.1" dyno=web.1 connect=0ms service=16ms status=302 bytes=1389
View animation.css
@keyframes animation-1 {
/* whatever */
}
@keyframes animation-2 {
/* whatever */
}
@keyframes animation-3 {
/* whatever */
View sti.sql
ALTER TABLE accounts ADD CONSTRAINT valid_sti_type CHECK (type IN ('STIClass1', 'STIClass2'));
View ability_with_callable_policy_object.rb
class AgencyVisitPolicy
def initialize(user)
@user = user
end
def to_proc
Proc.new { |visit| visit.agency_id.in?([@user.agency_id, nil]) }
end
end
View indexes.rb
pp ActiveRecord::Base.connection.select_all("SHOW INDEXES FROM spree_paypal_website_payments_standard_notifications")
=> [
{"Table"=>"spree_paypal_website_payments_standard_notifications",
"Non_unique"=>0,
"Key_name"=>"unique_idx_paypal_standard_notifications_transaction_id",
"Seq_in_index"=>1,
"Column_name"=>"transaction_id",
"Collation"=>"A",
"Cardinality"=>473,
"Sub_part"=>nil,
@clemens
clemens / 1-problem-without-instance-variables.md
Last active Aug 29, 2015
Problems without using instance variables to hand data from a Rails controller to a view
View 1-problem-without-instance-variables.md

This is the first time I've run into an issue like this.

I've been using focused_controller in some projects and in others I've just extracted the expose method (see below). Everything was always fine.

Now in a CRM app I have a CommentsController that handles comments for multiple commentable resources – e.g. a contact. The comment form is currently always displayed on the resource's show page. So in case of errors (e.g. empty comment text), the commentable's show template is the one that needs to be re-rendered in order to properly show error messages. I don't want to do ugly stuff like redirect with error messages in the GET parameters or put something in the session.

As it stands, I found two solutions that actually work and allow me to use contact (as a method call) in the show template:

  • Use a combination of instance_variable_set/get/defined?. Remember: the commentable is essentially polymorphic so I have to set the name dynamically.
@clemens
clemens / calendar_week.rb
Created Nov 24, 2014
A simple class wrapping a calendar week
View calendar_week.rb
class CalendarWeek
attr_reader :week, :year, :date
private :date
def self.current
for_date(Date.today)
end
def self.for_date(date)
new(date.year, date.cweek)
You can’t perform that action at this time.