Sha256: bed9e6301a7cc026f116e1752f0bf1199dd1aca057776878214445de9f24e810

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

module Shamu
  module Auditing

    # An audit record of a discrete change transaction.
    class Transaction < Services::Request
      include Entities::EntityPath

      # ============================================================================
      # @!group Attributes
      #

      # @!attribute
      # @return [Array<Object>] the chain of user ids from the
      # {Security::Principal} in place at the time of the request.
      attribute :user_id_chain, presence: true

      # @!attribute
      # @return [String] the primitive action that was requested, such as `add`,
      #     `remove`, or `change`.
      attribute :action, presence: true

      # @!attribute
      # @return [Hash] the changes by attribute requested in the transaction.
      attribute :changes

      # The {EntityPath} describing how to reach the leaf entity {#append_entity
      # appended} from the root entity.
      attribute :entity_path, presence: true do
        compose_entity_path( entities )
      end

      #
      # @!endgroup Attributes

      # Appends a child node to the {#entity_path}.
      # @overload append_entity( entity )
      #   @param [Entities::Entity] an entity
      # @overload append_entity( pair )
      #   @param [Array<String,Object>] pair consisting of entity class and id.
      def append_entity( entity )
        @entities ||= []
        entities << entity
      end

      # (see Services::Request#apply_to)
      def apply_to( model )
        super.tap do
          assign_changes_to_model model
        end
      end

      # @return [Boolean] true if entities have been added to the transaction.
      def entities?
        entities.present?
      end


      private

        attr_reader :entities

        def assign_changes_to_model( model )
          model.changes_json = changes.to_json if changes.present?
        end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shamu-0.0.24 lib/shamu/auditing/transaction.rb
shamu-0.0.21 lib/shamu/auditing/transaction.rb
shamu-0.0.20 lib/shamu/auditing/transaction.rb
shamu-0.0.19 lib/shamu/auditing/transaction.rb