Skip to content

Instantly share code, notes, and snippets.

View ltello's full-sized avatar

Lorenzo Tello ltello

View GitHub Profile
@ltello
ltello / action_view.rb
Created May 9, 2012 17:49
external links to tabs
module ActionView
module Helpers
module TextHelper
# Modify all html <a> tags in text to show them in a new browser's tab if they are externals to
# the domain of the url string opts[:except_site_host]
def external_links_to_tabs(text, opts = {})
text.gsub(A_LINK_RE) do
link, html_attrs = $&, $1
if href_to_this_site?(link, opts[:except_site_host].to_s)
@ltello
ltello / gist:3170585
Created July 24, 2012 15:13
Integers Roman notation
class Fixnum
# Returns the corresponding roman notation for numbers < 1000. Nice message otherwise.
def to_roman
return "Who knows, my friend!!" unless (1...1000) === self
[].tap do |result|
reverse_figures.each_with_index do |figure, weight|
result << figure.send(:to_roman_by_weight, weight)
end
end.reverse.join
@ltello
ltello / gist:3378668
Created August 17, 2012 13:24
Anagram count
# Given a list of words, return a hash with the anagram counting
class String
# Returns the associated anagram (string with the same letters sorted)
# Ex: "accb".anagram # => "abcc"
def anagram
split('').sort.join
end
end
@ltello
ltello / gist:3750352
Created September 19, 2012 15:41
Validate uniqueness with Rspec matchers (not shoulda)
describe 'Category' do
context '#create' do
before(:all) do
@properties = {:code => 'TST', :description => 'Test Category'}
@category = Category.create(@properties)
@same_category = Category.create(@properties)
end
context 'When right properties given' do
@ltello
ltello / #identities_controller.rb
Last active May 6, 2020 16:35
A sample controller to manage Identities linking Users to Kong Consumers
# frozen_string_literal: true
# @tag Identities
class IdentitiesController < ApplicationController
attr_reader :identity, :installer, :password, :token
before_action :create_identity, only: %i[create]
before_action :set_token, only: %i[change_password validate_reset_password_token]
before_action :set_identity, only: %i[destroy show update]
before_action :set_installer_identity, only: %i[change_password validate_reset_password_token password_resetable]
# frozen_string_literal: true
module PasswordResetable
extend ActiveSupport::Concern
included do
attr_reader :reset_password_token
end
# Check if the given token is valid to reset the password
@ltello
ltello / gas_safety_record.rb
Last active May 6, 2020 16:53
Sample model
# frozen_string_literal: true
class GasSafetyRecord < ApplicationRecord
# Associations
belongs_to :installation_job, inverse_of: :gas_safety_record
has_many :appliances,
class_name: "GasSafetyRecordAppliance",
dependent: :destroy,
inverse_of: :gas_safety_record
@ltello
ltello / gas_safety_record_test.rb
Last active May 6, 2020 16:55
Sample model test file (minitest)
# frozen_string_literal: true
require "test_helper"
class GasSafetyRecordTest < ActiveSupport::TestCase
describe GasSafetyRecord do
let(:gas_safety_record) { GasSafetyRecord.new }
subject { gas_safety_record }
describe "Columns" do
@ltello
ltello / keybase.md
Last active October 28, 2020 10:50
keybase.md

Keybase proof

I hereby claim:

  • I am ltello on github.
  • I am ltello8a (https://keybase.io/ltello8a) on keybase.
  • I have a public key ASCHe4CmnZ82oB6vl7rMn4dTfXBbRVCinILS9Ef-t5PDNAo

To claim this, I am signing this object: