Skip to content

Instantly share code, notes, and snippets.

View wojtha's full-sized avatar

Vojtěch Kusý wojtha

View GitHub Profile
@wojtha
wojtha / 00_active_support.rb
Last active November 1, 2017 23:19
Make Rails test.log more useful
# Based on https://ilikestuffblog.com/2008/06/18/find-tests-more-easily-in-your-testlog/
class ActiveSupport::TestCase
setup :log_example
private
def log_example
Rails.logger.info "\nStarting #{@method_name}\n#{'-' * (9 + @method_name.length)}\n"
end
@wojtha
wojtha / 00_README.md
Created October 24, 2017 20:42 — forked from dbalatero/00_README.md
This is an example of how I combine interaction/service classes with Wisper event broadcasting inside Rails.

This is an example of how I combine interaction/service classes with Wisper event broadcasting in Rails.

In this example, I show a UsersController#create API, a corresponding service object, and all the test code/listeners to make it all happen.

The outcome is:

  • Concepts in your system ("Signing up a user", "Creating an order") have a single entry point in your codebase, vs. making raw ActiveRecord calls to object.save in dozens of places.
  • Since your concept has one entry point (the service class), you can easily grep for usage of it.
  • Stupid easy to attach listeners to the service class
  • All event listeners are very small and easily unit tested
@wojtha
wojtha / private_constants.rb
Created October 23, 2017 23:26 — forked from skwp/private_constants.rb
private_constants.rb
# A simple way to create private constants without all the noise of ruby's 'private_constant'
#
# Before:
# class Foo
# FOO = "bar"
# private_constant :FOO
# end
#
# After:
# class Foo
@wojtha
wojtha / Ruby and Rails Interview Cheat Sheet.md
Created August 11, 2017 12:28 — forked from ahmadhasankhan/Ruby and Rails Interview Cheat Sheet.md
This is my Ruby interview cheat sheet. Feel free to fork it, Use it, Share it, or do whatever you want with it. PLEASE let me know if there is any error or if anything crucial is missing. I will keep updating...

Ruby and Rails Interview Questions

Ruby

  • What is a class?
  • What is an object?
  • What is a module? Can you tell me the difference between classes and modules?
  • Can you tell me the three levels of method access control for classes and modules? What do they imply about the method?
  • There are three ways to invoke a method in ruby. Can you give me at least two?
  • Explain this ruby idiom: a ||= b
@wojtha
wojtha / answers.md
Created August 11, 2017 12:27 — forked from austenito/answers.md
Answers to Ruby Interview Questions

Answers to [Ruby Interview Questions][1]

What is a class?

A text-book answer: classes are a blue-print for constructing computer models for real or virtual objects... boring.

In reality: classes hold data, have methods that interact with that data, and are used to instantiate objects.

Like this.

@wojtha
wojtha / mailer_job.rb
Created June 8, 2017 16:53
Alternative to Sidekiq delayed mailer extension
class MailerJob
include Sidekiq::Worker
sidekiq_options queue: 'mailer'
def perform(mailer_class, action, *params)
mailer_class.constantize.send(action, *params).deliver!
end
# Module DeliverAsync provides extension for mailers to easily send emails in background.
@wojtha
wojtha / migrate_rubocop_style_to_layout.rb
Last active January 10, 2019 10:37
Ruby script to migrate Rubocop settings from Style to Layout
# Fixes complains such as:
# .rubocop.yml: Style/IndentationConsistency has the wrong namespace - should be Layout
# .rubocop.yml: Style/IndentationWidth has the wrong namespace - should be Layout
# .rubocop.yml: Style/DotPosition has the wrong namespace - should be Layout
layouts = %w[
AccessModifierIndentation
AlignArray
AlignHash
AlignParameters
@wojtha
wojtha / Vagrantfile
Last active April 11, 2017 13:22
Using ansibler gem to share Ansible inventory file with Vagrant https://github.com/aisrael/ansibler
Vagrant.configure("2") do |config|
# Base VM OS configuration.
config.vm.box = "geerlingguy/ubuntu1404"
config.vm.synced_folder '.', '/vagrant', disabled: true
config.ssh.insert_key = false
config.vm.provider :virtualbox do |v|
v.memory = 256
v.cpus = 1
v.linked_clone = true
@wojtha
wojtha / Unit.csv
Created February 2, 2017 22:10
Rails seeds.rb with data in CSV
id name
1 milhões/mm³
2 g/dL
3 %
4 fL
5 pg
6 mil/mm³
7 mg/dL
8 mcg/dL
9 U/L
@wojtha
wojtha / question_multiple_choice.rb
Created January 23, 2017 10:08
Class Question::MultipleChoice is a simple, closed-ended question type that lets respondents select one or multiple answers from a defined list of choices.
# Class Question::MultipleChoice is a simple, closed-ended question type that lets respondents select one or multiple
# answers from a defined list of choices.
#
# @example Definition
# question = Question::MultipleChoice.new("Reference doesn't seem to be regular release. MAKE A BUILD?")
# .choice(:build, 'b', '[b]uild')
# .choice(:install, 'i', '[i]nstall and build')
# .choice(:clean, 'c', '[c]lean install and build')
# .choice(:skip, 's', '[s]kip')
# .choice(:quit, 'q', '[q]uit')