Skip to content

Instantly share code, notes, and snippets.

# advisory_documents
# id: integer
class AdvisoryDocument < ApplicationRecord
has_many :signed_advisory_documents
has_many :users, through: :signed_advisory_documents
end
const useWebsocketDataChannel = (createDataChannel) => {
const dataChannelRef = useRef()
const [connected, setConnected] = useState(false)
const receivedCallbackRef = useRef()
useEffect(() => {
dataChannelRef.current = createDataChannel({
connected: () => setConnected(true),
received: receivedCallbackRef.current
})
const callEvery = (...functions) => (returnValue) =>
functions.reduce((previousValue, currentFunction) => {
if (currentFunction === undefined) return previousValue
return currentFunction(returnValue)
}, returnValue)
export default chainEventHandler
// Async example (returns a promise)
// Use case: you need a function to validate values after specific events (i.e. onSubmit)
const useFormValidation = (schema, values) => async () => {
try {
const isValidated = await schema.validate(values, {
abortEarly: false
})
return isValidated
} catch (error) {
const { errors } = error
@jokklan
jokklan / heartland_food_ticket_script.rb
Last active May 29, 2019 10:36
Heartland Food Tickets
EventOrOrganization.current = 193
ticket_items_by_day = {
'2019-05-30' => TicketItem.find(1557),
'2019-05-31' => TicketItem.find(1558),
'2019-06-01' => TicketItem.find(1559)
}
Shift.all.includes(attendances: :member).find_each do |shift|
shift.attendances.each do |attendance|
require 'csv'
class CsvReader
def parse(filename='/home/vlatko/rails_projects/inscale-interview/user_ticker_info.csv')
data = []
CSV.foreach(filename, :headers => true, :converters => :all, :header_converters => :symbol) do |row|
data << user_existance_validator(hashed_row)
end
data.compact
end
@jokklan
jokklan / member_batches_controller.rb
Last active February 10, 2017 11:47
Refactoring test
class MembersBatchesController < ApplicationController
respond_to :html
before_action :find_models, except: [:restore_modal, :restore]
def add_to_team_modal
hide_team_ids = @member_ids.count == 1 ? Member.find(@member_ids).first.team_ids : []
@members = Member.find(@member_ids)
@allowed = @members.all? { |member| policy(member).add_to_team? && CollectionPolicy.new(current_member, member.teams).all_allow?(:manage?) }
@jokklan
jokklan / csv_import_test.rb
Last active February 9, 2017 10:06
CSV import test
# Simple example implementation of User and TicketElement:
#
# class User < ActiveRecord::Base
# has_and_belongs_to_many :ticket_elements
# end
#
# class TicketElement < ActiveRecord::Base
# has_and_belongs_to_many :users
# end
#
class CollectionPolicy
attr_reader :collection, :user
def initialize(collection)
@user = user
@collection = collection
end
def all_allow?(action)
collection.all? do |record|
@jokklan
jokklan / pg_app_upgrade.sh
Last active September 5, 2015 10:35
Upgrading PostgreSQL with Postgres.app
# Inspired by http://postgresapp.com/documentation/install.html
# This will list all current databases
psql --list
# Find the databases you want to move over, i took all Rails development databases
psql --list | grep development | awk '{print $1;}'
# Then use this list in a for loop to make a db dump for each db
mkdir postgresql_dumps