Skip to content

Instantly share code, notes, and snippets.

View janko's full-sized avatar

Janko Marohnić janko

View GitHub Profile
@janko
janko / activerecord.rb
Created November 13, 2012 17:02
Eliminate need for class eval
# lib/carrierwave/orm/activerecord.rb from jnicklas/carrierwave
# before
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{column}=(new_file)
column = _mounter(:#{column}).serialization_column
send(:"\#{column}_will_change!")
super
end
@janko
janko / file_reading.java
Last active September 30, 2018 08:09
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(FileReader.new(filename));
String line;
while ((line = reader.readLine()) != null) {
@janko
janko / carrierwave.rb
Created October 31, 2013 22:03
Paperclip vs CarrierWave
class ImageQuestion < TextQuestion
store :data, accessors: [:image]
mount_uploader :image, ImageUploader
validates :image, download: true, processing: true
end
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
@janko
janko / openssl.md
Last active August 29, 2015 13:58
OpenSSL upgrade instructions

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

OpenSSL

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

$ brew update
$ brew upgrade openssl
$ brew link --force openssl
@janko
janko / ldap.rb
Last active August 29, 2015 14:02
Example Faraday integration with the LDAP protocol, with caching to the database.
require "faraday"
module Faraday
class Adapter
class NetLdap < Faraday::Adapter
def call(env)
# LDAP request, and call `save_response(env, status, body, headers)`
@app.call(env)
end
end
@janko
janko / application.rb
Created June 7, 2014 16:57
Custom error pages in Rails
module MyApp
class Application < Rails::Application
# ...
config.exceptions_app = self.routes
config.action_dispatch.rescue_responses.merge!(
"RDS::ResourceNotFound" => :not_found,
)
# ...
end
@janko
janko / dive.rb
Last active August 29, 2015 14:09
A script that opens any gem in your EDITOR.
#!/usr/bin/env ruby
# USAGE: dive GEM_NAME
#
# 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
janko / Gemfile
Last active August 29, 2015 14:13
Specifying dependencies: Java vs Ruby vs Node
source "https://rubygems.org"
gem "rspec", "~> 3.1"
gem "rspec-rails", "~> 3.1"
@janko
janko / keyword_arguments.rb
Last active July 24, 2018 08:34
Ruby keyword arguments advantages and use cases
####
# 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)
end
search() # => KeyError: key not found :query
@janko
janko / activerecord.rb
Created April 4, 2015 20:53
Sequel's vritual rows are awesome
Movie.select(
:year,
"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"
)