Skip to content

Instantly share code, notes, and snippets.

View chrismitchell's full-sized avatar

Chris Mitchell chrismitchell

  • Sherwood Park, AB
  • 18:09 (UTC -06:00)
View GitHub Profile
@n3bulous
n3bulous / lograge_newrelic_config.rb
Last active January 22, 2024 20:39
Lograge with NewRelic Config
# Shared by Martin Streicher in the Rails Performance Slack
# https://railsperf.slack.com/archives/C0RUUGY6R/p1702565130357359
#
config.lograge.enabled = true # see lograge section below...
config.lograge.formatter = Lograge::Formatters::Logstash.new
config.log_formatter = ::NewRelic::Agent::Logging::DecoratingFormatter.new
config.lograge.custom_options =
lambda do |event|
payload = event.payload
@fractaledmind
fractaledmind / has_many_attached.rb
Created October 25, 2023 20:21
Vanilla Rails isomorphic attachment validations
# /app/models/concerns/has_many_attached.rb
module HasManyAttached
extend ActiveSupport::Concern
class_methods do
def has_many_attached(name, dependent: :purge_later, service: nil, strict_loading: false, **options)
super(name, dependent: :purge_later, service: nil, strict_loading: false)
if options[:file_types].any?
validate "validate_#{name}_file_types".to_sym
@ArturT
ArturT / api_controller.rb
Last active July 20, 2023 06:44
How to rescue ActionDispatch::Http::MimeNegotiation::InvalidType in API controller for Rails 6.1+ and render nice JSON error. Learn more how you could run RSpec/Minitest tests faster in Rails app https://docs.knapsackpro.com/2020/how-to-speed-up-ruby-and-javascript-tests-with-ci-parallelisation
# This is example how to rescue from exception ActionDispatch::Http::MimeNegotiation::InvalidType
# and show nice JSON error in your API
module API
class BaseController < ActionController::API
def process_action(*args)
super
rescue ActionDispatch::Http::MimeNegotiation::InvalidType => exception
# set valid Content-Type to be able to call render method below
request.headers['Content-Type'] = 'application/json'
render status: 400, json: { errors: [exception.message] }
@JamesYang76
JamesYang76 / Time Zone.md
Last active April 4, 2024 22:00
Time Zone for rails

Time zones

There are trhee diffrent time zones in rails

  • System - The server time zone
  • Application - Rails application use own time zone
  • Database - Default is UTC, but do not change it

Cautions

  • config.time_zone should be set when an app dose not support multiple time zones
  • Do not use Date/Time API using system time zone
  • Date could be incorrect by being converted from datetime or time to date
@jiparis
jiparis / ruby_interfaces.md
Last active April 26, 2021 16:01
Interfaces and abstract classes in Ruby

Two approaches to interfaces in Ruby:

First aproach

Using inheritance in a cleaner way. It allows abstract classes, but needs a custom inheritance chain and only supports 1 interface.

module Interface
  attr_accessor :is_abstract
  
  def abstract
@bambooom
bambooom / puppeteer-gif.js
Last active February 3, 2024 06:08
use puppeteer to generate gif
const fs = require('fs');
const puppeteer = require('puppeteer');
const GIFEncoder = require('gifencoder');
const PNG = require('png-js');
function decode(png) {
return new Promise(r => {png.decode(pixels => r(pixels))});
}
@adamwathan
adamwathan / vue.sublime-snippet
Created February 5, 2018 20:24
Vue Component Snippet for Sublime
<snippet>
<content><![CDATA[
<template>
${0}
</template>
<script>
export default {
props: [${1}],
data() {
@pixeltrix
pixeltrix / 20171004211734_add_timestamps_everywhere.rb
Created October 4, 2017 21:27
Migration to add timestamps easily to a bunch of tables
class AddTimestampsEverywhere < ActiveRecord::Migration[5.1]
def change
now = Time.current
%i[
candidates
evaluations
interviewers
questions
responses
@mjason
mjason / Dockerfile
Last active February 25, 2024 21:51
build phoenix in docker
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install -y git wget curl build-essential
RUN wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb && dpkg -i erlang-solutions_1.0_all.deb
RUN apt-get update
RUN apt-get install erlang -y
RUN apt-get install -y elixir
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -