Skip to content

Instantly share code, notes, and snippets.

Janko Marohnić janko

Block or report user

Report or block janko

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
@janko
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
send(:"\#{column}_will_change!")
super
end
@janko
janko / file_reading.java
Last active Sep 30, 2018
Java vs Ruby (#1) -- File reading.
View file_reading.java
// 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 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
end
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
@janko
janko / openssl.md
Last active Aug 29, 2015
OpenSSL upgrade instructions
View openssl.md

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 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)`
@app.call(env)
end
end
@janko
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
config.action_dispatch.rescue_responses.merge!(
"RDS::ResourceNotFound" => :not_found,
)
# ...
end
@janko
janko / dive.rb
Last active Aug 29, 2015
A script that opens any gem in your EDITOR.
View dive.rb
#!/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 Aug 29, 2015
Specifying dependencies: Java vs Ruby vs Node
View Gemfile
source "https://rubygems.org"
gem "rspec", "~> 3.1"
gem "rspec-rails", "~> 3.1"
@janko
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)
end
search() # => KeyError: key not found :query
@janko
janko / activerecord.rb
Created Apr 4, 2015
Sequel's vritual rows are awesome
View activerecord.rb
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"
)
You can’t perform that action at this time.