This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |