Skip to content

Instantly share code, notes, and snippets.

View clemens's full-sized avatar
:octocat:
Getting sh*t done

Clemens Kofler clemens

:octocat:
Getting sh*t done
View GitHub Profile
<ul>
<% @users.each do |user| %>
<li>
<%= user.name %>
<% if (orders = user.orders_in_timespan(@start_date, @end_date)).any? %>
<ul>
<% orders.each do |order| %>
<li><%= order.number %></li>
<% end %>
</ul>
h = { a: 1, b: 2 }
def foo(a:, b:)
[a, b]
end
foo(h) #=> [1, 2]
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|
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.
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
@keyframes animation-1 {
/* whatever */
}
@keyframes animation-2 {
/* whatever */
}
@keyframes animation-3 {
/* whatever */
ALTER TABLE accounts ADD CONSTRAINT valid_sti_type CHECK (type IN ('STIClass1', 'STIClass2'));
class AgencyVisitPolicy
def initialize(user)
@user = user
end
def to_proc
Proc.new { |visit| visit.agency_id.in?([@user.agency_id, nil]) }
end
end
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 August 29, 2015 14:10
Problems without using instance variables to hand data from a Rails controller to a view

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.