Skip to content

Instantly share code, notes, and snippets.

View stevecondylios's full-sized avatar

Steve Condylios stevecondylios

View GitHub Profile
@ericboehs
ericboehs / polymorphic_validation_with_cancan.rb
Created August 20, 2012 23:53
Use CanCan to determine if a polymorphic association has permission via validations
# Polymorphic validation
class Comment < ActiveRecord::Base
attr_accessible :content, :user, :user_id, :commentable, :commentable_id, :commentable_type
belongs_to :commentable, polymorphic: true
belongs_to :user
validates :commentable, :user, existence: { both: false } # Via validates_existence gem
validate :can_comment?, if: [:user, :commentable_id, :commentable_type]
@6temes
6temes / rails-tests.yml
Last active June 8, 2021 21:29
Github actions setup with Rspec and static code analysis for Ruby on Rails
env:
PGHOST: localhost
PGPASS: runner
PGUSER: runner
RAILS_ENV: test
DATABASE_URL: postgresql://runner:runner@localhost
name: Rails Tests
on: [push, pull_request_review]
# you can make a text file of request times (in ms, one number per line) and import it here, or you can use a probability distribution to simulate request times (see below where setting req_durations_in_ms)
# rq = read.table("~/Downloads/request_times.txt", header=FALSE)$V1
# argument notes:
# parallel_router_count is only relevant if router_mode is set to "intelligent"
# choice_of_two, power_of_two, and unicorn_workers_per_dyno are only relevant if router_mode is set to "naive"
# you can only select one of choice_of_two, power_of_two, and unicorn_workers_per_dyno
run_simulation = function(router_mode = "naive",
reqs_per_minute = 9000,
@hrbrmstr
hrbrmstr / heroku-pg.r
Last active October 25, 2021 01:15
Connect R (#rstats) to heroku PostgreSQL — https://www.heroku.com/postgres
library(processx)
library(RPostgres)
library(httr)
library(dbplyr)
library(tidyverse)
# this example assumes you've created a heroku postgresql
# instance and have the app name (in this example, "rpgtestcon").
# use the heroku command-line app
@trestletech
trestletech / server.R
Last active February 2, 2022 09:47
A Shiny app combining the use of dplyr and SQLite. The goal is to demonstrate a full-fledged, database-backed user authorization framework in Shiny.
library(shiny)
library(dplyr)
library(lubridate)
# Load libraries and functions needed to create SQLite databases.
library(RSQLite)
library(RSQLite.extfuns)
saveSQLite <- function(data, name){
path <- dplyr:::db_location(filename=paste0(name, ".sqlite"))
@aonemd
aonemd / subtitle_extractor.rb
Last active March 7, 2022 14:05
A Ruby script to extract text from images with subtitles and rename the image file to the extracted text (requires Tesseract to be installed)
require 'pathname'
require 'open3'
require 'mini_magick'
SRC_DIR = '/path/to/src/dir/'.freeze
TMP_DIR = '/path/to/tmp/dir/'.freeze
class TextReader
def initialize(input_path, output_path)
@input_path = input_path
@andynu
andynu / rfluff.rb
Created May 18, 2022 12:53
rfluff a way to list TODOS|XXX|HACK|etc /w optional vim quickfix formatting.
#!/usr/bin/ruby
#
# gather a few statistics and find all flagged comments
#
# Usage:
# rfluff [-qf] [tag] [tag] ...
#
# -qf outputs a vim compatible quickfix format, otherwise
# the output is human readable
@andynu
andynu / routes_check.rake
Last active June 1, 2022 01:40
Parses production logs and checks which routes are used out of all the routes that are defined.
desc 'Show basic controller usage stats'
task :controllers => :environment do
logfiles = Dir['log/%s.log*' % Rails.env].sort
logs_gz, logs_txt = logfiles.partition{|f| Pathname.new(f).extname == '.gz' }
results = `ag Started -A 1 #{logs_txt.join(' ')}`
unless logs_gz.empty?
results << `zcat #{logs_gz.join(' ')} |ag Started -A 1`
end
Event = Struct.new(:http_method, :uri_path, :client_ip, :requested_at_str, :controller_name, :controller_action, :format) do
def requested_at
@natematykiewicz
natematykiewicz / unroutable_routes.rake
Last active June 9, 2022 19:47
Find routes that will raise a routing error when requested
desc 'Find routes that will raise a routing error when requested'
task unroutable_routes: :environment do
# A lot of this code was taken from how `rake routes` works
# https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/railties/lib/rails/commands/routes/routes_command.rb
require 'action_dispatch/routing/inspector'
unroutables = Rails.application.routes.routes.
map { |r| ActionDispatch::Routing::RouteWrapper.new(r) }.
reject { |r| r.internal? || r.engine? || r.path.starts_with?('/rails/') || !r.controller }.
library(ggplot2)
library(ggirl)
# create the plot
plot <- ggplot() +
geom_polygon( data=map_data("state"), aes(x=long, y=lat, group=group),
fill="#cdddee", color="#7296bc" )+
geom_point(data = data.frame(long = -77.01728, lat = 38.78125),
aes(x=long, y=lat),