Sha256: 03fa33f04afb84311f32a5f7343cea27309059938959ae514b667eb2bb972dbf

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module Mongoid
  module History
    module Attributes
      class Create < ::Mongoid::History::Attributes::Base
        def attributes
          @attributes = {}
          changes.each do |k, v|
            next unless trackable_class.tracked_field?(k, :create)
            @attributes[k] = format_field(k, v)
          end
          insert_embeds_one_changes
          insert_embeds_many_changes
          @attributes
        end

        private

        def insert_embeds_one_changes
          trackable_class.tracked_embeds_one.each do |rel|
            rel_class = trackable_class.relation_class_of(rel)
            paranoia_field = Mongoid::History.trackable_class_settings(rel_class)[:paranoia_field]
            paranoia_field = rel_class.aliased_fields.key(paranoia_field) || paranoia_field
            rel = aliased_fields.key(rel) || rel
            obj = trackable.send(rel)
            next if !obj || (obj.respond_to?(paranoia_field) && obj.public_send(paranoia_field).present?)
            @attributes[rel] = [nil, format_embeds_one_relation(rel, obj.attributes)]
          end
        end

        def insert_embeds_many_changes
          trackable_class.tracked_embeds_many.each do |rel|
            rel_class = trackable_class.relation_class_of(rel)
            paranoia_field = Mongoid::History.trackable_class_settings(rel_class)[:paranoia_field]
            paranoia_field = rel_class.aliased_fields.key(paranoia_field) || paranoia_field
            rel = aliased_fields.key(rel) || rel
            @attributes[rel] = [nil,
                                trackable.send(rel)
                                         .reject { |obj| obj.respond_to?(paranoia_field) && obj.public_send(paranoia_field).present? }
                                         .map { |obj| format_embeds_many_relation(rel, obj.attributes) }]
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-history-0.8.5 lib/mongoid/history/attributes/create.rb