Module: DraftApprove::Draftable::InstanceMethods

Defined in:
lib/draft_approve/draftable/instance_methods.rb

Overview

Instance methods automatically added to an ActiveRecord model when acts_as_draftable is called

Instance Method Summary collapse

Instance Method Details

#draft_destroy!(options = nil) ⇒ Draft

Marks this object to be destroyed when this draft change is approved.

This method should only be called on objects which have already been persisted.

Parameters:

  • options (Hash) (defaults to: nil)

    the options to save the draft with

Options Hash (options):

  • :delete_method (Symbol)

    the method to use to delete the object when this draft is approved, eg. :delete. This must be a method available on the object. The default is destroy!.

Returns:

  • (Draft)

    the Draft object which was created



59
60
61
# File 'lib/draft_approve/draftable/instance_methods.rb', line 59

def draft_destroy!(options = nil)
  DraftApprove::Persistor.write_draft_from_model(Draft::DELETE, self, options)
end

#draft_save!(options = nil) ⇒ Draft?

Saves any changes to the object as a draft.

This method may be called both on a new object which has not been persisted yet, and on objects which have already been persisted.

Parameters:

  • options (Hash) (defaults to: nil)

    the options to save the draft with

Options Hash (options):

  • :validate (Symbol)

    whether to validate the model before draft changes are saved, defaults to true

  • :create_method (Symbol)

    the method to use when creating a new object from this draft, eg. :find_or_create_by!. This must be a method available on the object, and must accept a hash of attribute names to attribute values. The default is create!. Ignored if this draft is for an object which has already been persisted.

  • :update_method (Symbol)

    the method to use when updating an existing object from this draft, eg. :update_columns. This must be a method available on the object, and must accept a hash of attribute names to attribute values. The default is update!. Ignored if this draft is for an object which has not yet been persisted.

Returns:

  • (Draft, nil)

    the Draft object which was created, or nil if there were no changes to the object



40
41
42
43
44
45
46
# File 'lib/draft_approve/draftable/instance_methods.rb', line 40

def draft_save!(options = nil)
  if self.new_record?
    DraftApprove::Persistor.write_draft_from_model(Draft::CREATE, self, options)
  else
    DraftApprove::Persistor.write_draft_from_model(Draft::UPDATE, self, options)
  end
end

#draft_update!(attributes) ⇒ Draft?

Updates an existing object with the given attributes, and saves the updates as a draft.

Parameters:

  • attributes (Hash)

    a hash of attribute names to attribute values, like the hash expected by the ActiveRecord update / update! methods

Returns:

  • (Draft, nil)

    the Draft object which was created, or nil if there were no changes to the object



74
75
76
77
# File 'lib/draft_approve/draftable/instance_methods.rb', line 74

def draft_update!(attributes)
  self.assign_attributes(attributes)
  self.draft_save!
end

#draftable?Boolean

Whether this object is draftable. Helper method to identify draftable objects.

Returns:

  • (Boolean)

    true



15
16
17
# File 'lib/draft_approve/draftable/instance_methods.rb', line 15

def draftable?
  true
end