class Draft

@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' +Draft+s is generally avoided by this library (specifically by the DraftApprove::Persistor class).

@see DraftTransaction @see DraftApprove::Draftable::ClassMethods @see DraftApprove::Draftable::InstanceMethods

Constants

CREATE

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

DELETE
UPDATE

Public Instance Methods

apply_changes!() click to toggle source

Apply the changes in this draft, writing them to the database @api private

# File lib/draft_approve/models/draft.rb, line 72
def apply_changes!
  DraftApprove::Persistor.write_model_from_draft(self)
end
create?() click to toggle source

@return [Boolean] true if this Draft is to create a new record, false

otherwise
# File lib/draft_approve/models/draft.rb, line 54
def create?
  draft_action_type == CREATE
end
delete?() click to toggle source

@return [Boolean] true if this Draft is to delete an existing record,

+false+ otherwise
# File lib/draft_approve/models/draft.rb, line 66
def delete?
  draft_action_type == DELETE
end
draft_proxy() click to toggle source

Get a DraftChangesProxy for this Draft

@return [DraftChangesProxy] a proxy to get changes drafted in this Draft

and related objects, within the scope of the +DraftTransaction+ this
+Draft+ occurred within

@see DraftTransaction#draft_proxy_for

# File lib/draft_approve/models/draft.rb, line 83
def draft_proxy
  draft_transaction.draft_proxy_for(self)
end
update?() click to toggle source

@return [Boolean] true if this Draft is to update an existing record,

+false+ otherwise
# File lib/draft_approve/models/draft.rb, line 60
def update?
  draft_action_type == UPDATE
end