Module: Draftsman::Model::InstanceMethods

Defined in:
lib/draftsman/model.rb

Instance Method Summary collapse

Instance Method Details

#draft?Boolean

Returns whether or not this item has a draft.

Returns:

  • (Boolean)


139
140
141
# File 'lib/draftsman/model.rb', line 139

def draft?
  send(self.class.draft_association_name).present?
end

#draft_creationObject

DEPRECATED: Use ‘#draft_save` instead.



144
145
146
147
# File 'lib/draftsman/model.rb', line 144

def draft_creation
  ActiveSupport::Deprecation.warn('`#draft_creation` is deprecated and will be removed from Draftsman 1.0. Use `#save_draft` instead.')
  _draft_creation
end

#draft_destroyObject

DEPRECATED: Use ‘#draft_destruction` instead.



150
151
152
153
154
155
156
# File 'lib/draftsman/model.rb', line 150

def draft_destroy
  ActiveSupport::Deprecation.warn('`#draft_destroy` is deprecated and will be removed from Draftsman 1.0. Use `draft_destruction` instead.')

  run_callbacks :draft_destroy do
    _draft_destruction
  end
end

#draft_destructionObject

Trashes object and records a draft for a ‘destroy` event.



159
160
161
162
163
# File 'lib/draftsman/model.rb', line 159

def draft_destruction
  run_callbacks :draft_destruction do
    _draft_destruction
  end
end

#draft_updateObject

DEPRECATED: Use ‘#draft_save` instead.



166
167
168
169
# File 'lib/draftsman/model.rb', line 166

def draft_update
  ActiveSupport::Deprecation.warn('`#draft_update` is deprecated and will be removed from Draftsman 1.0. Use `#save_draft` instead.')
  _draft_update
end

#object_attrs_for_draft_record(object = nil) ⇒ Object

Returns serialized object representing this drafted item.



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/draftsman/model.rb', line 172

def object_attrs_for_draft_record(object = nil)
  object ||= self

  attrs = object.attributes.except(*self.class.draftsman_options[:skip]).tap do |attributes|
    self.class.serialize_attributes_for_draftsman(attributes)
  end

  if self.class.draft_class.object_col_is_json?
    attrs
  else
    Draftsman.serializer.dump(attrs)
  end
end

#published?Boolean

Returns whether or not this item has been published at any point in its lifecycle.

Returns:

  • (Boolean)


187
188
189
# File 'lib/draftsman/model.rb', line 187

def published?
  self.published_at.present?
end

#save_draftObject

Creates or updates draft depending on state of this item and if it has any drafts.

  • If a completely new record, persists this item to the database and records a ‘create` draft.

  • If an existing record with an existing ‘create` draft, updates the record and the existing `create` draft.

  • If an existing record with no existing draft, records changes in an ‘update` draft.

  • If an existing record with an existing draft (‘create` or `update`), updated back to its original undrafted state, removes associated `draft record`.

Returns ‘true` or `false` depending on if the object passed validation and the save was successful.



206
207
208
209
210
211
212
213
214
# File 'lib/draftsman/model.rb', line 206

def save_draft
  run_callbacks :save_draft do
    if self.new_record?
      _draft_creation
    else
      _draft_update
    end
  end
end

#trashed?Boolean

Returns whether or not this item has been trashed

Returns:

  • (Boolean)


217
218
219
# File 'lib/draftsman/model.rb', line 217

def trashed?
  send(self.class.trashed_at_attribute_name).present?
end