Sha256: c955725fae19df4b6fcb8b3ae175a332c6403b7373d52f7cfce1540ffca5c33d

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

module Trax
  module Model
    module MTI
      module Entity
        extend ::ActiveSupport::Concern

        included do
          class_attribute :_mti_namespace

          scope :records, lambda{
            map(&:entity)
          }
        end

        def model_type
          @model_type ||= uuid.record_type
        end

        def model_type_key
          :"#{model_type.name.demodulize.underscore}_entity"
        end

        def model
          @model ||= __send__(model_type_key)
        end

        def uuid
          @uuid ||= self[:id].uuid
        end

        module ClassMethods
          def all
            relation = super
            entity_ids = relation.pluck(:id)
            entity_types = entity_ids.map { |id| ::Trax::Model::UUID.new(id[0..1]) }
                                     .map(&:record_type)
                                     .flatten
                                     .compact
                                     .uniq

            relation_names = entity_types.map{ |type| :"#{type.name.demodulize.underscore}_entity" }

            relation.includes(relation_names).references(relation_names)
          end

          def mti_namespace(namespace)
            _mti_namespace = (namespace.is_a?(String)) ? namespace.constantize : namespace

            _mti_namespace.all.reject{|model| model.abstract_class }.each do |subclass|
              key = :"#{subclass.name.demodulize.underscore}_entity"
              has_one key, :class_name => subclass.name, :foreign_key => :id
            end
          end

          def multiple_table_inheritance_namespace(namespace)
            mti_namespace(namespace)
          end

        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trax_model-0.0.5 lib/trax/model/mti/entity.rb
trax_model-0.0.4 lib/trax/model/mti/entity.rb
trax_model-0.0.3 lib/trax/model/mti/entity.rb