Skip to content

Instantly share code, notes, and snippets.

View erados's full-sized avatar
🐋

Myungwoo Song erados

🐋
View GitHub Profile
@cjavdev
cjavdev / define_method.rb
Created January 20, 2021 22:49
code for an video about define_method: https://youtu.be/I0itVuoprAY
class ModelBase
def self.enum(**kwargs)
kwargs.each do |key, values|
define_method("#{key}=") do |v|
if values.include?(v)
instance_variable_set("@#{key}", v)
else
raise "Not a valid status: #{v}"
end
end
@timm-oh
timm-oh / application_record.rb
Last active May 19, 2024 11:08
Rails 5.2.3: Monkey patch update_all and update_columns to always include updated_at
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
# Didn't change #update_column because it uses #update_columns under the hood.
def update_columns(attrs)
new_attrs = attrs.symbolize_keys
new_attrs[:updated_at] ||= Time.current if self.class.column_names.include?('updated_at')
super(new_attrs)
end