Skip to content

Instantly share code, notes, and snippets.

View janko's full-sized avatar

Janko Marohnić janko

View GitHub Profile
@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-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-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 / 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 / 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 / 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
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 / 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"
)
@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 / 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"