Skip to content

Instantly share code, notes, and snippets.


Janko Marohnić janko

View GitHub Profile
janko / activerecord.rb
Created Nov 13, 2012
Eliminate need for class eval
View activerecord.rb
# lib/carrierwave/orm/activerecord.rb from jnicklas/carrierwave
# before
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{column}=(new_file)
column = _mounter(:#{column}).serialization_column
janko /
Last active Sep 30, 2018
Java vs Ruby (#1) -- File reading.
// Java
import java.util.BufferedReader;
import java.util.FileReader;
import java.util.IOException;
String content = new String();
try {
BufferedReader reader = new BufferedReader(;
String line;
while ((line = reader.readLine()) != null) {
janko / carrierwave.rb
Created Oct 31, 2013
Paperclip vs CarrierWave
View carrierwave.rb
class ImageQuestion < TextQuestion
store :data, accessors: [:image]
mount_uploader :image, ImageUploader
validates :image, download: true, processing: true
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
janko /
Last active Aug 29, 2015
OpenSSL upgrade instructions

These are upgrading instructions for the new OpenSSL vulnerability, Heartbleed. This is only for Mac, and assumes you have Homebrew installed.


You need to upgrade OpenSSL to at least version 1.0.1g.

$ brew update
$ brew upgrade openssl
$ brew link --force openssl
janko / ldap.rb
Last active Aug 29, 2015
Example Faraday integration with the LDAP protocol, with caching to the database.
View ldap.rb
require "faraday"
module Faraday
class Adapter
class NetLdap < Faraday::Adapter
def call(env)
# LDAP request, and call `save_response(env, status, body, headers)`
janko / application.rb
Created Jun 7, 2014
Custom error pages in Rails
View application.rb
module MyApp
class Application < Rails::Application
# ...
config.exceptions_app = self.routes
"RDS::ResourceNotFound" => :not_found,
# ...
janko / dive.rb
Last active Aug 29, 2015
A script that opens any gem in your EDITOR.
View dive.rb
#!/usr/bin/env ruby
# Opens the lib/ directory of the specified gem in EDITOR. If you're in a Bundler
# project, it will open the locked-down version of the gem.
# You can supply only the first few letters of the gem, and the first gem that
# matches will be opened.
janko / Gemfile
Last active Aug 29, 2015
Specifying dependencies: Java vs Ruby vs Node
View Gemfile
source ""
gem "rspec", "~> 3.1"
gem "rspec-rails", "~> 3.1"
janko / keyword_arguments.rb
Last active Jul 24, 2018
Ruby keyword arguments advantages and use cases
View keyword_arguments.rb
# 1. Simple assertion of required arguments
# - You can forget #fetch
# - You get a better error, and always on the line where method is defined
def search(options = {})
query = options.fetch(:query)
search() # => KeyError: key not found :query
janko / activerecord.rb
Created Apr 4, 2015
Sequel's vritual rows are awesome
View activerecord.rb
"ts_headline(title, to_tsquery('#{query}'), 'StartSel = <strong>, StopSel = </strong>, HighlightAll = true') AS title",
"ts_headline(plot, to_tsquery('#{query}'), 'StartSel = <strong>, StopSel = </strong>, HighlightAll = true') AS plot",
"ts_headline(episode, to_tsquery('#{query}'), 'StartSel = <strong>, StopSel = </strong>, HighlightAll = true') AS episode"