Sha256: 644380ded00b473ce28463055a3368e37b95a9e26991659aa378f208ccc70a5d

Contents?: true

Size: 1.65 KB

Versions: 8

Compression:

Stored size: 1.65 KB

Contents

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

        included do
          class_attribute :mti_config

          self.abstract_class = true
          self.mti_config = ::ActiveSupport::HashWithIndifferentAccess.new({
            :foreign_key => :id
          })

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

        module ClassMethods
          def inherited(subklass)
            super(subklass)

            subklass.after_create do |record|
              entity_model = mti_config[:class_name].constantize.new

              record.attributes.each_pair do |k,v|
                entity_model.__send__("#{k}=", v) if entity_model.respond_to?(k)
              end

              entity_model.save
            end

            subklass.after_update do |record|
              entity_model = record.entity

              if record.changed.any?
                record.changes.each_pair do |k,v|
                  entity_model.__send__("#{k}=", v[1]) if entity.respond_to?(:"#{k}")
                end
              end

              entity_model.save if entity_model.changed.any?
            end

            subklass.after_destroy do |record|
              entity_model = record.entity

              entity_model.destroy
            end
          end

          def entity_model(options)
            valid_options = options.assert_valid_keys(:class_name, :foreign_key)

            mti_config.merge!(valid_options)

            self.has_one(:entity, mti_config.symbolize_keys)
            self.accepts_nested_attributes_for(:entity)
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
trax_model-0.0.98 lib/trax/model/mti/abstract.rb
trax_model-0.0.97 lib/trax/model/mti/abstract.rb
trax_model-0.0.96 lib/trax/model/mti/abstract.rb
trax_model-0.0.95 lib/trax/model/mti/abstract.rb
trax_model-0.0.93 lib/trax/model/mti/abstract.rb
trax_model-0.0.92 lib/trax/model/mti/abstract.rb
trax_model-0.0.91 lib/trax/model/mti/abstract.rb
trax_model-0.0.9 lib/trax/model/mti/abstract.rb