class Draft

A model to store serialized draft data. Drafts attach themselves polymorphically to any other ActiveRecord class, loop through the target draftable classes’s attributes, and store them as a Hash in the :data text field in the database.

Drafts can also keep track of the target draftable’s CarrierWave uploads, if any exist.

Public Instance Methods

approve!() click to toggle source

Approve a draft, setting the attributes of the draftable object to contain the draft content, saving the draftable, and destroying the draft.

# File lib/drafter/draft.rb, line 38
def approve!
      draftable = inflate
      draftable.save!
      self.destroy
      draftable
end
approver() click to toggle source

It’s possible to use the :delegate_approval_to => :foo option in the draftable class method, so that this model can be approved as part of its parent model. This method finds the ‘approver’, i.e. the Draft object which is the parent object of this one and for which calling the ‘approve’ method will approve this subdraft too.

This is currently quite weak, in the sense that it’ll screw up as soon as you depart from standard AR class naming conventions. It’s a good target for future development, but I think at the moment, IAGNI.

At the moment, we just return the parent if a delegate exists. This should do the trick for us right now, as we’re not using deeply nested object graphs that need approvals. I’ve got the start of an alternate, somewhat stronger implementation, underneath that, but we don’t have time to do up the general case right now.

@return [Draft] approver the draft object which serves as the approval

context for this one. Returns self or a delegated draft object.
# File lib/drafter/draft.rb, line 93
def approver
  delegate = self.inflate.class.delegate_approval_to
  if delegate
    parent
  else
    self
  end
end
inflate() click to toggle source

Find the existing draftable, or build a new one, and set it up.

@return the existing draftable object, or a new one of the proper

type, with attributes and files hanging off it.
# File lib/drafter/draft.rb, line 68
    def inflate
draftabl = draftable.nil? ? self.draftable_type.constantize.new(:draft => self) : draftable
draftabl.draft = self # this should absolutely *not* be necessary, there's an STI bug in AR.
draftabl.apply_draft
draftabl
    end
method_missing(meth, *args, &block) click to toggle source

Set things up so we can use dot notation to access draft data, e.g. we can do either @foo.draft.data or (more neatly) we can do @foo.draft.title

Calls superclass method
# File lib/drafter/draft.rb, line 56
def method_missing(meth, *args, &block)
  if self.data.keys.include?(meth.to_s)
    self.data[meth.to_s]
  else
    super
  end
end
reject!() click to toggle source

Reject the draft, basically destroying the draft object and leaving the draftable unchanged.

# File lib/drafter/draft.rb, line 48
def reject!
  self.destroy
end