Sha256: 6baaadf21329ffbec6d0b116707077cb1b95b61f222426fadabeb65fd596e15c

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require_relative 'base_methods'

module LightweightAttributes
  module BaseClassMethods
    def _default_attributes # :nodoc:
      load_schema
      @default_attributes ||= if attributes_to_define_after_schema_loads.empty?
        LightweightAttributes::AttributeSet.new({})
      else
        ActiveModel::AttributeSet.new({})
      end
    end

    def attributes_builder
      if attributes_to_define_after_schema_loads.empty?
        unless defined?(@attributes_builder) && @attributes_builder
          defaults = _default_attributes.except(*(column_names - [primary_key]))
          @attributes_builder = LightweightAttributes::AttributeSet::Builder.new(attribute_types, defaults)
        end
        @attributes_builder
      else
        super
      end
    end

    def load_schema!
      super
      include BaseMethods if attributes_to_define_after_schema_loads.empty?
    end

    #TODO: maybe we need to properly handle other non-nil values
    private def define_default_attribute(name, value, type, from_user:)
      super if attributes_to_define_after_schema_loads.any?

      if value.nil? || (value == ActiveRecord::Attributes::ClassMethods.const_get(:NO_DEFAULT_PROVIDED))
        _default_attributes[name] = nil
      else
        super
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lightweight_attributes-0.2.0 lib/lightweight_attributes/base_class_methods.rb
lightweight_attributes-0.1.1 lib/lightweight_attributes/base_class_methods.rb
lightweight_attributes-0.1.0 lib/lightweight_attributes/base_class_methods.rb