Skip to content

Instantly share code, notes, and snippets.

View rodloboz's full-sized avatar

Rui Freitas rodloboz

View GitHub Profile
class AddFollowCountersToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :followers_count, :integer, null: false, default: 0
add_column :users, :followings_count, :integer, null: false, default: 0
end
end
class Users::FollowsController < ApplicationController
before_action :set_user
def create
if current_user.follow(@user.id)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
module Followable
extend ActiveSupport::Concern
included do
has_many :follower_relationships, foreign_key:
:following_id,
class_name: 'Follow'
has_many :followers, through: :follower_relationships, source: :follower
has_many :following_relationships, foreign_key:
module Authenticable
extend ActiveSupport::Concern
included do
attr_accessor :login
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates :username,
@rodloboz
rodloboz / mapbox.js
Created April 24, 2019 01:49
Mapbox Clusters
import 'mapbox-gl/dist/mapbox-gl.css';
import mapboxgl from 'mapbox-gl';
const fitMapToMarkers = (map, features) => {
const bounds = new mapboxgl.LngLatBounds();
features.forEach(({ geometry }) => bounds.extend(geometry.coordinates));
map.fitBounds(bounds, { padding: 70, maxZoom: 15 });
};
const initMapbox = () => {
@rodloboz
rodloboz / email.rb
Created January 21, 2019 10:50
Lecture on Regular Expression - Batch 224
PATTERN1 = /^(?<first_name>\w+)\.(?<last_name>\w+)@(?<provider>.+)\.(.+)$/
PATTERN2 = /^(\w+)\.(\w+)@(.+)\.(.+)$/
email = "[email protected]"
match = email.match(PATTERN1) # same as PATTERN.match(email)
p match
@rodloboz
rodloboz / 01-recap.rb
Created January 18, 2019 11:15
Ruby Hashes - Batch 224
"Hello world" # String
1 # Integer
3.14 # Float
true # Boolean
nil # Nil
[1, 3, 6, "Hello"] # Array
# boxes/container to store stuff/data
first_name = "rui"
[1, 2, 3, "hello"] # Array
"hello" # String
5 # Integer
1.356 # Float
(1..10) # Range
true # boolean
nil # Nil
# box/container to store stuff/data
# so it can be used again
# split the sentence into words
# ignore punctuation // filter words
# ignore one letter words
# shift initial consonant group to the end of each word
# add random suffix to the end
# pre-pend l to te beginning of each word
SUFFIXES = %w[em é ji oc ic uche ès].freeze
VOWELS = %w[a e i o u].freeze
# split the sentence into words
# ignore punctuation // filter words
# ignore one letter words
# shift initial consonant group to the end of each word
# add random suffix to the end
# pre-pend l to te beginning of each word
SUFFIXES = %w[em é ji oc ic uche ès].freeze
VOWELS = %w[a e i o u].freeze