Skip to content

Instantly share code, notes, and snippets.

View janko's full-sized avatar

Janko Marohnić janko

View GitHub Profile
@janko
janko / benchmark.rb
Last active August 29, 2015 14:18
Minitest loads *slower* than RSpec?
require "benchmark/ips"
File.write "minitest_test.rb", <<-EOS
require "minitest/autorun"
require "minitest/pride"
class MintestTest < Minitest::Test
def test_foo
assert true
end
@janko
janko / 01-intro.md
Last active March 20, 2016 21:02
Yaks demonstration on the local Ruby meetup

API

  • You sometimes need to create an API

    • You want frontend and backend separate
    • You need REST API alongside the web app (GitHub, DNSimple, Travis)
  • JSON API specifications

@janko
janko / benchmark.rb
Created May 19, 2015 23:17
Serialization performance
gem "active_model_serializers", "0.10.0.rc1"
require "yaks"
require "active_record"
require "active_model_serializers"
require "benchmark/ips"
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "testing")
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
@janko
janko / 1-models.rb
Created May 27, 2015 17:50
Components of good application design (my presentation for a local Ruby meetup)
# 1. Models (Entities)
#
# * The nouns of your business logic
# * Usually persisted in the database
# * Usually contain validations
# * Usually expose associations with other models
# * Usually have scopes (to encapsulate the query logic)
################
# ActiveRecord #
@janko
janko / 01-classic.rb
Created May 27, 2015 22:38
Controller refactoring (my presentation at a local Ruby meetup)
#################
# CLASSIC STYLE #
#################
class SessionsController < ApplicationController
# ...
def create
if user = AuthenticateUser.call(params[:username], params[:password])
sign_in!(user)
@janko
janko / 01-before.rb
Created May 27, 2015 22:41
Double dispatch in Ruby example (my presentation at a local Ruby meetup)
module SocialPresenter
# ...
class Twitter < Struct.new(:object)
def message
case object
when String
@janko
janko / 01-intro.md
Last active August 29, 2015 14:22
Dynamic eager loading in JSON APIs (my presentation from our local Ruby meetups)

Dynamic eager loading

  • Static: User.includes(:posts => :comments)

  • Dynamic: :posts => :comments is a product of user's input

Where did I need this?

@janko
janko / 01-activerecord.rb
Created May 27, 2015 22:50
PostgreSQL JSON querying in Sequel (my presentation from our local Ruby meetup)
require "active_record"
ActiveRecord::Base.establish_connection('postgres:///testing')
ActiveRecord::Migration.verbose = false
ActiveRecord::Migration.class_eval do
create_table :played_quizzes, force: true do |t|
t.integer :player_ids, array: true
t.json :quiz_snapshot
end
@janko
janko / 01.rb
Created May 27, 2015 22:54
Capabilities of the "mail" gem, without ActionMailer (my presentation from our local Ruby meetups)
require "mail"
Mail.defaults do
delivery_method :smtp,
address: "smtp.gmail.com",
port: 587,
user_name: ENV["GMAIL_EMAIL"],
password: ENV["GMAIL_PASSWORD"],
authentication: "plain",
end
@janko
janko / 01-presentation.md
Last active June 6, 2019 23:58
PostgreSQL full-text search capabilites (my presentation from our local Ruby meetup)

Full-text search

  • Keywords

  • Typos

  • Stemming

  • Stopword ignore