Skip to content

Instantly share code, notes, and snippets.

View vlado's full-sized avatar

Vlado Cingel vlado

View GitHub Profile
@vlado
vlado / a_rails_examples.rb
Last active November 7, 2022 14:59
Value Objects in Ruby (and Rails)
# create_table :products do |t|
# t.integer :price_in_cents
# ...
# end
class Product < ApplicationRecord
VAT = 25 # %
def price_in_eur
price_in_cents / 100.0
end
class SearchForm
include ActiveModel::Model
%i[query lat lng].each do |name|
define_method name do
@params[name]
end
end
def initialize(params)
@vlado
vlado / _form.html.erb
Last active June 9, 2020 06:32
Example how mdc form builder could be implemented
<%= mdc_form_with model: @my_model do |f| %>
<%= f.mdc_text_field :last_name %>
<% end %>
@vlado
vlado / encrypt_decrypt.sh
Created January 28, 2019 11:43
Encrypt/Decrypt file on macOs
# Encrypt
openssl aes-256-cbc -salt -in filename.ext -out filename.enc
# Decrypt
openssl aes-256-cbc -d -salt -in filename.enc -out filename.ext
@vlado
vlado / appointment.rb
Last active October 8, 2017 21:01
Rails bug when creating using association and scopes?
class Appointment < ApplicationRecord
belongs_to :inquiry
enum appointment_type: { default: 0, q1: 1, q2: 2 }
scope :q1, -> { where(appointment_type: q1) }
scope :on_date, -> (date) { where(scheduled_date: date) }
before_validation :set_start_and_end_time
def duration
[15, 30, 45].sample
@vlado
vlado / install_neo4j_on_semaphore_ci.sh
Last active August 5, 2016 09:18
Install Neo4j on Semaphore CI
#! /usr/bin/env bash
set -e
source /opt/change-java-version.sh
change-java-version 8
wget -O - https://debian.neo4j.org/neotechnology.gpg.key | sudo apt-key add -
echo 'deb http://debian.neo4j.org/repo stable/' >/tmp/neo4j.list
sudo mv /tmp/neo4j.list /etc/apt/sources.list.d
sudo apt-get update
@vlado
vlado / example.rb
Created March 5, 2016 21:29 — forked from JoshCheek/example.rb
Debug
# https://minhajuddin.com/2016/03/03/put-this-in-your-code-to-debug-anything
require 'rouge'
require 'method_source'
require 'pp'
class Dbg
def initialize(object, to:)
@object, @stream = object, to
end
@vlado
vlado / add_empty_check_for_name_and_ams_key_to_carriers.rb
Last active February 18, 2016 05:17
Postgres constraints and triggers migration examples
class AddEmptyCheckForNameAndAmsKeyToCarriers < ActiveRecord::Migration
def up
execute %{
ALTER TABLE carriers
ADD CONSTRAINT check_carriers_name_is_not_empty
CHECK (name <> '');
}
execute %{
ALTER TABLE carriers
ADD CONSTRAINT check_carriers_ams_key_is_not_empty
module ElasticsearchSupport
class ReindexUpdaterStore
class << self
def redis
$redis
end
def start(klass)
@vlado
vlado / bd_upgrade_es_on_semaphoreci.sh
Last active March 25, 2016 20:33
[BD] Upgrade Elasticsearch on SemaphoreCI
rbenv global 2.2.2
sudo service elasticsearch stop
if ! [ -e .semaphore-cache/elasticsearch-1.5.1.deb ]; then (cd .semaphore-cache; curl -OL https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.5.1.deb); fi
echo Y | sudo dpkg -i .semaphore-cache/elasticsearch-1.5.1.deb
sudo sh -c "echo 'script.disable_dynamic: false' >> /etc/elasticsearch/elasticsearch.yml"
sudo service elasticsearch start
sleep 5 && curl -XGET 'localhost:9200'