Class: Draft

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/draft_approve/models/draft.rb

Overview

Note:

It is strongly recommended that you do not directly create Draft objects, and instead use the supported public interface for doing so. See DraftApprove::Draftable::ClassMethods, DraftApprove::Draftable::InstanceMethods, and the README docs for this.

ActiveRecord model for persisting data about draft changes.

Each Draft must be linked to a DraftTransaction, and must have a draft_action_type which specifies whether this draft is to create a new record, update a record, or delete a record.

If the draft is to update or delete an existing record in the database, the Draft will also have a link to the acts_as_draftable instance to which it relates, via the polymorphic draftable association.

Linking to the acts_as_draftable instance is not possible for drafts which create new records, since the new record does not yet exist in the database! In these cases, the draftable_type column is still set to the name of the class which is to be created, but the draftable_id is nil.

The draft_changes attribute is a serialized representation of the draft changes. The representation is delegated to a DraftApprove::Serialization module. At present, there is only a JSON implementation, suitable for use with PostgreSQL databases.

Note that saving ‘no-op’ Drafts is generally avoided by this library (specifically by the DraftApprove::Persistor class).

Constant Summary collapse

CREATE =

IMPORTANT NOTE: These constants are written to the database, so cannot be updated without requiring a migration of existing draft data

'create'.freeze
UPDATE =
'update'.freeze
DELETE =
'delete'.freeze

Instance Method Summary collapse

Instance Method Details

#apply_changes!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Apply the changes in this draft, writing them to the database



72
73
74
# File 'lib/draft_approve/models/draft.rb', line 72

def apply_changes!
  DraftApprove::Persistor.write_model_from_draft(self)
end

#create?Boolean

Returns true if this Draft is to create a new record, false otherwise.

Returns:

  • (Boolean)

    true if this Draft is to create a new record, false otherwise



54
55
56
# File 'lib/draft_approve/models/draft.rb', line 54

def create?
  draft_action_type == CREATE
end

#delete?Boolean

Returns true if this Draft is to delete an existing record, false otherwise.

Returns:

  • (Boolean)

    true if this Draft is to delete an existing record, false otherwise



66
67
68
# File 'lib/draft_approve/models/draft.rb', line 66

def delete?
  draft_action_type == DELETE
end

#draft_proxyDraftChangesProxy

Get a DraftChangesProxy for this Draft

Returns:

  • (DraftChangesProxy)

    a proxy to get changes drafted in this Draft and related objects, within the scope of the DraftTransaction this Draft occurred within

See Also:



83
84
85
# File 'lib/draft_approve/models/draft.rb', line 83

def draft_proxy
  draft_transaction.draft_proxy_for(self)
end

#update?Boolean

Returns true if this Draft is to update an existing record, false otherwise.

Returns:

  • (Boolean)

    true if this Draft is to update an existing record, false otherwise



60
61
62
# File 'lib/draft_approve/models/draft.rb', line 60

def update?
  draft_action_type == UPDATE
end