Skip to content

Instantly share code, notes, and snippets.

@eric1234
eric1234 / composed_model.rb
Last active October 11, 2023 19:38
composed model example
concern :ComposedModel do
include ActiveModel::Model
def valid?
objects.all?(&:valid?) && super
end
def save!
raise ActiveRecord::RecordInvalid, self unless valid?
transaction { objects.each &:save! }
@eric1234
eric1234 / index.js
Last active April 15, 2022 16:02
PouchDB Rollup Conflict
import PouchDB from 'pouchdb-browser'
import MemoryAdapterPlugin from 'pouchdb-adapter-memory'
PouchDB.plugin(MemoryAdapterPlugin)
@eric1234
eric1234 / .gitignore
Last active January 24, 2022 15:08
Wordle Starter Words
*.txt
--- ext/openssl/ossl_pkey_ec.c 2010-06-21 04:18:59.000000000 -0500
+++ ext/openssl/ossl_pkey_ec.c 2013-12-10 13:30:18.919963527 -0600
@@ -757,8 +757,10 @@
method = EC_GFp_mont_method();
} else if (id == s_GFp_nist) {
method = EC_GFp_nist_method();
+ #if !defined(OPENSSL_NO_EC2M)
} else if (id == s_GF2m_simple) {
method = EC_GF2m_simple_method();
+ #endif
@eric1234
eric1234 / widgets_controller.rb
Last active June 1, 2018 14:59
Lettable-based Controller Example
class WidgetsController < ApplicationController
let(:widgets) { Widget.all }
let(:widget) { widgets.find_or_initialize_by id: params[:id] }
def new
render :form
end
def edit
render :form
@eric1234
eric1234 / README.md
Last active October 19, 2016 21:02
Simple download function for PHP

Usage

$url = 'http://some.url/file.txt';
$data = download($url, $headers, $err_msg);
if( !$data ) {
  echo $err_msg;
  die;
}
@eric1234
eric1234 / widgets_controller.rb
Created October 4, 2016 14:56
Stock Scaffold Rails Controller
class WidgetsController < ApplicationController
before_action :set_widget, only: [:show, :edit, :update, :destroy]
# GET /widgets
def index
@widgets = Widget.all
end
# GET /widgets/1
def show
@eric1234
eric1234 / multidef.rb
Created October 4, 2016 14:54
Define multiple methods with the same defination
module MultiDef
def defs *names, &blk
names.each { |name| define_method name, blk }
end
end
@eric1234
eric1234 / lettable.rb
Last active November 27, 2018 21:37
Easy Sharing of Data Loading in Rails Controllers
module Lettable
def let name, &blk
iv = "@#{name}"
define_method name do
return instance_variable_get iv if instance_variable_defined? iv
instance_variable_set iv, instance_eval(&blk)
end
helper_method name
@eric1234
eric1234 / agents_controller.rb
Last active March 21, 2017 17:33
Example Real Controller
class Admin::AgentsController < AdminController
include ListParams
include StatusFilter
helper :filter
let(:agents) { policy_scope authorize Agent.all }
let(:agent) { authorize agents.with_stats.find_or_build params[:id] }
def index