Skip to content

Instantly share code, notes, and snippets.

Avatar

Weston Ganger westonganger

View GitHub Profile
@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
View 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)
View 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
View Single File Rails (app.rb)
### BUNDLER
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
ruby '2.4.1'
gem "rails"
gem "sqlite3"
#gem 'responders'
View Rails i18n with fresh load paths
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!
View Rails Engine - Load assets via Sprockets
# 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
View Rails Engine - Automatically load migrations
# 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|
View Ruby CSV bom handling
### Article about the BOM: https://estl.tech/of-ruby-and-hidden-csv-characters-ef482c679b35
### Article about other CSV gotchas: https://blog.rubyhero.dev/solving-problems-with-csv-parsing
### Remove Blank Rows from end of file
### Ideally this would be handled by the controller to sanitize before saving upload however I wasnt able to make that work
file_data = file.read.sub(/[,\n\r\t ]*\z/, "")
i = -1
@westonganger
westonganger / Naive ERB to Slim Converter - convert_slim_to_erb
Last active March 29, 2023 02:05
Naive ERB to Slim Converter (because slimrb does a really terrible job converting)
View Naive ERB to Slim Converter - convert_slim_to_erb
#!/usr/bin/env ruby
### USAGE: `./convert_slim_to_erb path/to/views/`
### LIMITATIONS:
### - Does not add closing tags such as `<% end %>` or `</div>`
SELF_CLOSING_TAGS = ["input","br","hr","img","meta","area","base","col","embed","link","source","track","wbr","param"].freeze
@line = nil
@westonganger
westonganger / Adjust Rails Date or Time Formatting Defaults
Created March 26, 2023 03:47
Adjust Rails Date or Time Formatting Defaults
View Adjust Rails Date or Time Formatting Defaults
DATE_FORMAT = "%F".freeze ### Rails default is "%m/%d/%Y"
TIME_FORMAT = "#{DATE_FORMAT} %l:%M%p %Z".freeze
format_defaults = {
date_time12: TIME_FORMAT,
date_time24: "#{DATE_FORMAT} %H:%M %Z".freeze,
}.freeze
Date::DATE_FORMATS.merge!(format_defaults.merge({default: DATE_FORMAT}))
Time::DATE_FORMATS.merge!(format_defaults.merge({default: TIME_FORMAT}))
@westonganger
westonganger / Fix Arel.sql deprecation warnings,errors in Rails 5.2,6+
Created March 26, 2023 03:38
Fix Arel.sql deprecation warnings & errors in Rails 5.2/6+
View Fix Arel.sql deprecation warnings,errors in Rails 5.2,6+
### config/initializers/arel_sql_monkey_patch.rb
ActiveRecord::Relation.class_eval do
### FIX ERRORS FOR Arel.sql in Rails 6+
### FIX DEPRECATION WARNINGS FOR Arel.sql in Rails =5.2
if Rails::VERSION::STRING.to_f >= 5.2
def pluck(*args)
if args.any?{|x| x.is_a?(String) }
new_args = args.map{|x| x.is_a?(String) ? Arel.sql(x) : x }