module Mks module Edm module Defaults extend ActiveSupport::Concern included do before_validation :apply_default_values end def apply_default_values self.class.defaults.each do |key, value| next unless self.send(key).nil? val = value.respond_to?(:call) ? value.call(self) : value self[key] = val end end class_methods do def default(attribute, value = nil, &block) defaults[attribute] = value # Allow the passing of blocks defaults[attribute] = block if block_given? end def defaults @defaults ||= {} end end end end end