Module: DraftApprove::Draftable::ClassMethods
- Defined in:
- lib/draft_approve/draftable/class_methods.rb
Overview
Class methods automatically added to an ActiveRecord model when acts_as_draftable
is called
Instance Method Summary collapse
-
#draft_create!(attributes) ⇒ Draft
Creates a new object with the given attributes, and saves the new object as a draft.
-
#draft_transaction(created_by: nil, extra_data: nil) { ... } ⇒ DraftTransaction?
Starts a new
DraftTransaction
to group together a number of draft changes that must be approved and applied together. -
#find_and_draft_update_or_create_draft_by!(attributes) {|instance| ... } ⇒ Object
Finds an object with the given attributes and draft update it with the given block, or draft create a new object.
-
#find_or_create_draft_by!(attributes) {|instance| ... } ⇒ Object
Finds an object with the given attributes.
Instance Method Details
#draft_create!(attributes) ⇒ Draft
Creates a new object with the given attributes, and saves the new object as a draft.
42 43 44 |
# File 'lib/draft_approve/draftable/class_methods.rb', line 42 def draft_create!(attributes) self.new(attributes).draft_save! end |
#draft_transaction(created_by: nil, extra_data: nil) { ... } ⇒ DraftTransaction?
Starts a new DraftTransaction
to group together a number of draft changes that must be approved and applied together.
25 26 27 28 29 |
# File 'lib/draft_approve/draftable/class_methods.rb', line 25 def draft_transaction(created_by: nil, extra_data: nil) DraftApprove::Transaction.in_new_draft_transaction(created_by: created_by, extra_data: extra_data) do yield end end |
#find_and_draft_update_or_create_draft_by!(attributes) {|instance| ... } ⇒ Object
Finds an object with the given attributes and draft update it with the given block, or draft create a new object.
If an object is found matching the given attributes, the given block is applied to this object and the updates are saved as a draft.
If no object is found matching the given attributes, a new object is initialised with the given attributes, and the given block is applied to this new object before it is saved as a draft.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/draft_approve/draftable/class_methods.rb', line 103 def find_and_draft_update_or_create_draft_by!(attributes) instance = self.find_by(attributes) if instance.blank? instance = self.new(attributes) end # Whether or not this is a new record, execute the block to update # additional, non-find_by attributes yield(instance) if block_given? instance.draft_save! return instance end |
#find_or_create_draft_by!(attributes) {|instance| ... } ⇒ Object
Finds an object with the given attributes. If none found, creates a new object with the given attributes, executes the given block, and saves the new object as a draft.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/draft_approve/draftable/class_methods.rb', line 64 def find_or_create_draft_by!(attributes) instance = self.find_by(attributes) if instance.blank? instance = self.new(attributes) # Only execute the block if this is a new record yield(instance) if block_given? end instance.draft_save! return instance end |