module ValueObjects::ActiveRecord

Public Class Methods

included(base) click to toggle source
# File lib/value_objects/active_record.rb, line 6
def self.included(base)
  base.extend self
end

Public Instance Methods

value_object(attribute, value_class, options = {}) click to toggle source
# File lib/value_objects/active_record.rb, line 10
def value_object(attribute, value_class, options = {})
  type =
    begin
      column_for_attribute(attribute)&.type
    rescue ::ActiveRecord::StatementInvalid
      # This can happen if `column_for_attribute` is called but the database table does not exist
      # as will be the case when migrations are run and the model class is loaded by initializers
      # before the table is created.
      # This is a workaround to prevent such migrations from failing.
      nil
    end
  coder =
    case type
    when :string, :text
      JsonCoder.new(value_class)
    else
      value_class
    end
  serialize(attribute, coder)
  validates_with(::ValueObjects::ValidValidator, options.merge(attributes: [attribute])) unless options[:no_validation]
  setter = :"#{attribute}="
  define_method("#{attribute}_attributes=") do |attributes|
    send(setter, value_class.new(attributes))
  end
end