Skip to content

Instantly share code, notes, and snippets.

View HDias's full-sized avatar
🏠
God is Better

Horecio Araújo Dias HDias

🏠
God is Better
View GitHub Profile
@ka8725
ka8725 / send_mail.rb
Last active October 24, 2022 18:41
Send mail matcher
# frozen_string_literal: true
# Source: https://gist.github.com/ka8725/6053d55be74f15b53cabfc3ac4dc99df
# Installation: put this file into spec/support/matchers/ folder.
# Simple matcher that allows checking of an email was sent during an example run.
#
# @example Positive expectation
# expect { action }.to send_email
#
@justalever
justalever / validate_array_datatype.rb
Created January 3, 2020 03:08
Validate an array data type in Ruby on Rails using a custom validator - PostGresQL allows arrays as a DB type.
# Define the validator
# app/validators/array_validator.rb
class ArrayValidator < ActiveModel::EachValidator
def validate_each(record, attribute, values)
Array(values).each do |value|
options.each do |key, args|
validator_options = { attributes: attribute }
validator_options.merge!(args) if args.is_a?(Hash)
@rubiii
rubiii / decimal_validator.rb
Last active July 3, 2024 01:39
Rails validator for Postgres decimal type with custom precision and scale
class DecimalValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
precision = options[:precision]
scale = options[:scale]
if !precision || !scale
raise ArgumentError, "#{self.class.name} expects :precision and :scale option"
end
@bbugh
bbugh / array_inclusion_validator.rb
Last active April 30, 2024 06:20
Rails Postgres Array Column Validator
# app/models/validators/array_inclusion_validator.rb
class ArrayInclusionValidator < ActiveModel::EachValidator
include ActiveModel::Validations::Clusivity
def validate_each(record, attribute, values)
values.each do |value|
unless include?(record, value)
message = (options[:message].try(:gsub, '%{value}', value) || "#{value} is not included in the list")
record.errors.add(attribute, :array_inclusion, message: message, value: value)
end
@danielalvarenga
danielalvarenga / terminal-colors-branch.sh
Last active March 23, 2025 16:39
Show branch in terminal Ubuntu
# Add in ~/.bashrc or ~/.bash_profile
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
RED="\[\033[01;31m\]"
YELLOW="\[\033[01;33m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
NO_COLOR="\[\033[00m\]"
@exocode
exocode / application_helper.rb
Last active July 23, 2024 14:28 — forked from juggy/call_template.rb
Render with ``render_to_string`` a complete page within a sidekiq worker including current_user
module ApplicationHelper
def current_user
@current_user ||= warden.authenticate(:scope => :user)
end
end
@JunichiIto
JunichiIto / alias_matchers.md
Last active March 27, 2025 14:04
List of alias matchers in RSpec 3

This list is based on aliases_spec.rb.

You can see also Module: RSpec::Matchers API.

matcher aliased to description
a_truthy_value be_truthy a truthy value
a_falsey_value be_falsey a falsey value
be_falsy be_falsey be falsy
a_falsy_value be_falsey a falsy value
@PWSdelta
PWSdelta / rspec_model_testing_template.rb
Last active March 11, 2025 21:14
Rails Rspec model testing skeleton & cheat sheet using rspec-rails, shoulda-matchers, shoulda-callbacks, and factory_girl_rails. Pretty much a brain dump of examples of what you can (should?) test in a model. Pick & choose what you like, and please let me know if there are any errors or new/changed features out there. Reddit comment thread: http…
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems: