Skip to content

Instantly share code, notes, and snippets.

View westonganger's full-sized avatar

Weston Ganger westonganger

View GitHub Profile
@westonganger
westonganger / postgresql_fdw_with_activerecord_and_rails.md
Last active November 16, 2023 04:25
Postgresql FDW with ActiveRecord and Rails

Source Article (See for latest changes): https://westonganger.com/posts/using-postgresql-fdw-with-activerecord-and-rails

If you want to do some cross database joins or includes across postgresql databases regardless of local or remote databases you can use Postgres FDW. Its an awesome tool for the job when you want to avoid data replication.

Base Postgresql FDW Model:

class PostgresFdwBase < ApplicationRecord

  ### Article to familiarize with concepts in this models methods - https://thoughtbot.com/blog/postgres-foreign-data-wrapper
@westonganger
westonganger / Converting Postgresql SQL files to SQLite.md
Last active October 24, 2023 21:05
Converting Postgresql SQL files to SQLite

Step 1: Dump your postgresql create_table statements to a file:

pg_dump -s my_db_name > my_db_name_create.sql
  • You will need to manually remove all statements except CREATE TABLE, CREATE INDEX, CREATE_UNIQUE INDEX, etc.
    • Honestly just do it. It wont take too long, you'll be fine.
    • Some statements you will definately need to remove are SET, ALTER, CREATE SEQUENCE
  • For INDEX statements we need to find-and-remove USING btree
@westonganger
westonganger / Simple Rails Form Object
Last active October 11, 2023 18:23
Simple Rails Form Objects
class Forms::BaseForm
include ActiveModel::Validations
def initialize(attrs={})
attrs ||= {}
attrs.each do |k,v|
self.send("#{k}=", v) ### Use send so that it checks that attr_accessor has already defined the method so its a valid attribute
end
end
@westonganger
westonganger / Rails fields_for and nested attributes: How to add or remove without cocoon gem
Last active May 26, 2023 20:24
Rails fields_for and nested attributes: How to add or remove without cocoon gem
<%= form_for @post do |f| %>
<div class="comments-wrapper">
<% f.fields_for :comments do |f2| %>
<%= render "comment_fields", f: f2 %>
<% end %>
</div>
<button type="button" class="add-comment">Add Comment</button>
<script>
@westonganger
westonganger / Ruby Result objects (Success & Failure)
Last active May 19, 2023 21:32
Ruby Result objects (Success / Failure)
class Result
def initialize(h={}, **attrs)
if !h.is_a?(Hash)
raise ArgumentError.new("Non-hash argument provided")
end
@attrs = h.presence || attrs
@attrs.each do |k,v|
define_singleton_method(k) do
### BUNDLER
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
ruby '2.4.1'
gem "rails"
gem "sqlite3"
#gem 'responders'
def self.with_fresh_i18n_load_paths(load_paths, &block)
prev_load_path = I18n.load_path
I18n.load_path = load_paths
I18n.backend.reload!
block.call
ensure
I18n.load_path = prev_load_path
I18n.backend.reload!
# lib/<project_name>/engine.rb
module RailsI18nManager
class Engine < ::Rails::Engine
isolate_namespace RailsI18nManager
### Automatically load all migrations into main rails app
initializer "rails_i18n_manager.append_migrations" do |app|
if !app.root.to_s.match?(root.to_s)
config.paths["db/migrate"].expanded.each do |expanded_path|
# lib/<project_name>/engine.rb
module RailsI18nManager
class Engine < ::Rails::Engine
isolate_namespace RailsI18nManager
initializer "rails_i18n_manager.load_static_assets" do |app|
### Expose static assets
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end